How to read substring of array message (string, char data types)

Hi there! I am having a problem on how to read and process each of the element in the array.
I will receive a message something like {Q, "senID": "AB", "destID": "OE", Hello world }
There are four different element. How do I read them separately? I get confused on the use of array for arduino.
Thanks in advance!

Check out strtok(). With this example program:

#include <string.h>

const char szStr[] = "{Q, \"senID\": \"AB\", \"destID\": \"OE\", Hello world }";

const char s[2] = "\"";
char *token;

void setup() 
{

   Serial.begin( 115200 );
   
   /* get the first token */
   token = strtok(szStr, s);
   
   /* walk through other tokens */
   while( token != NULL ) 
   {
      Serial.println( token );
    
      token = strtok( NULL, s );
   }

}//setup

void loop() 
{
}//loop

you can break down the string into sub-strings, delimited by, say, the '"' character. The output to the serial monitor:

{Q, 
senID
: 
AB
, 
destID
: 
OE
, Hello world }

What parts are you trying to extract? It seems like an odd structure...

I am doing LoRa communication project, so it is the message between nodes.
I wanted to split them into four part, so that I can read the source ID, destination ID and message correctly.
For example, I need to identify the source ID is AB at the receiver part..
The structure of message didn't change but the content did. I am confused on how do I extract them..

Please post the transmit and receive code, using code tags.

I haven't completed my coding yet, I am just referring some coding which I don't really understand. That's why I am trying to write the script and I faced this problem.
May I know what code tags u are referring to?

String Routing[nodeClient+1][2] << for this one, can anyone help to explain what is the meaning of the [ ][ ] for a string? The nodeClient is a variable as well.

re String Routing[nodeClient+1][2]
That is declaring a 2D array of Strings rows 0 to nodeClient, cols 0 to 1
i.e. two Strings for each node, see the code below
Check out my Taming Arduino Strings tutorial if you are using a lot of Strings

This code does the parsing using Arduino Strings

String input = "{Q, \"senID\": \"AB\", \"destID\": \"OE\", Hello world }";

const size_t nodeClient = 4;
String Routing[nodeClient+1][2];

void setup() {
  Serial.begin(9600);
  for (int i = 10; i > 0; i--) {
    Serial.print(' '); Serial.print(i);
    delay(500);
  }
  Serial.println(); 

  // looking for " "
  int startIdx = input.indexOf('"'); // the " char
  while (startIdx >=0) {
    startIdx += 1; // step over the "
    int endIdx = input.indexOf('"',startIdx); // step over the starting " char 
    String word = input.substring(startIdx,endIdx);
    Serial.println(word);
    startIdx = input.indexOf('"',endIdx+1); // look for the next "
  }
  int counter = 1;
  for(size_t i=0; i<=nodeClient; i++) {
    Routing[i][0] = counter;
    counter += 1;
    Routing[i][1] = counter;
    counter += 1;
  }
  for(size_t i=0; i<=nodeClient; i++) {
    Serial.print(Routing[i][0]); Serial.print(" "); Serial.println(Routing[i][1]);
  }
  
}

void loop() {
  
}

The output is

senID
AB
destID
OE
1 2
3 4
5 6
7 8
9 10

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