2 ESPs communicating using Arduino IDE (working), trouble accessing data

Hello,
I am sending bits from one ESP as client to the other ESP as server.
This is working but I can't figure out how to access specific data in an array.
For example, I send from the client to the server

// val12 is an on/ off switch
  Serial.println(val12);
  client.println(val12);      
  client.println('d');

and access the value of val12 correctly here:

 WiFiClient client = server.available();
  if (client) {
    if (client.connected()) {     
       String request12 = client.readStringUntil('d'); 
       request12 = request12.charAt(0);
       delay(10);
       Serial.println("val12 is " + request12);

Similarly, I do the same with the next byte, receiving the correct value as follows:

// Client sends val13
  Serial.println(val13);
  client.println(val13);     
  client.println('f');
// Server receives val13
WiFiClient client = server.available();
  if (client) {
    if (client.connected()) {     
       String request12 = client.readStringUntil('d'); 
       request12 = request12.charAt(0);
       delay(10);
       Serial.println("val12 is " + request12);
       
       String request13 = client.readStringUntil('f');
       request13 = request13.charAt(2);
       delay(10);
       Serial.println("val13 is " + request13);

All seems good until I try to access the NEXT byte.

   //Client sends val14 to server
      Serial.println(val14);
      client.println(val14);      
      client.println('r');       // send the data

Receives

       String request14 = client.readStringUntil('r'); 
       request14 = request14.charAt(4);
       delay(10);
       Serial.println("val14 is " + request14);

It seems logical that request14 = request14.charAt(4); would be the correct sequence, but this yields nothing. Trying 5,6,7,8 in place of 4 also yields nothing.
Removing the line request14 = request14.charAt(4); altogether provides the correct value of the switch, so I know it's in the array.

Question:
What is the size of the data being sent? It is all in String format.
Is the size different from 'd' to "d"?
What is the logical place for val14 to be putting its data?

Thanks in advance. :slight_smile:

The ones that work expect a 1 character value followed by a letter, so they read the character at position 0.

The one that doesn't work is sending a 1 character value followed by a letter, but trying to read the 4th or later character of the 2 received. Why?

The first expects a 1 character value followed by a letter (works at position 0)
The second expects a 1 character value followed by a letter (works at position 2)
The third expects a 1 character value followed by a letter (does not work at position 3,4,5,6,7,8), but does work if no position is specified.

I don't know how to interpret this. Do you? :smiley:

The second expects a 1 character value followed by a letter (works at position 2)

This does not make sense.

Suppose you send "0d1f0r". You read a String, "0d". The value of interest is at position 0. Then, you read another String, "1f". The value of interest is NOT in position 2. It is in position 0.

Then, you read another String, "0r". The value of interest is NOT in position 4, 5, 6, 7, 8 or anything other than 0.

If you are diddling around somehow, and reading nothing, nothing, and "0d1f0r", and then looking for the r, you need to pay attention to where the r is in the String, and extract the one character at the position one less than the position of the r.

You REALLY should be sending something more like "0d!1f!0r!". Then, you can read up to the !, and determine the last letter in the String, and storing the value at the appropriate place based on that letter. That way, each time you read a String, you'll get exactly three characters.

That works a little better, thanks!

// Client sending data
    Serial.println(val12);
    client.println(val12);      
    Serial.println(val13);
    client.println(val13);     
    Serial.println(val14);      
    client.println(val14);      
    Serial.println(val16);     
    client.println(val16);       
    client.println('y');
       //Server receiving data
       String request = client.readStringUntil('y'); 
       request12 = request.charAt(0);      
       request13 = request.charAt(3);
       request14 = request.charAt(6);
       request16 = request.charAt(9);
// Client sending data
    Serial.println(val12);
    client.print(val12);     
    Serial.println(val13);
    client.print(val13);     
    Serial.println(val14);     
    client.print(val14);     
    Serial.println(val16);     
    client.print(val16);       
    client.println('y');
       //Server receiving data
       String request = client.readStringUntil('y');
       if(request.length() >= 4)
       {
          request12 = request.charAt(0);     
          request13 = request.charAt(1);
          request14 = request.charAt(2);
          request16 = request.charAt(3);
       }

Sending less data is better. Defensive programming is better.