Controlling LED with Simple Web Server

 General audience classification icon  General audience classification icon
A sample Web application hosted on ESP8266 MCU is presented below.
This application allows it to control the state of the LED remotely, connecting to the ESP8266 board with a web browser. The program presented is based on the example “HelloServer” available in the ESP8266WebServer library. Some modifications were made to simplify the program and to handle requests to turn the LED on and off. To check if it works, adding WiFi network credentials and setting the led constant with the number of GPIO to which the LED is connected is required. After a successful connection to the WiFi, ESP8266 would present through the serial monitor the IP address (e.g. 192.168.4.1). Writing in the address bar in the browser “HTTP://192.168.4.1” should return the serial monitor message “hello from esp8266!”.

Assuming the address in the terminal is 192.168.4.1 one may use the following URLs to disable and enable the LED, respectively:

http://192.168.4.1/LED0 
http://192.168.4.1/LED1 
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
 
#ifndef STASSID
#define STASSID "*********"
#define STAPSK  "*********"
#endif
 
const char* ssid = STASSID;
const char* password = STAPSK;
 
ESP8266WebServer server(80);
 
const int led = 2;
 
void handleRoot() {
  //Originally LED was controlled for every root request
  //so it is required to comment the lines which modify the LED state
  //digitalWrite(led, 1);
  server.send(200, "text/plain", "hello from esp8266!\r\n");
  //digitalWrite(led, 0);
}
 
void handleNotFound() {
  //digitalWrite(led, 1);
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i = 0; i < server.args(); i++) {
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
  //digitalWrite(led, 0);
}
 
void setup(void) {
  pinMode(led, OUTPUT);
  //digitalWrite(led, 0);
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");
 
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
 
  if (MDNS.begin("esp8266")) {
    Serial.println("MDNS responder started");
  }
 
  server.on("/", handleRoot);
 
  // request for turning led on
  server.on("/LED1", [](){
    server.send(200, "text/plain", "LED is ON");
    digitalWrite(led, 1);
  });
 
  // request for turning led off
  server.on("/LED0", [](){
    server.send(200, "text/plain", "LED is OFF");
    digitalWrite(led, 0);
  });
 
  server.onNotFound(handleNotFound);
 
  server.begin();
  Serial.println("HTTP server started");
}
 
void loop(void) {
  server.handleClient();
  MDNS.update();
}