stringarduinoxbeelcd

Serial Data Read and parse to variables from continuous string (Arduino)


I have 2 Arduino and 2 xbee. I send 2 sensor data from Arduino 1 (router) to Arduino to (coordinator): On coordinator, I receive wireless data from this 2 sensors(from router) perfectly.

The data stream is something like this:

20.1324325452924 divided in:
-first sensor(temperature): 20.1324325452
-second sensor(gas):924

My goal is to have these 2 values as 2 variables that get updated constantly and then pass these values on to the rest of the program to make something like print on LCD or something else:

temperature=20.1324325452
gas=924

I managed to divide that initial string that I receive on serial (20.1324325452924) in 2 variables but values from this 2 variables not updating like in the initial string (when sensor values are changed):

My code:

LiquidCrystal lcd(12,11,10,9,8,7);
String temperature;
String gas;
String readString;

char IncomingData[13];

 
 void setup() {
 Serial.begin(9600);                                     
}

 void loop() {
 while (Serial.available() > 0)                           
 {
       char IncomingData = Serial.read();                   
        readString += IncomingData ;     
        temperature = readString.substring(0, 13); //get the first 13 characters
        gas = readString.substring(13, 16); //get the last 3 characters 

        Serial.print(IncomingData);  //here I have my string: 20.1324325452924  which is updating properly when I have sensor values changes 
        
        // Process message when new line character is DatePrimite
        if (IncomingData == '\n')
        {
          Serial.println(temperature);
           lcd.begin(16, 2);
          lcd.setCursor(0,0);                              
          lcd.write("T:");
          lcd.print(temperature);               
         delay(500);
         temperature = "";                               // Clear DatePrimite buffer
     
         Serial.println(gaz);
         lcd.begin(16, 2);
         lcd.setCursor(0,1);                              
         lcd.write("G:");
         lcd.print(gas);
         delay(500);
         gaz = "";                                       // Clear DatePrimite buffer
      }
    }
}

Output from serial:
20.1324325452924
20.1324325452
924

First string it's updating when I receive new sensor data but the next 2 remains the same every time. I'm stuck for days I don't know to do this work. All I need to do is to divide the initial string which contains the data from 2 sensors in 2 variables that get updated constantly and then pass these values on to the rest of the program to make something like print on LCD.

Does anyone have any idea how to make this work?


Solution

  • Split the data after you receive the complete string.

    void loop() {
    
      while(!Serial.available()); // wait till data to be filled in serial buffer
    
      String incommingStr = Serial.readStringUntil('\n'); // read the complete string
    
      String temperature = incommingStr.substring(0, 13);
      String gas = incommingStr.substring(13, 16);
    
      Serial.print(incommingStr);
      Serial.println(temperature);
      Serial.println(gas);
    
      lcd.setCursor(0,0);                              
      lcd.print(temperature);
    
      lcd.setCursor(0,1);                              
      lcd.print(gas);
    
      delay(500);
    }
    

    You only need to call lcd.begin() once. Calling it from the setup() function.