Parsing several pieces of data to separate variables

Hello Everyone,

I have been going over the Serial Input Basics part 5, So far have it working but struggling to archive the last step I need.

I am receiving 4 pieces of data Humidity:,Temperature:,49.00,22.00 via serial, what I am trying to do is assign them to there own variables.

For instance DataA =“Humidity” DataB =“Temperature”,

The rest is working fine the float Humidity, float temperature etc are working ok but with the code I have so far DataA and DataB are coming out the same they are both saying Humidity:

I am clearly missing a step but would appreciate a nude in the right direction.

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); //RX, TX

const byte numChars = 50;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing

      // variables to hold the parsed data
char DataA[numChars] = {0};
char DataB[numChars] = {0};
float humidity = 0.0;
float temperature = 0.0;

boolean newData = false;

void setup() {
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
    mySerial.begin(9600);
}

void loop() {
    recvWithStartEndMarkers();
    if (newData == true) {
        strcpy(tempChars, receivedChars);
            // this temporary copy is necessary to protect the original data
            // because strtok() used in parseData() replaces the commas with \0     
        parseData();
        showParsedData();
        newData = false;
    }
}

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;
    
 
    while (mySerial.available() > 0 && newData == false) {
        rc = mySerial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

void parseData() {      // split the data into its parts

    char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(tempChars,",");      // get the first part - the string
    strcpy(DataA, strtokIndx); // copy it to DataA

    strtokIndx = strtok(tempChars,",");      // get the first part - the string
    strcpy(DataB, strtokIndx); // copy it to DataB
 
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    humidity = atof(strtokIndx);     // cconvert this part to a float

    strtokIndx = strtok(NULL, ",");
    temperature = atof(strtokIndx);     // convert this part to a float

}

void showParsedData() {
    Serial.print(DataA);
    Serial.println(DataB);

//    Serial.print("Humid");
//    Serial.print(" ");
//    Serial.print(humidity);
//    Serial.print("%  ");
//    Serial.print("Temp");
//    Serial.print(" ");
//    Serial.print(temperature);
//    Serial.print("°C  ");
//    Serial.print("Sound Level");
//    Serial.print(" ");
//    Serial.print(sound);
//    Serial.println(" Dbspl ");
}

This:

    strtokIndx = strtok(tempChars,",");      // get the first part - the string
    strcpy(DataB, strtokIndx); // copy it to DataB

should be:

    strtokIndx = strtok(NULL,",");      // get the first part - the string
    strcpy(DataB, strtokIndx); // copy it to DataB

strtokIndx = strtok(NULL,","); // get the first part - the string
strcpy(DataB, strtokIndx); // copy it to DataB

@ToddL1962 Thank you very much, exactly what I needed to achieve. :slight_smile: :slight_smile: