Automatic dusk-to-dawn light
Lights switch on by themselves at dusk and off again in the morning.
Shop by category
Shop all componentsThe simplest useful smart-home device: a NodeMCU, a relay and a web page with one big button. A good base for adding timers, sensors or voice assistants later.
Keep to low-voltage loads unless a qualified electrician wires the mains side in an earthed, closed enclosure.
Tick anything you already have and the total updates.
An electrically isolated switch that lets a 3.3/5 V pin turn a 12 V (or mains) load on and off.
Follow the colours: red is power, dark grey is ground, blue is a signal.
wifi_smart_switch.ino// Wi-Fi smart switch
// Geekobot project library · board: NodeMCU ESP8266
// Wiring diagram, parts list and build steps: Geekobot project page "wifi-smart-switch"
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
// ---- pins ----
const uint8_t RELAY_PIN = D5;
const bool RELAY_ACTIVE_LOW = true; // most relay modules switch on when IN is LOW
const char* WIFI_SSID = "YOUR_WIFI_NAME"; // 2.4 GHz network
const char* WIFI_PASS = "YOUR_WIFI_PASSWORD";
ESP8266WebServer server(80);
const unsigned long READ_EVERY_MS = 2000;
unsigned long lastRead = 0;
const char* statusText = "Starting";
bool relayOn = false;
bool manualMode = false; // set when you press the button on the dashboard
const char PAGE[] PROGMEM = R"HTML(<!doctype html><html><head><meta name="viewport" content="width=device-width,initial-scale=1"><title>Smart switch</title>
<style>body{font-family:system-ui,sans-serif;background:#0d093d;color:#fff;margin:0;padding:20px}h1{font-size:20px;margin:0 0 14px}
.g{display:grid;grid-template-columns:repeat(auto-fit,minmax(140px,1fr));gap:12px}.c{background:#1c1866;border-radius:14px;padding:14px}
.l{font-size:12px;opacity:.7}.v{font-size:28px;font-weight:700;margin-top:4px}button{margin-top:14px;padding:12px 18px;border:0;border-radius:10px;font-size:16px;background:#a9c5ea;color:#0d093d}
.s{margin-top:12px;font-size:13px;opacity:.8}</style></head><body><h1>Smart switch</h1><div class="g" id="g"></div><button id="b" onclick="tog()">Switch</button><p class="s" id="s">Connecting…</p>
<script>const F=[];
async function tick(){try{const d=await(await fetch('/data')).json();document.getElementById('g').innerHTML=F.map(f=>'<div class="c"><div class="l">'+f[1]+'</div><div class="v">'+(typeof d[f[0]]==='boolean'?(d[f[0]]?'YES':'no'):d[f[0]])+' '+f[2]+'</div></div>').join('');
document.getElementById('s').textContent='Status: '+d.status+' · updated '+new Date().toLocaleTimeString();document.getElementById('b').textContent=(d.relay?'Turn OFF':'Turn ON')+(d.auto?' (auto)':'');}catch(e){document.getElementById('s').textContent='Lost connection, retrying…'}}
async function tog(){const d=await(await fetch('/data')).json();await fetch('/relay?on='+(d.relay?0:1));tick()}setInterval(tick,2000);tick();</script></body></html>)HTML";
void setRelay(bool on) { digitalWrite(RELAY_PIN, (on != RELAY_ACTIVE_LOW) ? HIGH : LOW); }
void handleData() {
String j = "{";
j += "\"relay\":"; j += relayOn ? "true," : "false,"; j += "\"auto\":"; j += manualMode ? "false," : "true,";
j += "\"status\":\""; j += statusText; j += "\"}";
server.send(200, "application/json", j);
}
void handleRoot() { server.send_P(200, "text/html", PAGE); }
void handleRelay() {
if (server.hasArg("on")) {
manualMode = true;
relayOn = server.arg("on") == "1";
setRelay(relayOn);
}
server.send(200, "text/plain", relayOn ? "on" : "off");
}
void evaluate() {
statusText = "OK";
}
void setup() {
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT);
setRelay(false);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
Serial.print(F("Connecting to Wi-Fi"));
for (int i = 0; i < 40 && WiFi.status() != WL_CONNECTED; i++) { delay(500); Serial.print("."); }
Serial.println();
Serial.print(F("Open this address in your browser: http://"));
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.on("/data", handleData);
server.on("/relay", handleRelay);
server.begin();
}
void loop() {
server.handleClient();
if (millis() - lastRead >= READ_EVERY_MS) {
lastRead = millis();
evaluate();
Serial.println();
}
}
The sketch above runs here on a simulated NodeMCU (ESP8266). Move the sliders for the sensors and buttons it reads, and watch the Serial monitor, display and outputs change.
Load must be on COM and NO, with its own supply in series.
Your module may be active-HIGH. Set RELAY_ACTIVE_LOW to false.
Some 5 V relay boards do not fully switch off with 3.3 V logic. Remove the JD-VCC jumper, feed JD-VCC from 5 V and VCC from 3.3 V, or use a relay board made for 3.3 V input.
Open the Serial Monitor at 115200 baud and check for messages. Make sure you chose the right board and port in the Arduino IDE.
Close other programs using the serial port, try another USB cable (some are charge-only) and press reset just before uploading.
Not directly; the board serves the page on your local network only. For remote access, send the readings to a cloud service such as MQTT, ThingSpeak or Blynk, or use a VPN into your home network.
No. ESP32-C3 and ESP8266 boards support 2.4 GHz networks only. Most routers broadcast both, so choose the 2.4 GHz name.
The relay is rated for mains, but mains wiring is dangerous. Keep this build on low-voltage loads (12 V pumps, fans, LED strips), or have a qualified electrician fit the relay inside an earthed, closed enclosure.
5 parts: NodeMCU ESP8266 V3 Lua CH340 Wi-Fi Development Board, 5V Relay Module with Optocoupler (1 channel), DC Power Adapter 12V 2A (5.5x2.1mm), Solderless Breadboard (830 points) and Dupont Jumper Wires (40 pcs). Tools: Digital Multimeter and Wire Stripper and Cutter. On this page you can tick the parts you already have and add only the rest to your cart.
It is a beginner project and takes about 1–2 hours. The build on this page has 6 steps.
No. This page has the complete, tested code (wifi_smart_switch.ino).
Yes. The Run it panel under the code runs this exact sketch on a simulated board. Move the sliders or press the buttons for the sensors it reads, and the Serial monitor, display and outputs react as they will on the real build. It is a simulation, so real readings depend on your parts and wiring.
About ₹533 for the parts at the prices on this page, plus tools if you do not have them. Quantity price breaks lower the cost when you build several.
Isolation, relays and switching higher-power loads.
Ideas that reuse this build: Bluetooth 4-channel version. They are listed under Take it further on this page.
Lights switch on by themselves at dusk and off again in the morning.
A precise thermostat that keeps tropical fish at a steady 25–26 °C.
Starts the pump when the tank runs low and stops it when it is full.
Tap a hidden pad to switch a lamp on or off, even through a wooden desk.
Learning project. Check every circuit and the datasheet of each part before you power it up.
Connecting to Wi-Fi... Open this address in your browser: http://192.168.1.50
A simulation of the sketch above, so you know what to expect. Real readings vary with your parts and wiring.