I need to parse data from a sensor. The data format is 0+1254-1.5+4215 (moisture+temperature+electrical conductivity). If I use the following code, I could parse the data but I need the sign of temperature. Please help me.
I need to parse data from a sensor. The data format is 0+1254-1.5+4215 (moisture+temperature+electrical conductivity). If I use the following code, I could parse the data but I need the sign of temperature. Please help me.
you could use the 'strstr' function along with 'strtok'
Thanks for your help. I apologize for not informing you. I am reading the data for every 1 seconds through serial port. The data always changes. For example: for this data 0+1818.28+22.5+0, its not working properly. Could you help me?
Rustom:
Thanks for your help. I apologize for not informing you. I am reading the data for every 1 seconds through serial port. The data always changes. For example: for this data 0+1818.28+22.5+0, its not working properly. Could you help me?
Have you tried the suggestions you have already received? If so what happened?
Thanks for your help. I apologize for not informing you. I am reading the data for every 1 seconds through serial port. The data always changes. For example: for this data 0+1818.28+22.5+0, its not working properly. Could you help me?
the method offered in reply #1 broke down the string 0+1818.28+22.5+0 correctly.
Moisture is 1818.28
Temp is 22.50
EC is 0.00
Please POST THE CODE you are using to read this sensor data
also do you have some datasheet for the sensor that describes the string it outputs?
connection.begin();
Serial.begin(57600);
//small delay to let the sensor do its startup stuff
delay(3000);//3 seconds should be more than enough
}
//main loop
void loop(){
//send measurement query (M) to the first device on our bus
resp = connection.sdi_query("?M!",1000);//1 second timeout
//this really just returns a message that tells you the maximum wait before the measurement is ready
delay(1000);//sleep for 1 seconds before the next command
resp = connection.sdi_query("?D0!",1000);//1 second timeout
sprintf(output_buffer,"%s",resp?resp:"No Response Recieved!!");
char * strtokIndx; // this is used by strtok() as an index
float EC = 0.0;
float Temp = 0.0;
float Moisture=0.0;
strtokIndx = strtok(resp,"+"); // get the first part - the string
//strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(NULL, "+"); // this continues where the previous call left off
Moisture = atof(strtokIndx); // convert this part to an integer
Serial.println("Moisture is");
Serial.println(Moisture);
strtokIndx = strtok(NULL, "+");
Temp = atof(strtokIndx); // convert this part to a float
Serial.println("Temp is");
Serial.println(Temp);
strtokIndx = strtok(NULL, "+");
EC = atof(strtokIndx); // convert this part to a float
Serial.println("EC is");
Serial.println(EC);
delay(1000);
"I have posted the code to read sensor data. Unfortunately when the temperature goes down to zero, I am unable to get the sign."
Are all the signs numeric indicators? If only one goes negative then you should be able simply search for a "-" and know the temp is going to be a negative value.