I'm New to arduino so this might seem like a stupid question for you, but how the hell can I insert a value on an HTML page and write it in an int variable?
When connecting to ESP8266, the web page works fine and the input box appears.
As you can see i've a "Salva" Button.
How do I store the values I place on the web page about variables int Hou Hou 1 etc. inside an INT?
You have to create a HTML form (on your page) not just input. read about it here
I use ESPwebserver.h to handle url requests.
The easiest is to let the forma action direct you back towards the same page, and then you can read the form arguments using the .hasArg() and arg() functions. Since the .arg() function returns a String you will have to convert the input to an int using either using .toInt() or with your own function (.toInt() returns 0 for incorrect input, if 0 can be a correct input and if you want incorrect input to be ignored your own function is better)
Deva_Rishi:
You have to create a HTML form (on your page) not just input. read about it here
I use ESPwebserver.h to handle url requests.
The easiest is to let the forma action direct you back towards the same page, and then you can read the form arguments using the .hasArg() and arg() functions. Since the .arg() function returns a String you will have to convert the input to an int using either using .toInt() or with your own function (.toInt() returns 0 for incorrect input, if 0 can be a correct input and if you want incorrect input to be ignored your own function is better)
Thank you! it was exactly what I was looking for!
But now I have another problem, when I go to use the .hasArg() function it returns me the error:
'class WiFiClient' has no member named 'hasArg'
Actually, client is my WiFiClient: "WiFiClient client = server.available();"
Could it be a problem with libraries? I have updated them with the latest available for the ESP8266, the following libraries are loaded in the program:
#include <ESP8266WebServer.h> #include <ESP8266WebServerSecure.h> #include <stdlib.h> used for string conversion #include <WiFiUdp.h> #include <WiFiClient.h> #include <NTPClient.h> For the NTP Time #include <Servo.h> #include <DFPlayer.h> DFPlayer mini
The issue is not with the libraries, but with the object you have to create, you are not creating a client(which connects to a page) but a server. check out the HelloServer and AdvancedWebServer examples in Examples\ESP8266WebServer
Deva_Rishi:
The issue is not with the libraries, but with the object you have to create, you are not creating a client(which connects to a page) but a server. check out the HelloServer and AdvancedWebServer examples in Examples\ESP8266WebServer
Thanks again! Seems like it's working, it compiles!
I've also converted strings to INT correctly
But i still can't save the numbers entered on the web page in the strings
From what I see, I have to use the GET function based on the HTTP header but I can not understand how it works...
using the post or the get on an esp doesn't really matter much (just that the arguments will show up in your URL or not) the issue must be in your 'handle' functions that you refer to by
server.on(/Salvaorari, handleFunction);
within setup()
which will call the function
void handleFunction() { }
where you can use the server.hasArg() and server.arg() to extract the arguments and their data.
if you put
ESP8266WebServer WBServer(80);
int Ora;
int Min;
int Sec;
int Ora1;
int Min1;
int Sec1;
int Ora2;
int Min2;
int Sec2;
String Orastring;
String Minstring;
String Secstring;
String Orastring1;
String Minstring1;
String Secstring1;
String Orastring2;
String Minstring2;
String Secstring2;
Well every time you submit a form you do read the values but do not respond by sending a webpage that contains these values. Does that make sense ? My Italian is not so good, so the comments in your code are helping me less that way.
i usually do what you have in memore() and let that follow by putting the responding webpage in a String
String s;
s+="the web page": // etc
and then do WBServer.send(200, "text/html", s);Now if you submit the form and the values are read from the arguments, you can show them to the user straight away afterwards
Oh and your loop() i think it is missing the WBServer.handleClient(); like this your memore() never gets called !!WBServer.on("/Gestione/salva", memore); in setup() requires .handleClient() within loop() yes like this it doesn't work. So in recap i don't use the whole client.println() thing, just the ESPwebServer. You use both (i've done that once too, it doesn't work) Check out the AdvancedWebServer example under ESPwebserver. Sorry i have no more time today.
So today i had a bit more time, and took it.. I awarded you a karma point for writing proper HTML-code (which is also the reason i went through the trouble of showing this a bit more clear. So as you suspected there was something structurally wrong and i came to this using a lot of your code
#include <ESP8266WebServer.h>
#include <ESP8266WebServerSecure.h>
#include <SoftwareSerial.h>
#include <stdlib.h>
#include <WiFiUdp.h>
#include <WiFiClient.h>
//#include <NTPClient.h>
#include <Servo.h>
//#include <DFPlayer.h>
const char* ssid = "SSID"; //SSID WIFI
const char* password = "PASSWORD"; //PASSWORD WIFI
ESP8266WebServer WBServer(80);
WiFiUDP ntpUDP;
//NTPClient NTPTime (ntpUDP, "ntp1.inrim.it", 3600); //SERVER NTP E GMT (3600 +1)
Servo myservo;
const int buttonPIN = D5;
const int piezoPIN = D6;
const int trigPIN = D4;
const int echoPIN = D3;
long durata;
int distanza;
SoftwareSerial mp3Serial(D1, D2);
#define PIN_BUSY D10
// WiFiServer server(80); //PORTA SITO WEB
String header; //HEADER SITO WEB
String Door6A1;
String Door6A2;
String Door6B1;
String Door6B2;
String Door7B1;
String Door7B2;
String Door7A1;
int Ora;
int Min;
int Sec;
int Ora1;
int Min1;
int Sec1;
int Ora2;
int Min2;
int Sec2;
void setup()
{
myservo.write(50);
Serial.begin(9600);
delay(250);
mp3Serial.begin (9600);
delay(250);
myservo.attach(13);
pinMode (buttonPIN, INPUT_PULLUP);
pinMode (piezoPIN, OUTPUT);
pinMode (trigPIN, OUTPUT);
pinMode (echoPIN, INPUT);
//mp3_set_serial (mp3Serial);
delay(1500);
//mp3_set_volume (28);
delay(2000);
WiFi.begin(ssid, password);
delay(2000);
WBServer.on("/6A1/chiudi",D6A1);
WBServer.on("/Gestione/salva", Memore);
WBServer.on("/Gestione", Gestione);
WBServer.on("/",handleRoot);
WBServer.begin();
}
void loop() {
WBServer.handleClient();
int buttonState = digitalRead(buttonPIN); //LEGGI STATO BOTTONE
if (buttonState == LOW) {
//azionebottone();
}
else {
//NTPTime.update(); //AGGIORNA ORA DA NTP
//oraprogrammata(); //LOOP ORARIO PROGRAMMATO
}
}
void D6A1() {
}
void handleRoot() {
String s = "";
s += "<!DOCTYPE html><html>";
s += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">";
s += "<link rel=\"icon\" href=\"data:,\">";
s += "<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}";
s += ".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;";
s += "text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}";
s += ".stato { border: none; color: #195B6A; padding: 16px 40px;";
s += "text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}";
s += "</style></head>";
s += "<body><h1>Althea Italia S.p.A.
Controllo remoto chiusura porte
</h1>";
s += "<p><a href=\"/6A1/chiudi\"><button class=\"button\">CHIUDI PORTA 6A1</button></a><stato class=\"stato\">" + Door6A1 + "</p>";
s += "<p><a href=\"/6A2/chiudi\"><button class=\"button\">CHIUDI PORTA 6A2</button></a><stato class=\"stato\">" + Door6A2 + "</p>";
s += "<p><a href=\"/6B1/chiudi\"><button class=\"button\">CHIUDI PORTA 6B1</button></a><stato class=\"stato\">" + Door6B1 + "</p>";
s += "<p><a href=\"/6B2/chiudi\"><button class=\"button\">CHIUDI PORTA 6B2</button></a><stato class=\"stato\">" + Door6B2 + "</p>";
s += "<p><a href=\"/7B1/chiudi\"><button class=\"button\">CHIUDI PORTA 7B1</button></a><stato class=\"stato\">" + Door7B1 + "</p>";
s += "<p><a href=\"/7B2/chiudi\"><button class=\"button\">CHIUDI PORTA 7B2</button></a><stato class=\"stato\">" + Door7B2 + "</p>";
s += "<p><a href=\"/7A1/chiudi\"><button class=\"button\">CHIUDI PORTA 7A1</button></a><stato class=\"stato\">" + Door7A1 + "</p>";
s += "<p><a href=\"/All/chiudi\"><button class=\"button\">CHIUDI TUTTE</button></a></p>";
s += "<p><a href=\"/Gestione\"><button class=\"button\">Gestione Orario</button></a></p>";
WBServer.send(200, "text/html", s);
}
This compiles for me, but that meant i had to get rid of some stuff like the NTP thing and mp3 thing, but you should manage to put that back in. Also i removed the responses to all the individual 'door-buttons' but left you 1 example. I hope the structure of how to do what you want is now clear enough, basically you setup callback functions to web-page addresses, and if these contain arguments you can extract their values. Those String variables should probably actually be local to function that uses them or you may get some fragmentation issue at some point. (in my experience it is fine to use the String class on an ESP(it has a lot of memory), as long as you keep the variables either 'local' or reserve space for them.
Actually the error was pretty stupid!
I made confusion with the WiFiServer server(80); in my void setup declaration and function.
client.println wasn't about WBServer
Now, i've to make two NodeMCU under the same wifi talk to eachother... but I think i'm gonna open another topic for this. Can I count on your help?