ESP8266 Easy Address: No IP to Remember, Type bluegrays.local and Switch the LED from Your Phone
Type bluegrays.local in the browser and the page of the NodeMCU opens: no IP address to remember and no need to look at the Serial Monitor every time. The board joins your home Wi-Fi and gives itself a name on the network with mDNS; when a phone on the same network asks for that name, the board answers. The single button on the page switches the LED on and off. You change the name in one line of code. No app or account needed. The ESP8266 only joins 2.4 GHz networks.

Video
Circuit Diagram

Source Code
1// ─────────────────────────────────────────
2// BlueGrays · bluegrays.com
3// ESP8266 Easy Address: No IP to Remember, Type bluegrays.local and Switch the LED from Your Phone
4// ─────────────────────────────────────────
5// The NodeMCU joins your home Wi-Fi and gives itself a name on the
6// network. Without memorising the IP address, type bluegrays.local in the
7// browser of a phone on the same network: the page that switches the LED
8// opens. A service called mDNS announces the name to the phones; the IP
9// address is printed to the Serial port as well.
10//
11// Pins:
12// D5 (GPIO14) -> 220 ohm resistor -> LED anode (long leg)
13// GND -> LED cathode (short leg, minus rail)
14
15#include <ESP8266WiFi.h>
16#include <ESP8266WebServer.h>
17#include <ESP8266mDNS.h>
18
19// ============ WI-FI NETWORK ============
20// Write the name and password of your own network inside the quotes. The ESP8266 only joins 2.4 GHz
21// networks.
22const char *const WIFI_SSID = "YOUR_WIFI_NAME";
23const char *const WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";
24const unsigned long WIFI_HINT_MS = 20000; // Time until the hint (ms)
25
26// ============ EASY ADDRESS ============
27// The name of the board on the network: http://bluegrays.local in the browser opens this board. Two
28// boards on one network need different names.
29const char *const HOST_NAME = "bluegrays";
30
31// ============ PINS ============
32// D3, D4 and D8 have a special job while the board starts; the LED uses the spare pin D5.
33const uint8_t PIN_LED = D5; // GPIO14
34
35// ============ PAGE TEXTS ============
36const char TEXT_TITLE[] = "Easy Address";
37const char TEXT_ON[] = "ON";
38const char TEXT_OFF[] = "OFF";
39const char TEXT_NOT_FOUND[] = "Page not found";
40
41// ============ WEB PAGE ============
42// The fixed top of the page: UTF-8 encoding, a phone-friendly width and the looks (CSS).
43const char PAGE_HEAD[] = R"rawliteral(<!DOCTYPE html><html><head><meta charset="utf-8">
44<meta name="viewport" content="width=device-width,initial-scale=1"><title>ESP8266</title>
45<style>
46body{margin:0;padding:28px 20px;background:#0f172a;color:#f8fafc;font-family:sans-serif;
47text-align:center}
48h1{font-size:26px;margin:0 0 28px}
49a{display:block;width:240px;height:240px;margin:0 auto;border-radius:50%;background:#334155;
50color:#f8fafc;font-size:36px;font-weight:bold;line-height:240px;text-decoration:none}
51a.on{background:#facc15;color:#0f172a}
52</style></head><body>)rawliteral";
53
54ESP8266WebServer server(80); // Port 80: the web default
55bool ledOn = false;
56bool mdnsUp = false;
57bool wifiUp = false;
58bool hintDone = false;
59
60// Switches the LED on or off.
61void setLed(bool on) {
62 ledOn = on;
63 digitalWrite(PIN_LED, on ? HIGH : LOW);
64}
65
66// Announces the easy address. mDNS can only start while the Wi-Fi is up, so this runs on joining,
67// not in setup.
68void startMdns() {
69 if (!mdnsUp && MDNS.begin(HOST_NAME)) {
70 mdnsUp = true;
71 MDNS.addService("http", "tcp", 80);
72 }
73 if (mdnsUp) {
74 Serial.print("Easy address: ");
75 Serial.print("http://");
76 Serial.print(HOST_NAME);
77 Serial.println(".local");
78 }
79}
80
81// Builds the page and sends it to the browser: one big button that shows the current state.
82void sendPage() {
83 String html = PAGE_HEAD;
84 html += "<h1>";
85 html += TEXT_TITLE;
86 html += ledOn ? "</h1><a class=\"on\" href=\"/toggle\">" : "</h1><a href=\"/toggle\">";
87 html += ledOn ? TEXT_ON : TEXT_OFF;
88 html += "</a></body></html>";
89 // The browser must not keep a copy of the page; every tap has to reach the board.
90 server.sendHeader("Cache-Control", "no-store");
91 server.send(200, "text/html; charset=utf-8", html);
92}
93
94// An address that has done its job sends the browser back to the root page (303). The address bar
95// always shows / and reloading the page does not repeat the last tap.
96void goHome() {
97 server.sendHeader("Location", "/");
98 server.send(303);
99}
100
101// Every address has its own function; it runs when the browser asks for that address.
102void handleRoot() {
103 sendPage();
104}
105
106// The address of the button: flips the state (off if it was on, on if it was off).
107void handleToggle() {
108 setLed(!ledOn);
109 Serial.println(ledOn ? "LED is on" : "LED is off");
110 goHome();
111}
112
113void handleNotFound() {
114 server.send(404, "text/plain; charset=utf-8", TEXT_NOT_FOUND);
115}
116
117// Watches the connection: prints the IP address once connected and reports a drop. The ESP8266
118// rejoins a lost network by itself.
119void checkWifi() {
120 bool up = WiFi.status() == WL_CONNECTED;
121 if (up && !wifiUp) {
122 hintDone = true;
123 Serial.println("Wi-Fi connected");
124 Serial.print("Open in your browser: ");
125 Serial.print("http://");
126 Serial.println(WiFi.localIP());
127 startMdns();
128 } else if (!up && wifiUp) {
129 Serial.println("Wi-Fi lost. Reconnecting");
130 }
131 wifiUp = up;
132
133 if (!up && !hintDone && millis() > WIFI_HINT_MS) {
134 hintDone = true;
135 Serial.println("Could not join the Wi-Fi");
136 Serial.println("Check the network name and password");
137 Serial.println("The ESP8266 only sees 2.4 GHz networks");
138 }
139}
140
141void setup() {
142 Serial.begin(115200);
143 pinMode(PIN_LED, OUTPUT);
144 setLed(false);
145 Serial.println();
146 Serial.println("Easy address: bluegrays.local");
147 if (strncmp(WIFI_SSID, "YOUR_", 5) == 0) {
148 Serial.println("Put your Wi-Fi name and password in the code");
149 }
150 Serial.println("Connecting to Wi-Fi");
151 // The board opens no network of its own, it joins yours
152 WiFi.mode(WIFI_STA);
153 WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
154
155 server.on("/", handleRoot);
156 server.on("/toggle", handleToggle);
157 server.onNotFound(handleNotFound);
158 server.begin();
159}
160
161// loop never waits: a long delay would freeze the page and can drop the Wi-Fi.
162void loop() {
163 checkWifi();
164 MDNS.update(); // Answers the name questions
165 server.handleClient(); // Handles a waiting request
166}
167FAQ
What is mDNS?
mDNS is a way to turn names into IP addresses on small networks. The phone asks every device on the network at once who has bluegrays.local; the board that carries the name answers with its own IP address. No server and no router setting is involved. Names ending in .local are reserved for this method and only work inside the same network.
Does bluegrays.local open on every device?
iPhone, iPad, Mac and current Windows and Linux computers resolve .local names by themselves. On Android, version 12 and later usually do; on older phones the name may not open. In that case use the IP address printed in the Serial Monitor: the board prints both addresses and both open the same page.
How do I change the name?
Write the new name inside the quotes of the HOST_NAME line; with salon, for example, the address becomes salon.local. Use only lowercase letters, digits and the hyphen; no spaces and no accented letters. Two boards on one network need different names, otherwise the phone cannot tell which one to open.
Can I open bluegrays.local from outside my home?
No. Names ending in .local are only heard inside the same Wi-Fi network; a question asked over mobile data or from another network never reaches the board. That is protection, not a flaw: only devices on your home network can open the page of the board. Guest networks also often keep devices from seeing each other.
