Hello,
I have the code sort of working right up to the last part.
I have an ESP8266 ESP12F that runs as a station? / Server I can connect to it using another ESP 12F module.
The Server side is set so a request to a page request of /status comes back with a text string based on the state of a GPIO port on it. so if the port is HIGH the HTTP response I send back is "HIGH". well it sort of works, it comes back with all the HTTP Header stuff then the "HIGH" text, and I'm having problems getting the CLIENT side to filter out all the HTTP Header stuff. I have tried to write some code to wait until the "\r\n\r\n" seqeunce. I read is "the separator" between the header and the actuall data of the request but I can't get it to remove the HTTP junk and assign the text I want to a String . it just fails every time.
Are there some easy ways to remove it with a simple command?
I've tried using the readstringuntil command and also the .startswith
command but as far as I can tell that only does one char and not a sequence of chars like '\r\n\r\n' so perhaps its useless. I've spent days on this just slowly trying different things. I'm very new to this C language I've spent most my life doing assembler and this has so many possibilities but just throws up these walls that hold me back. it's so annoying. Help me please! I will post the code when I can figure out how to. Nope i've tried the buttons above the window and non suggest code posting window options so i guess i need help with that as well.
In your editor, mark all the code and copy it to the clipboard. In a post here, click on the "</>" button. Three lines will appear. The middle line is highighted and says "Type or paste code here". Simply press control/v and paste the code in.
HTML really is for sending complex structures such as an entire web page. There are other technologies for exchanging single variables, but for now, we should probably just help you extract the string you want.
Hello, Thanks for the Code insertion Instructions!
I have pasted all the code. It's in a bad way. There are parts where I've tried the readstringuntil and startswith instructions and they are still in there so should be one or the other.
Simply the program Monitors a GPIO Pin for a momentary switch, upon closing the switch it pulls the port low.
This then sends the /toggle page request to the server (the server side then pulses one of its IO pins to switch the inverter on or off.
if the button is not pressed it continually sents the /status page request to the server. upon server receiving /status page request it reads another IO port on connected to the Inverters POWER Led output and sends back with either "HIGH" or "LOW" depending on inverters on/off state. I then want to read this received HIGH or LOW text and convert to a String so I can compare that to a string to show the inverters state on the switch panel.
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
const char* ssid = "INV";
const char* password = "aabbccddeeff";
const char* host = "10.10.10.1";
int value = 1;
String stringLow, stringHigh;//These just here for testing the readstringuntil and startswith commands.
String stringone;
String line;
String result;
void setup() {
stringLow = String("LOW");
stringHigh = String("HIGH");
stringone = String("testing is good\r\n\r\n");
line = String("TEST");
Serial.begin(115200);
pinMode(2, OUTPUT); //Set GPIO2 ESP Blue Led output. STATUS
pinMode(4, INPUT); //Set GPIO4 4th Down RHS to Input. On Off Button
pinMode(12, OUTPUT); //Set GPIO12 LHS Remote inverter Power Status On or Off?
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
digitalWrite(2, digitalRead(2)^1); //flash ESP Blue LED.
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
digitalWrite(2, HIGH); //switch ESP BLUE LED OFF.
}//end setup
void loop() {
delay(1000);
Serial.print("Connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient Client;
HTTPClient sender;
const int httpPort = 80;
if (!Client.connect(host, httpPort)) {
Serial.println("Connection failed");
return;
}
if (digitalRead(4) == HIGH); //If button is not pressed send status request to server
{
// sender.begin(Client, "http://10.10.10.1/status");
// Serial.print("10.10.10.1/status Switch not pressed");
// int httpCode = sender.GET();
// sender.end();
// the following command used mostly Strings to hold the data originally but it failed to work.
// So converted to pure text and it worked.
Client.print(String("GET /status &headers=falseHTTP/1.1\r\nHost: 10.10.10.1\r\nConnection: close\r\n\r\n"));
delay(500);
// Read all the lines of the reply from server and print them to Serial
// This part is a bit of a mess I was adding parts to try and test it and ended up in this mess.
//
while (Client.available()) {
String line = Client.readStringUntil('\r');
char c = Client.read();
result = result + c;
// Serial.println("First = ");
// Serial.println(result);
// Serial.println(line);
result="";
// Added this part trying the startswith command to find the http header seperator sequence '\r\n\r\n' instead of the above readstringuntil.
if (line.startsWith("\r\n\r\n", 0)) ;// find line that starts with Header seperator before the Text you want.
{
Serial.println("Seperator found");
// so continue to read data from HTTP. and according to forums it should be your data but its NOT.
while (Client.available()) {
//Continue to read the data after the HTTP header serperator. until the HTTP Data ends.
String line = Client.readStringUntil('\r');
char c = Client.read();
result = result + c;
}
}
}
if (line == "LOW") {
digitalWrite(12, LOW);
Serial.println("Low from Server");
}
if (line == "HIGH") {
digitalWrite(12, HIGH);
Serial.println("High from Server");
}
//If button is not pressed then send the /toggle page request to the server to Pulse the power input pin to switch the Inverter on.
if (digitalRead(4) == LOW) {
//send toggle request to SERVER to toggle POWER SWITCH.
Client.print(String("GET /toggle &headers=falseHTTP/1.1\r\nHost: 10.10.10.1\r\nConnection: close\r\n\r\n"));
delay(500);
delay(1000);
}
}
// Read all the lines of the reply from server and print them to Serial
while(Client.available()){
String line = Client.readStringUntil('\r');
Serial.println("Line Toggle ");
Serial.println(line);
}
Serial.println("");
Serial.println(String("Try nr. ") + value + " is finished.");
Serial.println("Waiting for next try...");
Serial.println("");
value = value + 1;
delay(500);
}
You are right, the code is a mess and doesn't make much sense in its current form. Maybe we should just start that part from scratch. I would think the simplest way to read until header end is to continue reading lines (until \n instead of \r) and then check the length of the String read. The end of the header should have a length of two (I'm guessing), being the \r and the \n.
Once that is done, read another line. If there is something preceding the HIGH or LOW you want, that too will have to be skipped over. I can't guess how to best do that because I would need to see the data line itself. It may be as easy as skipping x number of characters or skipping until H or L comes along.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.