NodeMCU and HTTP variables
Posted: Wed Oct 03, 2018 3:49 pm
Hello everyone,
I have a problem trying to display some variables like temperatures (float temp; & float settemp;) on my HTML webserver. I'm using a DHT22 sensor which reads the temperature and I want to display it on my HTML webpage.
I tried to use <h1>temp</h1> but I keep getting the actual text instead of the number stored in the variable called temp.
Below is my code, can someone give me a clue what am I doing wrong and how to properly do it?
I have a problem trying to display some variables like temperatures (float temp; & float settemp;) on my HTML webserver. I'm using a DHT22 sensor which reads the temperature and I want to display it on my HTML webpage.
I tried to use <h1>temp</h1> but I keep getting the actual text instead of the number stored in the variable called temp.
Below is my code, can someone give me a clue what am I doing wrong and how to properly do it?
Code: Select all
static const char PROGMEM INDEX_HTML[] = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<title>Set Temperature</title>
</head>
<body>
Thermostat <a href="ON">ON</a><br>
Thermostat <a href="OFF">OFF</a><br>
Thermostat <a href="reset">RESET</a><br>
<form method="post" action="/save" >
<br><br>
Temperature Setpoint:<br><input name="settemp" type="text" size="16" value="" ><br><br>
<input type="submit" name="Temperature change" value="accept">
</form>
</body>
</html>
)rawliteral";
void handleRoot() {
webserver.send(200, "text/html", INDEX_HTML);
}
void handleNotFound() {
webserver.send(404, "text/plain", "Page not found ...");
}
void handleSave() {
String str = "Settings Saved ...\r\n";
Serial.print("number of arguments ");
Serial.println(webserver.args()); // number of arguments
if (webserver.args() > 0 ) {
for ( uint8_t i = 0; i < webserver.args(); i++ ) {
str += webserver.argName(i) + " = " + webserver.arg(i) + "\r\n";
Serial.println("Arg "+ String(i)+"="+ webserver.argName(i));
Serial.println("Arg "+ String(i)+"="+ webserver.arg(i));
}
String inString = "";
inString = (webserver.arg(0));
settemp = inString.toFloat();
Serial.print("settemp ");
Serial.println(settemp);
Serial.println();
}
webserver.send(200, "text/plain", str.c_str());
}
void handleON() {
Serial.println("Thermostat ENABLE");
master=1;
webserver.send(200, "text/html", "Thermostat ENABLE"); //Send ADC value only to client ajax request
}
void handleOFF() {
Serial.println("Thermostat DISABLE");
master=0;
webserver.send(200, "text/html", "Thermostat DISABLE"); //Send ADC value only to client ajax request
}
void handlereset() {
Serial.println("Thermostat RESET");
master=2;
webserver.send(200, "text/html", "Thermostat RESET"); //Send ADC value only to client ajax request
}
void setup() {
Serial.begin(115200);
pinMode(Relay, OUTPUT);
digitalWrite(Relay, HIGH);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
WiFi.mode(WIFI_AP_STA);
setupAccessPoint();
webserver.on("/", handleRoot);
webserver.on("/save", handleSave);
webserver.onNotFound(handleNotFound);
webserver.on("/ON", handleON); //as Per <a href="ledOn">, Subroutine to be called
webserver.on("/OFF", handleOFF);
webserver.on("/reset", handlereset);
webserver.begin();
Serial.println("Web server has started");
Serial.println("----------------------");
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}