Anyone knows how to change this to work with 3 integers instead?

// Example 5 - Receive with start- and end-markers combined with parsing

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

      // variables to hold the parsed data
char messageFromPC[numChars] = {0};
int integerFromPC = 0;
float floatFromPC = 0.0;

boolean newData = false;

//============

void setup() {
    Serial.begin(9600);
    Serial.println("This demo expects 3 pieces of data - text, an integer and a floating point value");
    Serial.println("Enter data in this style <HelloWorld, 12, 24.7>  ");
    Serial.println();
}

//============

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 (Serial.available() > 0 && newData == false) {
        rc = Serial.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(messageFromPC, strtokIndx); // copy it to messageFromPC
 
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    integerFromPC = atoi(strtokIndx);     // convert this part to an integer

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

}

//============

void showParsedData() {
    Serial.print("Message ");
    Serial.println(messageFromPC);
    Serial.print("Integer ");
    Serial.println(integerFromPC);
    Serial.print("Float ");
    Serial.println(floatFromPC);
}

Original Code from here
I am trying to communicate my Arduino with ESP32. Now the last part left is to make the values obtained in Arduino send over to ESP32 and make the values a variable so they can be read by Arduino IoT Cloud. I searched for solutions. However, the best solution is what I found on this thread, which then led me to the code above. I am a beginner, so I am very sorry for my incompetence.
Thank you!

talk to @errols ...

if you are sure you are receiving 3 data, just add one
strtokIndx = strtok(NULL, ",");

and use something like

    integerFromPC = atoi(strtokIndx);     // convert this part to an integer

to extract the data

You mean 3 integers instead of 1 string, 1 integer and 1 float?

If so, I think the code you have found was purposely written to provide examples of each of those 3 kinds of values. It currently reads the message to extract an integer, you just need to do that 3 times.

But if you are a beginner, this is not the best way to start. It's too complex and confusing. Start with simple tutorials that flash LEDs etc. You need to understand the basics of programming in C before tackling something like this.

consider

char s [80];

#define MaxTok  10
char *toks [MaxTok];
int   vals [MaxTok];

// -----------------------------------------------------------------------------
int
tokenize (
    char       *s,
    const char *sep )
{
    unsigned n = 0;
    toks [n] = strtok (s, sep);
    vals [n] = atoi (toks [n]);

    for (n = 1; (toks [n] = strtok (NULL, sep)); n++)
        vals [n] = atoi (toks [n]);

    return n;
}

// -----------------------------------------------------------------------------
void dispToks (
    char * toks [])
{
    char s [40];
    for (unsigned n = 0; toks [n]; n++)  {
        sprintf (s, " %6d  %s", vals [n], toks [n]);
        Serial.println (s);
    }
}

// -----------------------------------------------------------------------------
void loop ()
{
    if (Serial.available ())  {
        int n = Serial. readBytesUntil ('\n', s, sizeof(s)-1);
        s [n] = 0;      // terminate string

        tokenize (s, ",");
        dispToks (toks);
    }
}

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

Thank you everyone for the help! I found the solution that I wanted it is from this site. The code for breaking up a single string into different variables are as follows:

if(Serial.available()){
  String rxString = "";
  String strArr[2]; //Set the size of the array to equal the number of values you will be receiveing.
  //Keep looping until there is something in the buffer.
  while (Serial.available()) {
    //Delay to allow byte to arrive in input buffer.
    delay(2);
    //Read a single character from the buffer.
    char ch = Serial.read();
    //Append that single character to a string.
    rxString+= ch;
  }
  int stringStart = 0;
  int arrayIndex = 0;
  for (int i=0; i < rxString.length(); i++){
    //Get character and check if it's our "special" character.
    if(rxString.charAt(i) == ','){
      //Clear previous values from array.
      strArr[arrayIndex] = "";
      //Save substring into array.
      strArr[arrayIndex] = rxString.substring(stringStart, i);
      //Set new string starting point.
      stringStart = (i+1);
      arrayIndex++;
    }
  }
  //Put values from the array into the variables.
  String value1 = strArr[0];
  String value2 = strArr[1];
  //Convert string to int if you need it.
  int intValue1 = value1.toInt();
}

I studied and changed the code to like this:

String value1,value2,value3,value4,value5;
char ch;
int aqi,co;
float temp,hum;
 if(Serial2.available()){
  String rxString = "";
  String strArr[4]; //Set the size of the array to equal the number of values you will be receiveing.
  //Keep looping until there is something in the buffer.
  while (Serial2.available()) {
    //Delay to allow byte to arrive in input buffer.
    delay(2);
    //Read a single character from the buffer.
    ch = Serial2.read();

    //Append that single character to a string.
    rxString+= ch;
  }
  int stringStart = 0;
  int arrayIndex = 0;
  
  for (int i=0; i < rxString.length(); i++){
    //Get character and check if it's our "special" character.
    if(rxString.charAt(i) == ','){
      //Clear previous values from array.
      strArr[arrayIndex] = "";
      //Save substring into array.
      strArr[arrayIndex] = rxString.substring(stringStart, i);
      //Set new string starting point.
      stringStart = (i+1);
      arrayIndex++;
    }
  }

  //Put values from the array into the variables.
  value1 = strArr[0];
  value2 = strArr[1];
  value3 = strArr[2];
  value4 = strArr[3];
  value5 = strArr[4];
  //Convert string to int if you need it.
  aqi = value1.toInt();
  co = value2.toInt();
  temp = value3.toFloat();
  hum = value4.toFloat();

I am still a bit new to this. The solution provided by gcjr and J-M-L doesnt seem to work for me. Moreover, I am not familiar with the code, I dont even know like 80% of the code even does lol. Therefore, I found the next solution. I think that this solution is better for begineers like me and the code is easier to understand, I manged to tweak them by myself.

Anyways I hope it helps other people in the future! I am not a native English speaker, hopefully you guys can understand my english. Thank you very much!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.