Parsing in and comparing a character stream

Hello!!

I’m trying to find a set of 3 integers from a web response.

My MCU receives the following response from a web server:

Connecting to: 192.168.86.131:80
CONNECTED TO THE SERVER
receiving from remote server
HTTP/1.1 200 OK
Date: Sun, 23 Feb 2020 12:27:02 GMT
Server: Apache/2.4.37 (Win32) OpenSSL/1.1.1 PHP/7.2.12
X-Powered-By: PHP/7.2.12
Content-Length: 14
Connection: close
Content-Type: text/html; charset=UTF-8

>127<>50<>250<
closing connection

when executing the following code:

//Start reading the response from the server
  while (client.available()) {
    char ch = static_cast<char>(client.read());
    Serial.print(ch);
  }
  // Close the connection
  Serial.println();
  Serial.println("closing connection");
  client.stop();
  delay(6000);

however when I try to filter out everything but the 3 integers like so:

//Start reading the response from the server
  while (client.available()) {
    char ch = static_cast<char>(client.read());
    Serial.print(ch);
  //-------integer finder------------
    char toFind = '>';
    char toFend = '<';
    if(ch==toFind) {
      while(ch != toFend) {
        Serial.print('val:');
        ch = static_cast<char>(client.read());
        Serial.print(ch);
      }
    }
//-----------------------------------
  }
  // Close the connection
  Serial.println();
  Serial.println("closing connection");
  client.stop();
  delay(6000);

I get the following:

Connecting to: 192.168.86.131:80
CONNECTED TO THE SERVER
receiving from remote server
HTTP/1.1 200 OK
Date: Sun, 23 Feb 2020 12:30:45 GMT
Server: Apache/2.4.37 (Win32) OpenSSL/1.1.1 PHP/7.2.12
X-Powered-By: PHP/7.2.12
Content-Length: 14
Connection: close
Content-Type: text/html; charset=UTF-8

>1986096186119860961862198609618671986096186<>19860961865198609618601986096186<>1986096186219860961865198609618601986096186<
closing connection

Can’t quite wrap my head around what’s going on here.

What I know I could possibly be doing wrong is my character equality comparison.

I don’t imagine parsing in one character from the stream at a time is my hangup but I might be overseeing something?

I also just realized my 'val:' is never making it out to the serial terminal :-/

Got it!

//Start reading the response from the server
    String Rval;
    String Gval;
    String Bval;
    while (client.available()) {
      char ch = static_cast<char>(client.read());
      //Serial.print(ch);

      char toFind = '>';
      char toFend = '<';

      // get first value
      if (ch == toFind) {
        //Serial.println("Found the marker character!!");
        while (ch != toFend) {
          //Serial.print('val:');
          ch = static_cast<char>(client.read());
          if (ch != toFind && ch != toFend) {
            Rval += ch;
          }
        }
      }
      // get second value
      if (ch == toFind) {
        //Serial.println("Found the marker character!!");
        while (ch != toFend) {
          //Serial.print('val:');
          ch = static_cast<char>(client.read());
          if (ch != toFind && ch != toFend) {
            Gval += ch;
          }
        }
      }
      // get third value
      if (ch == toFind) {
        //Serial.println("Found the marker character!!");
        while (ch != toFend) {
          //Serial.print('val:');
          ch = static_cast<char>(client.read());
          if (ch != toFind && ch != toFend) {
            Bval += ch;
          }
        }
      }
    }
    // Make sure we got the right values:
    Serial.println(Rval);
    Serial.println(Gval);
    Serial.println(Bval);
    rint = Rval.toInt();
    gint = Gval.toInt();
    bint = Bval.toInt();
    // Close the connection
    Serial.println();
    Serial.println("closing connection");
    client.stop();

Now for data typecasting :smiley:

        Serial.print('val:');

this is not the right way. in C or C++ a string is in double quotes

        Serial.print("val:");

J-M-L:

        Serial.print('val:');

this is not the right way. in C or C++ a string is in double quotes

        Serial.print("val:");

Thank you!

I sometimes accidentally type "System.out.println" too :grinning: