1

I'm new here, so I hope my question is not naive. I'm using Arduino mega connected with a pulse sensor and a GPS (and other things that are not related to our problem), pulse sensor and GPS data are sent momentarily through HC-12 433 Mhz module to a receiver arduino uno that is only receiving data through HC-12 as well

for processing reasons, I send GPS data with a "G" prefix .. e.g "G11.00,7.45". and pulse data consist of those three: "S100" "Q97" "B22" // 100,97,22 are just examples, and each one is sent alone

for some reason, the receiver Arduino uno, is reading the data in it's ASCII decimal form and also separate it in a new line.

I can not use Serial.readString(), because it only print data after terminating the Serial communication and I need it momentarily, I tried to configure Serial.setTimeout() , but I couldn't manage to find a suitable time for all data.

please check my code and help me solve my problem, thank you

#include <Adafruit_GPS.h>
// what's the name of the hardware serial port?
#define GPSSerial Serial1
// Connect to the GPS on the hardware port
Adafruit_GPS GPS(&GPSSerial);

String gpsData="";
String pulseData="";
#include <SoftwareSerial.h>
SoftwareSerial mySerial(12, 13); // RX, TX  HC-12
uint32_t timer = millis();
void setup(){  // I think there is no problem here
  Serial.begin(115200);             // we agree to talk fast!
  mySerial.begin(115200);
  interruptSetup();   // initialize timers to read pulse sensor and GPS at fixed intervals
      GPS.begin(9600);
  delay(100);
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  delay(100);
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_100_MILLIHERTZ);
  GPS.sendCommand(PGCMD_ANTENNA);
  delay(1000);
  GPSSerial.println(PMTK_Q_RELEASE);
}
void loop(){
 // that show me if there is a new data, you can skip it
 if (GPS.newNMEAreceived()) {
    // a tricky thing here is if we print the NMEA sentence, or data
    // we end up not listening and catching other sentences!
    // so be very wary if using OUTPUT_ALLDATA and trying to print out data
    GPS.lastNMEA(); // this also sets the newNMEAreceived() flag to false
    if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
      return; // we can fail to parse a sentence in which case we should just wait for another
  }

  //
if (millis() - timer > 10000) { // send GPS data every 10 seconds
    timer = millis(); // reset the timer
    if (GPS.fix) {
      
      gpsData.concat('G');
      x=GPS.latitude/100;
      gpsData.concat(x);  // I also tried String(x)
      gpsData.concat(',');
      y=GPS.longitude/100;
      gpsData.concat(y);  // I also tried String(y)
      mySerial.println(gpsData);  // here I send GPS  data through HC-12 to the receiver Arduino uno
      gpsData=""; // reset 
    }
}
  sendDataToProcessing('S', Signal);     // send Processing the raw Pulse Sensor data
  if (QS == true){                       // Quantified Self flag is true when arduino finds a heartbeat
       
        sendDataToProcessing('B',BPM);   // send heart rate with a 'B' prefix
        sendDataToProcessing('Q',IBI);   // send time between beats with a 'Q' prefix
        QS = false;                      // reset the Quantified Self flag for next time    

  }
  delay(50);
}
void sendDataToProcessing(char symbol, int data ){
 // here I send pulse data through HC-12 to the receiver Arduino uno, and then process it using processing in the receiver arduino
    pulseData.concat(symbol);
    pulseData.concat(data);
    Serial.println(pulseData);  // just for debugging and it was okay 
    mySerial.println(pulseData);
    pulseData=""; //reset
  }

interruptSetup() {
// no need to read it, it is just some timer 1 and timer 2 initialization and their ISR, timer 1 read pulse sensor every 2 milliseconds (yeah fast but I guess mega could handle it),and do some calculations to find S, Q, and B.
and timer 2 read GPS coming sentences.
}

the receiver code

void setup() {
  // put your setup code here, to run once:
Serial.begin(115200);  //I will connect HC-12 directly to pin 0,1
}

void loop() {
  // put your main code here, to run repeatedly:
while(Serial.available() >1){
  String input = String (Serial.read());//read string , I need to cast it into String because data comes in ASCII
// I also tried int input = Serial.read() ; //same thing happened
    Serial.println(input);
}
}

waiting for your response.

###edit### to conclude the problem is that I want to receive data in the uno as it has been sent from the mega

e.g: I receive sI guess the ascii of the letter and the number also changes to some number

send  |  recive
S490  | 52
        53 
Q252  | 10
        83
B160  | 48
        13

I used Serial.write(Serial.read()) instead of Serial.println, and it worked, does someone know why?

7
  • 1
    It's hard for me to understand what your problem is, but I think changing Serial.println(input); to Serial.print(input); will remove the added newlines (if that was the problem). Commented Aug 14, 2020 at 8:50
  • I tried to restate the problem, check it again please Commented Aug 14, 2020 at 9:03
  • I think, using char input = Serial.read(); would have worked in your old code. Serial.read returns and int with the character/ascii code (or -1 if it gets a read-error). Serial.print doesn't know it's ascii and will interpret it as a integer, and will convert this number to the Ascii representation of that number. Apparently String(...) does the same. Write doesn't try to be clever, and just reads a byte, an sends this byte out over serial. Is your problem solved now? Commented Aug 14, 2020 at 9:45
  • No, it is now printing the same output but each char is in a new line, for example like this : ``` B 1 2``` Commented Aug 15, 2020 at 7:35
  • 1
    oh yeah it worked, I didn't notice, thank you ....... Commented Aug 15, 2020 at 15:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.