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,


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

    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


What's wrong in my code?

  • what is the reason for transmitting the H? ... you only need the T to separate the two values – jsotola Jun 23 '19 at 6:54
  • Can you show us the actual serial String, that you received in that case? – chrisl Jun 23 '19 at 9:17

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
  } else {

You could use a for loop to get the location of the "T", then split the String into 2 Strings.

// 4104 bytes.
String readString = "1.450187T103.824745";
String lati, longi;

void setup(){


  //do stuff code
  for(int i = 0; i < readString.length(); i++){
    if(readString.charAt(i) == 'T'){
      lati = readString.substring(0, i);
      longi = readString.substring(i + 1);



void loop(){}
  • Why using String.substring() when checking for a single character? Why not accessing the character array directly? – chrisl Jun 23 '19 at 10:02
  • Based on the OP's code, I was assuming they wanted to use the String object, not the "C-style" string (char array). Of course the char array is the preferred method and I'm sure the compile size will be far less using it. – VE7JRO Jun 23 '19 at 15:43
  • I've changed the String.substring() to String.charAt() and the compile size dropped 102 bytes. Thank you @chrisl. – VE7JRO Jun 23 '19 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.