I'm totaly new to Arduino, I'm using ESP2866 NodeMCU and dht22 to display temperature and humidity in a web page, it is autorefresh (using AJAX) and it works perfectly, I wanted to add 2 buttons to it so I can control 2 Leds but when I tried that I couldn't control them and even the values of temperature and humidity stopped refreshing. Can you tell me what is wrong in the code ? here it is :

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include "DHT.h"
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

 /*Put your SSID & Password*/
 const char* ssid = "xxx";  // Enter SSID here
 const char* password = "xxx";  //Enter Password here

  ESP8266WebServer server(80);

 // DHT Sensor
 uint8_t DHTPin = D6; 

  // Initialize DHT sensor.
  DHT dht(DHTPin, DHTTYPE);                

  float Temperature;
  float Humidity;
  float reading;
  float percentage;
  // Auxiliar variables to store the current output state
  String output5State = "off";
  String output4State = "off";

 // Assign output variables to GPIO pins
 const int output5 = 5;
 const int output4 = 4;
 void setup() {

 pinMode(DHTPin, INPUT);

// Initialize the output variables as outputs
  pinMode(output5, OUTPUT);
 pinMode(output4, OUTPUT);
 // Set outputs to LOW
 digitalWrite(output5, LOW);
 digitalWrite(output4, LOW);
 Serial.println("Connecting to ");

//connect to your local wi-fi network
WiFi.begin(ssid, password);

//check wi-fi is connected to wi-fi network
while (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi connected..!");
Serial.print("Got IP: ");  Serial.println(WiFi.localIP());

server.on("/", handle_OnConnect);

Serial.println("HTTP server started");

void loop() {


void handle_OnConnect() {

Temperature = dht.readTemperature(); // Gets the values of the temperature
Humidity = dht.readHumidity(); // Gets the values of the humidity 

String header=SendHTML( Temperature,Humidity,percentage);

server.send(200, "text/html",header); 
// turns the GPIOs on and off
        if (header.indexOf("GET /5/on") >= 0) {
          Serial.println("GPIO 5 on");
          output5State = "on";
          digitalWrite(output5, HIGH);
        } else if (header.indexOf("GET /5/off") >= 0) {
          Serial.println("GPIO 5 off");
          output5State = "off";
          digitalWrite(output5, LOW);
        } else if (header.indexOf("GET /4/on") >= 0) {
          Serial.println("GPIO 4 on");
          output4State = "on";
          digitalWrite(output4, HIGH);
        } else if (header.indexOf("GET /4/off") >= 0) {
          Serial.println("GPIO 4 off");
          output4State = "off";
          digitalWrite(output4, LOW);


   void handle_NotFound(){
   server.send(404, "text/plain", "Not found");

   String SendHTML(float TempCstat,float Humiditystat, float  percentage){
    String ptr = "<!DOCTYPE html> <html>\n";
    ptr +="<head><meta name=\"viewport\" content=\"width=device-width, 
    initial-scale=1.0, user-scalable=no\">\n";
    // ptr +="<meta http-equiv=\"refresh\" content=\"2\" >\n";
   ptr +="<link href=\"https://fonts.googleapis.com/css? 
    family=Open+Sans:300,400,600\" rel=\"stylesheet\">\n";
    ptr +="<title>ESP8266 Weather Report</title>\n";
    ptr +="<style>html { font-family: 'Open Sans', sans-serif; display: 
    block; margin: 0px auto; text-align: center;color: #333333;}\n";
    ptr +="body{margin-top: 50px;}\n";
    ptr +="h1 {margin: 50px auto 30px;}\n";
    ptr +=".side-by-side{display: inline-block;vertical-align: 
     middle;position: relative;}\n";
    ptr +=".humidity-icon{background-color: #3498db;width: 30px;height: 
    30px;border-radius: 50%;line-height: 36px;}\n";
     ptr +=".humidity-text{font-weight: 600;padding-left: 15px;font-size: 
     19px;width: 160px;text-align: left;}\n";
     ptr +=".humidity{font-weight: 300;font-size: 60px;color: #3498db;}\n";
     ptr +=".temperature-icon{background-color: #f39c12;width: 30px;height: 
     30px;border-radius: 50%;line-height: 40px;}\n";
     ptr +=".temperature-text{font-weight: 600;padding-left: 15px;font-size: 
     19px;width: 160px;text-align: left;}\n";
     ptr +=".temperature{font-weight: 300;font-size: 60px;color: 
     ptr +=".superscript{font-size: 17px;font-weight: 600;position: 
     absolute;right: -20px;top: 15px;}\n";
     ptr +=".data{padding: 10px;}\n";
     ptr +="</style>\n";
     ptr +="<script>\n";
     ptr +="setInterval(loadDoc,200);\n";
     ptr +="function loadDoc() {\n";
     ptr +="var xhttp = new XMLHttpRequest();\n";
     ptr +="xhttp.onreadystatechange = function() {\n";
     ptr +="if (this.readyState == 4 && this.status == 200) {\n";
     ptr +="document.getElementById(\"webpage\").innerHTML 
     ptr +="};\n";
     ptr +="xhttp.open(\"GET\", \"/\", true);\n";
     ptr +="xhttp.send();\n";
     ptr +="}\n";
     ptr +="</script>\n";

     ptr +="</head>\n";
     ptr +="<body>\n";

     ptr +="<div id=\"webpage\">\n";

     ptr +="<h1>ESP8266 Weather Report</h1>\n";
     ptr +="<div class=\"data\">\n";
     ptr +="<div class=\"side-by-side temperature-icon\">\n";
     ptr +="<svg version=\"1.1\" id=\"Layer_1\" 
     xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\" y=\"0px\"\n";
     ptr +="width=\"9.915px\" height=\"22px\" viewBox=\"0 0 9.915 22\" 
     enable-background=\"new 0 0 9.915 22\" xml:space=\"preserve\">\n";
      ptr +="<path fill=\"#FFFFFF\" d=\"M3.498,0.53c0.377-0.331,0.877- 
      ptr +="c0.491,1.163,0.518,2.514,0.062,3.693c-0.414,1.102-1.24,2.038- 
      ptr +="c-1.417-0.323-2.659-1.314-3.3-2.617C0.014,18.26- 
      ptr +="c0.013-3.44,0-6.881,0.007- 
      ptr +="</svg>\n";
      ptr +="</div>\n";
      ptr +="<div class=\"side-by-side temperature- 
      ptr +="<div class=\"side-by-side temperature\">";
      ptr +=(int)TempCstat;
      ptr +="<span class=\"superscript\">°C</span></div>\n";
      ptr +="</div>\n";  
      ptr +="<div class=\"data\">\n";
      ptr +="<div class=\"side-by-side humidity-icon\">\n";
      ptr +="<svg version=\"1.1\" id=\"Layer_2\" 
      xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"0px\" y=\"0px\"\n\"; 
      width=\"12px\" height=\"17.955px\" viewBox=\"0 0 13 17.955\" enable- 
      background=\"new 0 0 13 17.955\" xml:space=\"preserve\">\n";
      ptr +="<path fill=\"#FFFFFF\" 
     ptr +="c-0.438,1.574-2.264,4.681-6.252,4.681c-3.988,0-5.813-3.107- 
     ptr +="</svg>\n";
     ptr +="</div>\n";
     ptr +="<div class=\"side-by-side humidity-text\">Humidity</div>\n";
     ptr +="<div class=\"side-by-side humidity\">";
     ptr +=(int)Humiditystat;
     ptr +="<span class=\"superscript\">%</span></div>\n";
     ptr +="</div>\n";


     ptr +="<div class=\"data\">\n";
     ptr +="<div class=\"side-by-side button-text\">Button</div>\n";
     ptr +="<div class=\"side-by-side Button\">";
     ptr +=" <p><a href=\"/5/on\"><button class=\"button\">ON</button></a> 
     // If the output5State is off, it displays the ON button       
        if (output5State=="off") {
          ptr +="<p><a href=\"/5/on\"><button class=\"button\">ON</button> 
        } else {
          ptr +="<p><a href=\"/5/off\"><button class=\"button 
         // If the output4State is off, it displays the ON button       
        if (output4State=="off") {
          ptr +="<p><a href=\"/4/on\"><button class=\"button\">ON</button> 
        } else {
          ptr +="<p><a href=\"/4/off\"><button class=\"button 
           ptr +="<span class=\"superscript\">%</span></div>\n";
           ptr +="</div>\n";
           ptr +="</div>\n";
           ptr +="</body>\n";
           ptr +="</html>\n";
           return ptr;
  • Why are you searching for request parameters inside your generated response HTML? – gre_gor Feb 19 '19 at 0:52
  • Where shoulf I search ! – Al Ma Feb 19 '19 at 12:25
  • @AlMa, did you read the answer? – Juraj Feb 23 '19 at 12:25

Write handlers for different URLs

server.on("/5/on", led5On); 
server.on("/5/off", led5off);

do you get the concept?

or you could write one handler server.on("/setLED", setLED);, if you would send "/setLED?num=5&state=on". and then in setLED function get the parameters values as

String num = sever.arg("num");
String state = sever.arg("state");

see the SimpleAuthentication example

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.