Parse data

Hi,

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.

char * strtokIndx; 
float EC = 0.0;
float Temp = 0.0;
float Moisture=0.0;

char resp[]="0+1254-1.5+4215";

    strtokIndx = strtok(resp,"+-");    
   
   
    strtokIndx = strtok(NULL, "+-"); 
    Moisture = atof(strtokIndx);     
    Serial.println("Moisture is");
    Serial.println(Moisture);

    strtokIndx = strtok(NULL, "+-");
    Temp = atof(strtokIndx);    
    Serial.println("Temp is");
    Serial.println(Temp);

    strtokIndx = strtok(NULL, "+-");
    EC = atof(strtokIndx);     
    Serial.println("EC is");
    Serial.println(EC);

Rustom:
Hi,

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'

something like this maybe:

void setup() {
  char *strtokIndx, *strstrIndex, *resp_copy_ptr;
  float EC = 0.0;
  float Temp = 0.0;
  float Moisture = 0.0;

  char resp[] = "0+1254-1.5+4215";

  char resp_copy[sizeof(resp)];

  Serial.begin(115200);

  memcpy(resp_copy, resp, strlen(resp) + 1);
  resp_copy_ptr = resp_copy;

  strtokIndx = strtok(resp, "+-");

  strtokIndx = strtok(NULL, "+-");
  strstrIndex = strstr(resp_copy_ptr, strtokIndx);
  resp_copy_ptr = strstrIndex - 1;
  Moisture = atof(resp_copy_ptr);
  Serial.print("Moisture is ");
  Serial.println(Moisture);

  strtokIndx = strtok(NULL, "+-");
  strstrIndex = strstr(resp_copy_ptr, strtokIndx);
  resp_copy_ptr = strstrIndex - 1;
  Temp = atof(resp_copy_ptr);
  Serial.print("Temp is ");
  Serial.println(Temp);

  strtokIndx = strtok(NULL, "+-");
  strstrIndex = strstr(resp_copy_ptr, strtokIndx);
  resp_copy_ptr = strstrIndex - 1;
  EC = atof(resp_copy_ptr);
  Serial.print("EC is ");
  Serial.println(EC);

}

void loop() {
  // put your main code here, to run repeatedly:

}

Output:

Moisture is 1254.00
Temp is -1.50
EC is 4215.00

hope that helps...

The parse example in Serial Input Basics my give you some ideas.

...R

what about using sscanf?

    char resp[]="0+1254-1.5+4215";
    int  a, b, c, d;

    sscanf (resp, "%d%d%d%d", &a, &b, &c, &d);

    char out [80];
    sprintf (out, " %d %d %d %d", a, b, c, d);
    Serial.println (out);

Hi,

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?

...R

Rustom:
Hi,

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?

sherzaad:
the method offered in reply #1 broke down the string 0+1818.28+22.5+0 correctly.

#include <SDISerial.h>
#define DATA_PIN 2
SDISerial connection(DATA_PIN);

char* resp; // Pointer to response char

//initialize variables
void setup(){

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);

}

@Rustom, what is your Reply #7 intended to convey?

You have posted code without any discussion of it.

...R

I have posted the code to read sensor data. Unfortunately when the temperature goes down to zero, I am unable to get the sign.

What sensor are you using? Do you have a datasheet for it?

Blackfin:
What sensor are you using? Do you have a datasheet for it?

Teros 12. Its a soil sensor. It uses SDI-12 serial communication protocol. I have used software SDI-12 library.

"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.

Rustom:
I have posted the code to read sensor data. Unfortunately when the temperature goes down to zero, I am unable to get the sign.

You're trying to find the sign of 0 degrees? What? :o

Rustom:
Teros 12. Its a soil sensor. It uses SDI-12 serial communication protocol. I have used software SDI-12 library.

For the interested: SDI-12 support group

Rustom:
I have posted the code to read sensor data. Unfortunately when the temperature goes down to zero, I am unable to get the sign.

According to the integrator manual, the response to the data command aD0! will be:

a+<calibratedCountsVWC>±<temperature>+<electricalConductivity>

It looks like the '±' should be there even for a temperature of 0.0.

Can you post an example response for T=0.0?