I'm trying to get an Arduino Uno that is set up as a web server via an ESP8266 module to receive an argument from a web page request by simply parsing the info from a url such as http://10.0.0.129/?argument.
The sketch I used was modified from an Ethernet one doing the same task. I had to use software serial with the hardware and WifIEsp provided suitable compatibility.
The webserver works fine to present a web page but the HTTP Request info returned is a bit random and usually doesn't contain the url info.
Any Clues?
#include <SoftwareSerial.h>
#include <WiFiEsp.h>
#include <WiFiEspClient.h>
#include <WiFiEspServer.h>
#include <WiFiEspUdp.h>
IPAddress ip(10, 0, 0, 129);
SoftwareSerial Serial1(2, 3); // RX, TX
WiFiEspServer Server(80); // create a server at port 80
String HTTP_req; // stores the HTTP request
void setup()
{
Serial1.begin(9600);
WiFi.init(&Serial1);
WiFi.begin("user","pass");
Server.begin(); // start to listen for clients
Serial.begin(9600); // for diagnostics
HTTP_req="";
}
void loop()
{
WiFiEspClient Client = Server.available();
if (Client) {
bool currentLineIsBlank = true;
bool savechar = false;
while (Client.connected()) {
if (Client.available()) {
char c = Client.read();
HTTP_req += c;
if (c == '\n' && currentLineIsBlank) {
Serial.print("HTTP REQ = "); Serial.println(HTTP_req); //Serial Monitor Debug
Client.println("");
Client.println("");
Client.println("");
Client.println("Arduino Web Page");
Client.println("
Parameter Parse Test
");Client.println("
");
Client.println("
");
Client.print("Argument="); // Display Argument Detected
if (HTTP_req.indexOf("?")<=0){
Client.println("None");
}
else {
Client.println(HTTP_req.substring(HTTP_req.indexOf("?")+1));
}
Client.println("");
Client.println("");
HTTP_req="";
break;
}
if (c == '\n') currentLineIsBlank = true; else if (c != '\r') currentLineIsBlank = false;
} // end if (client.available())
} // end while (client.connected())
delay(1);
Client.stop();
} // end if (client)
}
parseparam1.ino (2.22 KB)