We are fairly new to Arduino. We tried to make a HTML form where user can input values and this value can be used by the ESP8266 to switch on the LED after the seconds input by the user. We managed to make the form, but the LED would not switch on after the given time. Here is our code. Can someone please help us figure out the mistakes we are making? Thank you!
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
MDNSResponder mdns;
// Replace with your network credentials
const char* ssid = "xxxxxxx";
const char* password = "xxxxxxx";
ESP8266WebServer server(80);
String webPage = "";
int gpio5_pin = 5;
void setup(void){
webPage += "HTTP/1.1 200 OK";
webPage += "Content-Type: text/html";
webPage += "";
webPage += "<HTML>";
webPage += "<HEAD>";
webPage += "<TITLE>Arduino GET test page</TITLE>";
webPage += "</HEAD>";
webPage += "<BODY>";
webPage += "<H1>LED</H1>";
webPage += "<FORM ACTION='/' method=get >"; //uses IP/port of web page
webPage += "Start at: <INPUT TYPE=TEXT NAME='LED' VALUE='' SIZE='25' MAXLENGTH='50'>
";
webPage += "<a href=\"submit\"><INPUT TYPE=SUBMIT NAME='submit' VALUE='Start'>";
webPage += "</FORM>";
webPage += "<BODY/>";
webPage += "</HTML>";
// preparing GPIOs
pinMode(gpio5_pin, OUTPUT);
digitalWrite(gpio5_pin, LOW);
delay(1000);
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (mdns.begin("esp8266", WiFi.localIP())) {
Serial.println("MDNS responder started");
}
server.on("/", [](){
server.send(200, "text/html", webPage);
});
server.on("/submit", [](){
server.send(200, "text/html", webPage);
String readString = "";
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the String readString
delay(2); //slow looping to allow buffer to fill with next character
}
if(readString.length()>0){
Serial.println(readString); //prints string to serial port out
int pos1 = readString.indexOf('=');
int pos2 = readString.indexOf('&');
String newString;
newString = readString.substring(pos1+1, pos2);
Serial.print("newString is: ");
Serial.println(newString);
int val;
val = newString.toInt();
delay(val*1000);
digitalWrite(gpio5_pin, HIGH);
//delay(1000);
Serial.print("The value sent is: ");
Serial.println(val);
readString=""; //clears variable for new input
newString = ""; //clears variable for new input
}
});
server.begin();
Serial.println("HTTP server started");
}
void loop(void){
server.handleClient();
}
Hi, the LED never switches on or off. All the "Serial.println" functions don't get executed in this part of the code, either:
server.on("/submit", [](){
server.send(200, "text/html", webPage);
String readString = "";
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the String readString
delay(2); //slow looping to allow buffer to fill with next character
}
if(readString.length()>0){
Serial.println(readString); //prints string to serial port out
int pos1 = readString.indexOf('=');
int pos2 = readString.indexOf('&');
String newString;
newString = readString.substring(pos1+1, pos2);
Serial.print("newString is: ");
Serial.println(newString);
int val;
val = newString.toInt();
delay(val*1000);
digitalWrite(gpio5_pin, HIGH);
//delay(1000);
Serial.print("The value sent is: ");
Serial.println(val);
readString=""; //clears variable for new input
newString = ""; //clears variable for new input
}
});
If I had your hardware, I'd add some Serial.print() statements in the two server.on lambdas, to assure that the correct one gets called.
In the one that does something useful, I'd print the value that server.uri() returns, to see if it contained anything useful. I'd call server.args() to see how many arguments there are, and call server.arg(n) to get each one (setting n to the appropriate value, of course).
If I had your hardware, I'd add some Serial.print() statements in the two server.on lambdas, to assure that the correct one gets called. In the one that does something useful, I'd print the value that server.uri() returns, to see if it contained anything useful.
We added the Serial. print() statements and it is only working on the first server.on lambda (which is not the correct one). We printed the value of server.uri(). It only prints "/". What should we do? Thank you!
Look at the AdvancedWebServer sketch in the examples folder of that library. Observe how the handleNotFount() function gets number of arguments, argument names, and argument values using methods of the server object.
RoyBlue:
We added the Serial. print() statements and it is only working on the first server.on lambda (which is not the correct one). We printed the value of server.uri(). It only prints "/". What should we do? Thank you!
Yes, we changed the ACTION value and it does go to the second server.on lambda. We tried out a print statement on the lambda and it worked. However, the logic and the code for LED switching on after a given time does not work. Here is that code:
server.send(200, "text/html", webPage);
String readString = "";
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the String readString
delay(2); //slow looping to allow buffer to fill with next character
}
if(readString.length()>0){
Serial.println(readString); //prints string to serial port out
int pos1 = readString.indexOf('=');
int pos2 = readString.indexOf('&');
String newString;
newString = readString.substring(pos1+1, pos2);
Serial.print("newString is: ");
Serial.println(newString);
int val;
val = newString.toInt();
delay(val*1000);
digitalWrite(gpio5_pin, HIGH);
//delay(1000);
Serial.print("The value sent is: ");
Serial.println(val);
readString=""; //clears variable for new input
newString = ""; //clears variable for new input
}
As mentioned before, it might be because the Serial.read is not synonymous with server. What can we do? Thanks!
No problem, thank you. We figured out the problem. It was indeed the problem that Serial.read cannot be used to server. We used server.arg(name) and solved it.