ad space

ESP8266 Web Server LED control

It is quite simple to write ESP8266 led on/off code without using WiFi connectivity but that is not the advantage of using ESP8266. The advantage of using ESP8266 is to use WiFi connectivity for wireless control of devices like a LED. In this ESP8266 web server example, it is illustrated how to control a LED  from a web page hosted by ESP8266 web server. Although simple, it illustrates the basic of creating a web server that serves a webpage with a button which controls a LED. The LED could be easily replaced with motors, switches sensors etc. Building on this basic foundation one can build complex IoT applications and IoT solutions for industrial needs or home automation projects.

It is assumed here that you know how to setup NodeMCU(ESP8266) WiFi Server using Arduino IDE. Here we will use NodeMCU which is based on ESP8266 chip. See ESP8266 vs NodeMCU if you don't know the difference them.

ESP8266 Web Server LED Control Program code

The following is code for configuring ESP8266 WiFi, setting it up as a Web Server with a button to control physical LED connected to D2 pin of ESP8266.

 #include "ESP8266WiFi.h"
#define LED D2

String ssid = "YOUR WIFI NAME";
String password = "YOUR WIFI PASSWORD";
String LEDstate; // State variable for LED to track its state
WiFiServer server(80);

void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
digitalWrite(LED,LOW);
LEDstate = "OFF";
Serial.println("");
Serial.println("");
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
server.begin();
Serial.println("Server started");
Serial.println(WiFi.localIP());
}

void loop() {
    WiFiClient client = server.available();
    if (!client) {
    return;
    }
    
    Serial.println("new client");
    while(!client.available()) {
    delay(1);
    }
    
    String req = client.readStringUntil('\r');
    Serial.println(req);
    client.flush();
    
    // Depending on the present state of LED, toggle its state
    if (req.indexOf("/ledtoggle") != -1) {
      if(LEDstate == "ON") {
      digitalWrite(LED,LOW);
      LEDstate = "OFF";
      Serial.println("LED OFF\n");
      } 
      else {
      digitalWrite(LED,HIGH);
      LEDstate = "ON";
      Serial.println("LED ON\n");
      }
    }
        String web;
        web += "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
        web += "<html>\r\n";
        web += "<head>\r\n";
        web += "<meta charset=\"UTF-8\"/>\r\n";
        web += "<title> LED Control </title>\r\n";
        web += "</head>\r\n";
        web += "<body>\r\n";
        web += "<h1>ESP8266 Web Server LED Controller</h1>\r\n";
        web += "<div>\r\n";
        web += "<a href=\"/ledtoggle\"> <button> LED On/Off </button> </a>\r\n";
        web += "</div>\r\n";
        web += "<br/>\r\n";
        web += "<p>\r\n";
        web += "LED State: "+ LEDstate +"\r\n";
        web += "</p>\r\n";
        web += "</body>\r\n";
        web += "</html>\r\n";
    client.print(web);
}
 

This code is written in the Arduino IDE for the ESP8266 Wi-Fi module. It is ESP8266 web server button example for LED control. It sets up a web server that allows the user to control an LED connected to the D2 pin of the ESP8266 module through a web page. See the difference between program code for esp8266 and Arduino code for led blinking.

The first line of the code includes the ESP8266WiFi library which is used to connect the ESP8266 module to a Wi-Fi network. The LED constant is defined to represent the pin number of the LED connected to the ESP8266 module.

Next, the Wi-Fi network name and password are stored in ssid and password respectively. The LEDstate variable is used to keep track of the state of the LED (ON/OFF).

The WiFiServer object is created on port 80 to listen for incoming connections from web clients.

In the setup() function, the serial communication is started and the LED pin is set to OUTPUT mode. The LED is turned off and the ESP8266 module is connected to the Wi-Fi network using the ssid and password variables. The code waits until the ESP8266 module is connected to the Wi-Fi network before proceeding. Once connected, the server is started and the IP address of the ESP8266 module is printed to the serial monitor.

In the loop() function, the server listens for incoming connections from web clients using the server.available() method. If there is no client available, the function returns. When a client connects, the code waits for the client to send a request by reading the request string until a carriage return character is received.

If the request string contains "/ledtoggle", the code toggles the state of the LED and updates the LEDstate variable accordingly. A web page is generated using HTML tags and stored in the web string. The HTML code includes a button that allows the user to toggle the LED and displays the current state of the LED.

Finally, the web page is sent to the client using the client.print() method, and the client connection is closed using the client.flush() method. The loop function continues to run, waiting for the next client connection.

LED Control with ESP8266 Web Page demonstration

Below is video that demonstrates the code above, that is, how the LED is controlled using the webpage served by the ESP8266 web server.

In this ESP8266 led example it was shown how a button on a web page can be used to control a LED connected to ESP8266 NodeMCU board. The opposite application would be to read the state of a switch and display the switch state on a web page. This can be useful in IoT projects and IoT monitoring application. In the esp8266 web server with html web page example it is illustrated how to read and display the state of a physical switch on a webpage. 


Post a Comment

Previous Post Next Post