Web page LED switch
Join your Wi-Fi, serve a one-button page and switch GPIO 2 from any phone on the network.
ESP32
3V3
GND
EN
23
VP
22
VN
TX
34
RX
35
21
32
GND
33
19
25
18
26
5
27
17
14
16
12
4
GND
0
13
2
D2
15
D3
D1
CMD
D0
5V
CLK
Wiring
| From | To board | Note |
|---|---|---|
| LED anode, through 220 Ω | GPIO 2 | Skip if your board has an onboard LED |
| LED cathode | GND |
Tested on this board
Not run on this board yet. Treat the wiring and code as a first draft.
Firmware, Arduino sketch for ESP32 Dev Module
#include <WiFi.h>
#include <WebServer.h>
const char* WIFI_NAME = "your-network";
const char* WIFI_PASS = "your-password";
const int LED = 2;
WebServer server(80);
bool on = false;
void page() {
String h = "<!doctype html><meta name=viewport content='width=device-width'>";
h += "<h1>LED is ";
h += on ? "on" : "off";
h += "</h1><a href='/toggle'>Toggle</a>";
server.send(200, "text/html", h);
}
void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
WiFi.begin(WIFI_NAME, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(300);
Serial.print(".");
}
Serial.printf("\nOpen http://%s\n", WiFi.localIP().toString().c_str());
server.on("/", page);
server.on("/toggle", []() {
on = !on;
digitalWrite(LED, on);
server.sendHeader("Location", "/");
server.send(303);
});
server.begin();
}
void loop() {
server.handleClient();
}Parts
| Quantity | Part | Where to buy |
|---|---|---|
| 1× | ESP32 dev board | Buy at Amazon |
| 1× | LED and 220 Ω resistor, only if your board has no LED on GPIO 2 |
Amazon buy links may include an Associates tag, and this site may earn a commission from qualifying Amazon purchases at no extra cost to you. Adafruit links are direct.
Libraries
WiFi (built in), WebServer (built in)
Load it
- In the Arduino IDE, install the esp32 board package and pick the board named above the sketch.
- Paste the sketch, choose the board's port and upload. Hold BOOT if the upload stalls at Connecting.
- Close the IDE's serial monitor, then open the bench here to watch the output.