ESP8266 Web Page LED Control: Turn an LED On and Off from Your Phone with a NodeMCU
Switch an LED on and off from the browser of your phone. The NodeMCU V3 joins your home Wi-Fi and serves a small web page; open the board's IP address on a phone in the same network and you get one big button. Every tap turns the LED on the breadboard on or off, and the page shows the state of the LED. No app and no account needed; the IP address is printed to the Serial Monitor. The ESP8266 only joins 2.4 GHz networks.

Video
Circuit Diagram

Source Code
1// ─────────────────────────────────────────
2// BlueGrays · bluegrays.com
3// ESP8266 Web Page LED Control: Turn an LED On and Off from Your Phone with a NodeMCU
4// ─────────────────────────────────────────
5// The NodeMCU joins your home Wi-Fi and serves a small web page. Open the
6// board's IP address in the browser of a phone on the same network and you
7// get one big button; every tap turns the LED on the breadboard on or off.
8// The IP address is printed to the Serial Monitor (115200).
9//
10// Pins:
11// D1 (GPIO5) -> 220 ohm resistor -> LED anode (long leg)
12// GND -> LED cathode (short leg)
13
14#include <ESP8266WiFi.h>
15#include <ESP8266WebServer.h>
16
17// ============ WI-FI NETWORK ============
18// Write the name and password of your own network inside the quotes. The ESP8266 only joins 2.4 GHz
19// networks.
20const char *const WIFI_SSID = "YOUR_WIFI_NAME";
21const char *const WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";
22const unsigned long WIFI_HINT_MS = 20000; // Time until the hint (ms)
23
24// ============ PINS ============
25// D3, D4 and D8 have a special job while the board starts; the LED uses a spare pin such as D1.
26const uint8_t PIN_LED = D1; // GPIO5
27
28// ============ PAGE TEXTS ============
29const char TEXT_TITLE[] = "LED Control";
30const char TEXT_LED_ON[] = "LED is on";
31const char TEXT_LED_OFF[] = "LED is off";
32const char TEXT_TURN_ON[] = "TURN ON";
33const char TEXT_TURN_OFF[] = "TURN OFF";
34const char TEXT_NOT_FOUND[] = "Page not found";
35
36// ============ WEB PAGE ============
37// The fixed top of the page: UTF-8 encoding, a phone-friendly width and the looks (CSS).
38const char PAGE_HEAD[] = R"rawliteral(<!DOCTYPE html><html><head><meta charset="utf-8">
39<meta name="viewport" content="width=device-width,initial-scale=1"><title>ESP8266 LED</title>
40<style>
41body{margin:0;padding:40px 20px;background:#0f172a;color:#f8fafc;font-family:sans-serif;
42text-align:center}
43h1{font-size:26px;margin:0 0 8px}
44p{font-size:20px;color:#94a3b8;margin:0 0 40px}
45a{display:block;max-width:300px;margin:0 auto;padding:48px 0;border-radius:28px;background:#334155;
46color:#f8fafc;font-size:32px;font-weight:bold;text-decoration:none}
47a.on{background:#facc15;color:#1f2937}
48</style></head><body>)rawliteral";
49
50ESP8266WebServer server(80); // Port 80: the web default
51bool ledOn = false;
52bool wifiUp = false;
53bool hintDone = false;
54
55// Turns the LED on or off and reports it on the Serial port.
56void setLed(bool on) {
57 ledOn = on;
58 digitalWrite(PIN_LED, on ? HIGH : LOW);
59 Serial.println(on ? "LED on" : "LED off");
60}
61
62// Builds the page for the current LED state and sends it to the browser. The button is a link: it
63// leads to /on while the LED is off and to /off while it is on.
64void sendPage() {
65 String html = PAGE_HEAD;
66 html += "<h1>";
67 html += TEXT_TITLE;
68 html += "</h1><p>";
69 html += ledOn ? TEXT_LED_ON : TEXT_LED_OFF;
70 html += "</p>";
71 html += ledOn ? "<a class=\"on\" href=\"/off\">" : "<a href=\"/on\">";
72 html += ledOn ? TEXT_TURN_OFF : TEXT_TURN_ON;
73 html += "</a></body></html>";
74 // The browser must not keep a copy of the page; every tap has to reach the board.
75 server.sendHeader("Cache-Control", "no-store");
76 server.send(200, "text/html; charset=utf-8", html);
77}
78
79// Every address has its own function; it runs when the browser asks for that address.
80void handleRoot() {
81 sendPage();
82}
83
84void handleOn() {
85 setLed(true);
86 sendPage();
87}
88
89void handleOff() {
90 setLed(false);
91 sendPage();
92}
93
94void handleNotFound() {
95 server.send(404, "text/plain; charset=utf-8", TEXT_NOT_FOUND);
96}
97
98// Watches the connection: prints the IP address once connected and reports a drop. The ESP8266
99// rejoins a lost network by itself.
100void checkWifi() {
101 bool up = WiFi.status() == WL_CONNECTED;
102 if (up && !wifiUp) {
103 hintDone = true;
104 Serial.println("Wi-Fi connected");
105 Serial.print("Open in your browser: ");
106 Serial.print("http://");
107 Serial.println(WiFi.localIP());
108 } else if (!up && wifiUp) {
109 Serial.println("Wi-Fi lost. Reconnecting");
110 }
111 wifiUp = up;
112
113 if (!up && !hintDone && millis() > WIFI_HINT_MS) {
114 hintDone = true;
115 Serial.println("Could not join the Wi-Fi");
116 Serial.println("Check the network name and password");
117 Serial.println("The ESP8266 only sees 2.4 GHz networks");
118 }
119}
120
121void setup() {
122 Serial.begin(115200);
123 pinMode(PIN_LED, OUTPUT);
124 digitalWrite(PIN_LED, LOW); // Start with the LED off
125 Serial.println();
126 Serial.println("LED from a web page");
127 if (strncmp(WIFI_SSID, "YOUR_", 5) == 0) {
128 Serial.println("Put your Wi-Fi name and password in the code");
129 }
130 Serial.println("Connecting to Wi-Fi");
131 // The board opens no network of its own, it joins yours
132 WiFi.mode(WIFI_STA);
133 WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
134
135 server.on("/", handleRoot);
136 server.on("/on", handleOn);
137 server.on("/off", handleOff);
138 server.onNotFound(handleNotFound);
139 server.begin();
140}
141
142// loop never waits: a long delay would freeze the page and can drop the Wi-Fi.
143void loop() {
144 checkWifi();
145 server.handleClient(); // Handles a waiting request
146}
147FAQ
Why does the NodeMCU V3 not fit on a breadboard?
The NodeMCU V3 (LoLin) is a wide board: seated on a breadboard it covers every hole on both sides and leaves no room for wires. So the board sits next to the breadboard and we use male-to-female jumper wires: the female end goes on a board pin, the male end into the breadboard. Two breadboards side by side with the board across the middle also work. The narrower V2 boards leave one open row on each side.
Why is the LED on D1, would D4 not work?
D3, D4 and D8 have a special job while the board starts: D3 and D4 must be HIGH and D8 LOW, or the board may not boot. D4 also carries the blue onboard LED, which works inverted. For external LEDs, buttons and relays, D1, D2, D5, D6 and D7 are the reliable choices. ESP8266 pins give 3.3 V and one pin should supply 12 mA at most; with a 220 Ω resistor a red LED draws about 6 mA, so it is safe.
Can I open the page from outside my home?
No, not as it is. The board's address (the one starting with 192.168) is only valid inside your home network; it cannot be reached over mobile data or from another network. That is also a safeguard: the page has no password, so only people on your Wi-Fi can switch the LED. Control over the internet needs other methods such as a cloud service or MQTT; those are the subject of later projects.
Does the board's IP address change at every start?
The router hands out the address, and most routers give the same board the same address for days, but there is no guarantee: it can change when the router restarts or the board stays off for a long time. If the page does not open, check the latest address in the Serial Monitor. To pin it down, reserve a fixed address for this board in your router's settings (look for DHCP reservation or static IP).
