Go Down

Topic: Filter out the right information of my http get function (Read 127 times) previous topic - next topic

Kassie

Hi I am working on a led/relay I can control using my website. So everything seems to be working and when I print my string on my serial monitor
 
Code: [Select]

while (client.available()) {
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }



 I receive this:

Code: [Select]

HTTP/1.1 200 OK
Date: Thu, 17 Aug 2017 12:50:32 GMT
Server: Apache
Upgrade: h2c
Connection: Upgrade, close
Vary: Accept-Encoding
Transfer-Encoding: chunked
Content-Type: text/html

a

LedOne: 1
0

The only information that matters to me which I want to save in a string is: LedOne:1 But how? >:(  I tried several things but nothing seems to be working.

This might help here I print out the string length (Serial.print(line.length()):
Code: [Select]

HTTP/1.1 200 OK15
Date: Thu, 17 Aug 2017 12:56:00 GMT36
Server: Apache15
Upgrade: h2c13
Connection: Upgrade, close27
Vary: Accept-Encoding22
Transfer-Encoding: chunked27
Content-Type: text/html24
1
a2

LedOne: 111
02
1
1


Any help would be appreciated.

PaulS

If what you want to process ALWAYS starts with "LedOne:", then indexOf() looks useful, since you seem to think that you need crutches.

Then, substring() and parseInt() might be useful.
The art of getting good answers lies in asking good questions.

Kassie

Thanks for your reply,

But as you can see every line is another string or something, because he prints the amount of characters in every line(see last code segment). Maybe you can tell me how to save all these 14 lines into 1 string so I am able to use the function Indexof as you suggested, because now indexof scans all 14 lines sepperatly.

PaulS

Quote
But as you can see every line is another string or something
As I can see, you posted a snippet of code that does NOT prove that.

Does it matter? The String either contains "LedOff:" or it doesn't. If it doesn't, do nothing special. If it does, get the data after the ':' and convert it to an int, if that's what you need to do.
The art of getting good answers lies in asking good questions.

Kassie

Thanks for the fast reply,

As you suggested I tried indexof() see code here:

Code: [Select]

while (client.available()) {
    String line = client.readStringUntil('\r');
    Serial.print(line); 
    int indexofLedOne = line.indexOf('LedOne');
    Serial.print(indexofLedOne);
  }


This is what my serial monitor returns:

Code: [Select]

HTTP/1.1 200 OK-1
Date: Thu, 17 Aug 2017 14:39:57 GMT4
Server: Apache2
Upgrade: h2c7
Connection: Upgrade, close5
Vary: Accept-Encoding10
Transfer-Encoding: chunked7
Content-Type: text/html5
-1
a-1

LedOne: 13
0-1
-1
-1


I am not able to find the logic behind these numbers can you help me understand this, or am I doing something wrong?

PaulS

Quote
am I doing something wrong?
Single quotes are for single characters. Can you post a picture of your keyboard with the LedOne key circled?

The art of getting good answers lies in asking good questions.

Kassie

Thanks for pointing out that mistake!

Im not totally sure what you mean with a picture of my keyboard. But assuming you mean the thing i'm typing on, thats not what im using as input the LedOne: comes from my website and the number :1 can change to other numbers.

The code i'm using right now:

Code: [Select]

while (client.available()) {
   String line = client.readStringUntil('\r');
   Serial.print(line);  
   int indexofLedOne = line.indexOf("LedOne");
   if (indexofLedOne == 2 ){
     Serial.println();
     Serial.print("the index: ");
     Serial.println(indexofLedOne);
     Serial.print("Its working!");
   }
 }


My serial monitor:
Code: [Select]

HTTP/1.1 200 OK
Date: Thu, 17 Aug 2017 15:21:24 GMT
Server: Apache
Upgrade: h2c
Connection: Upgrade, close
Vary: Accept-Encoding
Transfer-Encoding: chunked
Content-Type: text/html

a

LedOne: 1
the index: 2
Its working!
0


The only thing left is, how can I now read out the : 1 , : 0 or : (other digit) and save this into a string outside of this while loop.

Thanks for all the help so far!


PaulS

Quote
Im not totally sure what you mean with a picture of my keyboard. But assuming you mean the thing i'm typing on, thats not what im using as input the LedOne: comes from my website and the number :1 can change to other numbers.
The code that the Arduino is running came out your fingertips to your keyboard and into the IDE...

Code: [Select]
   if (indexofLedOne == 2 ){
Any non-negative number means that the String contains "LedOne".

Quote
The only thing left is, how can I now read out the : 1 , : 0 or : (other digit) and save this into a string outside of this while loop.
So, if you know where LedOne occurs in the String, and you know how long "LedOne" is, that means you know where it ends, right? If you know where it ends, or you know where the : is, that means that you know where the value begins, right?

Knowing where the value begins means that you can create a substring containing only the value, and you can call the substring's parseInt() method.
The art of getting good answers lies in asking good questions.

Kassie

Everthing is working right now big thanks to you Paul!

Code: [Select]

while (client.available()) {
    String line = client.readStringUntil('\r');
    Serial.print(line); 
    int indexofLedOne = line.lastIndexOf("LedOne: ");
      if (indexofLedOne >= 0 ){
       Serial.println();
       Serial.print("the index: ");
       Serial.println(indexofLedOne);
          if (line.substring(8, 11) == ": 0") {
             Serial.println("Relay/ Led will be disabled");
             //some code to enable the LED/Relay
             digitalWrite(2, LOW);
          }
          else if (line.substring(8, 11) == ": 1") {
             Serial.println("Relay/ Led will be enabled");
             //some code to enable the LED/Relay
             digitalWrite(2, HIGH);
          }
           else if (line.substring(8, 11) == ": 2") {
             Serial.println("Relay/ Led will be blinking");
             //some code to blink the LED/Relay
           }
          else{
            Serial.println ("error the data received is not equal to : 0, : 1 or : 2");
            digitalWrite(2, LOW);
          }
      }
  }


There is only one little question left, how can I let this led blink (maybe even using pwm) whilst checking de LedOne value. Because the loop i created stops me from checking the value.

PaulS

Quote
There is only one little question left, how can I let this led blink (maybe even using pwm) whilst checking de LedOne value. Because the loop i created stops me from checking the value.
You need to separate using the data you get from the client from getting data from the client.

Create a function to get data from the client, if there is a client. Have loop() call that function instead of having the code in loop().

Create a function to use the data from the client. Have loop() call that function instead of having the code in loop().

Then, it will be very easy to keep the functionality separate.
The art of getting good answers lies in asking good questions.

Go Up