Home › DIY Projects › Phone-Controlled Light
Control a Light from Your Phone with ESP32 (Wi-Fi Web Server)
Your first IoT project: the ESP32 hosts its own web page, and you tap ON or OFF on your phone to switch the light. No app, no cloud, no extra parts.
- Level: Beginner
- Time: 20 minutes
- Board: ESP32 DevKit V1
- No wiring needed
Watch it in action
Subscribe to ZEENAIX on YouTube for more projects
What you will build
The ESP32 has Wi-Fi built in. In this project it joins your Wi-Fi network and runs a tiny web server. Open its IP address (for example http://192.168.1.25) in your phone's browser and you get a page with a glowing bulb and two big buttons, ON and OFF. Tap one and the blue LED on the board switches instantly.
We use the LED that is already built into the ESP32 DevKit V1 on GPIO 2, so you do not need any wires. You will learn how the ESP32 connects to Wi-Fi, how a web server answers a browser, and how a link like /on can run code on a microcontroller. This is the same idea behind smart plugs and smart bulbs.
Components required
How it connects
Tap any part in the diagram to see what it does and where it connects.
Tap a part to learn more.
| Part | ESP32 pin | Wire |
|---|---|---|
| Built-in blue LED | GPIO 2 | Already on the board |
| USB port | Your computer or a USB charger | Micro USB cable |
Arduino code
Copy this sketch into the simulator or the Arduino IDE. It uses only the WiFi and WebServer libraries that come with the ESP32 board package, so there is nothing extra to install. We wrote and tested the code ourselves; you are free to use and change it.
/*
ZEENAIX DIY Project: Control a Light from Your Phone with ESP32 (Wi-Fi web server)
https://zeenaix.com/diy-projects/phone-controlled-light-esp32/
Parts: ESP32 DevKit V1 (30-pin). Uses the blue LED built into the board
on GPIO 2, so no other parts are needed.
What it does:
Joins your Wi-Fi and runs a small web page. Open the IP address it prints
on your phone (same Wi-Fi) and tap ON or OFF to switch the light.
Also works with simple links: /on /off /toggle
In the ZEENAIX simulator keep WIFI_NAME as "Velxio-GUEST" and open the
IoT Gateway link. On a real board, put your own Wi-Fi name and password.
*/
#include <WiFi.h>
#include <WebServer.h>
const char* WIFI_NAME = "Velxio-GUEST"; // your Wi-Fi name
const char* WIFI_PASS = ""; // your Wi-Fi password
const int LIGHT_PIN = 2; // built-in blue LED on ESP32 DevKit V1
WebServer server(80);
bool lightOn = false;
void setLight(bool on) {
lightOn = on;
digitalWrite(LIGHT_PIN, on ? HIGH : LOW);
Serial.println(on ? "Light ON" : "Light OFF");
}
void sendPage() {
String html = "<!DOCTYPE html><html><head><meta charset='utf-8'>";
html += "<meta name='viewport' content='width=device-width,initial-scale=1'>";
html += "<title>ZEENAIX Light</title><style>";
html += "body{font-family:sans-serif;text-align:center;background:#0F172A;color:#fff;margin:0;padding:40px 16px}";
html += ".bulb{width:120px;height:120px;border-radius:50%;margin:20px auto;background:";
html += lightOn ? "#facc15;box-shadow:0 0 60px #facc15}" : "#334155}";
html += "a{display:inline-block;margin:10px;padding:18px 34px;border-radius:999px;font-size:22px;";
html += "font-weight:700;text-decoration:none;color:#06281f}";
html += ".on{background:#22c55e}.off{background:#e2e8f0}</style></head><body>";
html += "<h1>ZEENAIX Smart Light</h1><div class='bulb'></div>";
html += "<p>The light is <b>";
html += lightOn ? "ON" : "OFF";
html += "</b></p><a class='on' href='/on'>ON</a><a class='off' href='/off'>OFF</a>";
html += "</body></html>";
server.send(200, "text/html", html);
}
void setup() {
Serial.begin(115200);
pinMode(LIGHT_PIN, OUTPUT);
setLight(false);
WiFi.begin(WIFI_NAME, WIFI_PASS);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" connected!");
server.on("/", sendPage);
server.on("/on", []() { setLight(true); sendPage(); });
server.on("/off", []() { setLight(false); sendPage(); });
server.on("/toggle", []() { setLight(!lightOn); sendPage(); });
server.begin();
Serial.print("Open this on your phone: http://");
Serial.println(WiFi.localIP());
}
void loop() {
server.handleClient();
}Step-by-step instructions
A. Try it in the free simulator (5 minutes)
- Open the ZEENAIX Arduino Simulator and choose Blank editor, then pick the ESP32 DevKit V1 board.
- Delete the starter code, then paste the sketch from the code section. Keep
WIFI_NAMEas"Velxio-GUEST", which is the simulator’s virtual Wi-Fi. - Press the green ▶ run button. The first compile for ESP32 takes a little longer than Arduino Uno.
- Open the Serial Monitor. You will see
Connecting to Wi-Fi.. connected!and thenOpen this on your phone: http://... - Open the simulator’s IoT Gateway link to load the ESP32 web page, then tap ON and OFF and watch the blue LED on the board.
B. Build it for real (15 minutes)
- In the Arduino IDE, add the ESP32 boards: Tools › Board › Boards Manager, search "esp32" and install esp32 by Espressif Systems.
- In the code, change
WIFI_NAMEandWIFI_PASSto your own Wi-Fi name and password. The ESP32 works only on 2.4 GHz Wi-Fi. - Connect the ESP32 with a micro USB data cable. Choose Tools › Board › ESP32 Dev Module and the right port, then press Upload.
- Open the Serial Monitor at 115200 baud and press the EN (reset) button on the board. Note the IP address it prints.
- Connect your phone to the same Wi-Fi, open the browser and type the IP address, for example
http://192.168.1.25. - Tap ON and OFF. The blue LED next to the USB port switches. Add the page to your home screen to use it like an app.
Try it here: the page your phone will show
The light is OFF
Serial Monitor: Light OFF
How it works
WiFi.begin() joins your network and the router gives the ESP32 an IP address, like a house number on your Wi-Fi. WebServer server(80) listens on port 80, the normal port for web pages.
Each server.on() line links a web address to a piece of code. When your phone opens /on, the ESP32 runs setLight(true), which sets GPIO 2 HIGH and lights the LED, and then sends the page back so you see the new state. /off does the opposite, and /toggle flips it. In loop(), server.handleClient() keeps checking for new requests.
The web page itself is just HTML text built inside sendPage(). The bulb colour changes depending on lightOn, so the page always shows the real state of the light.
Tested: we compiled and ran this exact sketch on the ESP32 DevKit V1 in the ZEENAIX Arduino Simulator. It compiled successfully, and the Serial Monitor showed Connecting to Wi-Fi.. connected! followed by Open this on your phone: http://192.168.4.15, with the web server running.
Real-life uses of a Wi-Fi switch
Smart room light
Add a 5V relay module to GPIO 2 (with care and adult help for mains power) and switch a real lamp from your phone.
Night lamp for kids
Switch a small LED strip on or off from bed without getting up.
Shop sign switch
Turn a small display light on and off from the counter.
Fan or cooler control
With a relay, switch a small DC fan from anywhere in the house on the same Wi-Fi.
Learning IoT basics
The same web server idea is used to read sensors, so it is the first step to your own smart home dashboard.
Troubleshooting
- Stuck on "Connecting to Wi-Fi...": check the Wi-Fi name and password, and make sure your router has 2.4 GHz turned on. The ESP32 cannot join 5 GHz-only networks.
- Upload fails with "Failed to connect": hold the BOOT button on the ESP32 while the IDE says "Connecting...", and use a data cable, not a charge-only cable.
- Page does not open on the phone: the phone must be on the same Wi-Fi (not mobile data). Type the address with
http://, exactly as the Serial Monitor shows. - Nothing in the Serial Monitor: set the speed to 115200 baud and press the EN button to restart the board.
Frequently asked questions
Do I need to install an app?
No. The ESP32 serves a normal web page, so any phone browser works, Android or iPhone.
Can I control it from outside my home?
Not with this simple version. It works on your local Wi-Fi only, which is also safer. Remote control needs a cloud service or port forwarding.
Can I switch a real bulb or fan?
Yes, by connecting a relay module to GPIO 2 instead of using the built-in LED. Mains 230V wiring is dangerous, so do it only with an adult who knows electrical safety.
Do I need to buy parts to try this?
No. You can run the whole project in the free ZEENAIX simulator first, then buy the ESP32 when you are ready to build it.
Build it for real
Get the ESP32 board and jumper wires from the ZEENAIX store, with delivery across India. The jumper wires are handy when you add a relay or more LEDs later.
▶ Copy code + open simulatorMore DIY projects
ESP32 DevKit V1 × 1