ESP8266 Tutorials

Last time you learned how to connect the ESP8266 to a WiFi 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.ESP8266 Tutorials
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 WiFi network.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
const char* ssid = “your wifi SSID”;
const char* password = “password”;
ESP8266WebServer server(80);
MDNSResponder mdns;
String pageTurnOn = “<html><body><h1>LED is ON</h1><a href=\”/turnoff\”>Turn Off</a></body></html>”;
String pageTurnOff = “<html><body><h1>LED is OFF</h1><a href=\”/turnon\”>Turn On</a></body></html>”;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
WiFi.begin(ssid, password);
delay(1000);
while (WiFi.status() != WL_CONNECTED) {
delay(200);
//a flashing LED means the ESP is not connected
digitalWrite(LED_BUILTIN,!digitalRead(LED_BUILTIN));
}
//a stable LED on means the ESP is now connected
digitalWrite(LED_BUILTIN,true);
mdns.begin(“esp8266”, WiFi.localIP());

Do not forget to to change the SSID and PASSWORD for your own.
MDNSResponder 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. “digitalWrite(LED_BUILTIN,!digitalRead(LED_BUILTIN));” line will keep the LED flashing until a connection to the WiFi 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.
For more detail: ESP8266 Tutorials


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

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

Scroll to Top