0

I'm currently using google geolocation to get location data to one nodemcu and sending it through serial to another Arduino. The serial command I'm getting right now is,

1.450187T103.824745 

I'm using the following code to parse it.

 if (Serial.available()) {
 char c = Serial.read(); //gets one byte from serial buffer
 //if (c == '\n') { //looks for end of data packet marker
 if (c == '\n') {
 // Serial.println(readString); //prints string to serial port out
 //do stuff 
 substring = readString.substring(0,8);
 lati = substring;
 loc = readString.indexOf("T");
 substring = readString.substring(loc+1, loc+11);
 longi = substring;
 readString=""; //clears variable for new input
 substring=""; 
 } 
 else { 
 readString += c; //makes the string readString
 }
 }

It works most of the time but sometimes I get 'T' inside the string as well. like this

lat=1.450146&longi=102464T103

What's wrong in my code?

VE7JRO
2,51519 gold badges27 silver badges29 bronze badges
asked Jun 23, 2019 at 6:34
2
  • what is the reason for transmitting the H? ... you only need the T to separate the two values Commented Jun 23, 2019 at 6:54
  • Can you show us the actual serial String, that you received in that case? Commented Jun 23, 2019 at 9:17

1 Answer 1

1

with readBytesUntil and C string

if (Serial.available()) {
 char buff[32];
 int l = Serial.readBytesUntil('\n', buff, sizeof(buff));
 if (l > 0 && buff[l - 1] == '\r') {
 l--; // to remove \r if it is there
 }
 buff[l] = 0; // terminate the string
 char* p = strchr(buff,'T'); // returns pointer in string
 if (p != NULL) {
 *p = 0; // write 0 as char at pointer to terminate the first part
 p++; // move the pointer to next char
 Serial.println(buff);
 Serial.println(p);
 } else {
 Serial.println(buff);
 }
}
answered Jun 23, 2019 at 11:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.