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
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
I receive this:
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()):
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.
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.
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
int indexofLedOne = line.indexOf('LedOne');
Serial.print(indexofLedOne);
}
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:
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:
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.
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...
if (indexofLedOne == 2 ){
Any non-negative number means that the String contains "LedOne".
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.
Everthing is working right now big thanks to you Paul!
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.
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.