-->
Page 1 of 1

NodeMCU and HTTP variables

PostPosted: Wed Oct 03, 2018 3:49 pm
by V3N0MX
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?


Code: Select allstatic 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());

}


Re: NodeMCU and HTTP variables

PostPosted: Thu Oct 04, 2018 12:07 pm
by rudy
I had tried the code posted and it didn't work. After a while trying to get it to compile I gave up since the problems were not what you were asking about.

If you want help then make it easier for others to help you.

Re: NodeMCU and HTTP variables

PostPosted: Fri Oct 05, 2018 4:11 am
by V3N0MX
@rudy,

Sorry I posted just a part of the bigger code because I wanted to focus on the problem, not on the whole program.

I think by searching more I answered my question with this topic: viewtopic.php?f=45&t=10201&p=48303&hilit=html+variables#p48303