ESP8266 Tutorials

Last time you learned how to connect the ESP8266 to a Wi-Fi network and download data from a URL. Today we will explore a way to listen to connections just like a server; a web server in fact. The ESP will listen for connections on port 80 and will serve simple pages to connected clients. For this example, a different approach will be taken to program the ESP8266. The structure we used in our last tutorial was purely procedural. The problem with procedural structured programs is that it is difficult and messy to handle multiple simultaneous connections. In this example we will use handlers which are a way of structuring our programs to serve multiple clients and at the same time keeping the code pretty.
The code will listen for connections on port 80, will server a web page showing a link with “LED On” or “LED Off”. Once clicked the on board LED switches on or off respectively. The on board LED is on GPIO 1, the pin used by TXD, so we won’t be able to use Serial. print() in our program.
We start off with the basics. Loading libraries and connecting to a Wi-Fi network.

#include <ESP8266WiFi.h>
#include <Wi-Fi Client. h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
const char* ssid = “your Wi-Fi SSID”;
const char* password = “password”;
ESP8266WebServer server(80);
MDNS Responder mdns;
String page Turn On = “<html><body><h1>LED is ON</h1><a href=\”/turnoff\”>Turn Off</a></body></html>”;
String page Turn Off = “<html><body><h1>LED is OFF</h1><a href=\”/turn on\”>Turn On</a></body></html>”;
void setup() {
Wi-Fi. begin(ssid, password);
while (Wi-Fi. status() != WL_CONNECTED) {
//a flashing LED means the ESP is not connected
digital Write(LED_BUILTIN,! digital Read(LED_BUILTIN));
//a stable LED on means the ESP is now connected
digital Write(LED_ BUILTIN, true);
mdns. begin(“esp8266”, Wi-Fi. local IP());

Do not forget to to change the SSID and PASSWORD for your own.
MDNS Responder lets us name our ESP8266 and use it instead of the IP address. Think of it as a personal local domain. The LED_BUILTIN is the LED on board the ESP which is connected to GPIO1. “digital Write(LED_BUILTIN,! digital Read(LED_BUILTIN));” line will keep the LED flashing until a connection to the Wi-Fi network is established. Then we call the MDNS to allow us to use the name “esp8266.local”. Next we define our commands which are basically calls for a different web page.

server. On("/turn on", [](){
   server. send(200, "text/html", page Turn On);

   digital Write(LED_BUILTIN,HIGH);


 server. On("/turnoff", [](){
   server. Send(200, "text/html", page Turnoff);

   digital Write(LED_BUILTIN,LOW);

   server. on Not Found([](){

     server. send(404, "text/html", "Command Not Found");


  server. begin();


void loop(void){
  server. handle Client();

The structure server. On acts just like events. When a certain criteria (the page) is met, execute some commands.
Read More: ESP8266 Tutorials

Leave a Comment

Your email address will not be published. Required fields are marked *