Automatic dusk-to-dawn light
Lights switch on by themselves at dusk and off again in the morning.
Shop by category
Shop all componentsMQTT is a light message system that smart-home software such as Home Assistant understands. The ESP32 connects to your broker, listens on a command topic and publishes its state on a second topic. Sending the text ON or OFF to the command topic switches the relay, and the state topic always shows the real position. Without a password your broker is open to anyone on your network, so give it a username and password if the network is shared.
Anything connected to 230 V mains must be installed by a qualified electrician in a closed, earthed enclosure. Test with a low-voltage lamp first.
Tick anything you already have and the total updates.
Follow the colours: red is power, dark grey is ground, blue is a signal.
USB phone charger. Anything connected to 230 V mains must be installed by a qualified electrician in a closed, earthed enclosure. Test with a low-voltage lamp first.
mqtt_relay.inoVerify & upload in Electrolark#include <WiFi.h>
#include <PubSubClient.h>
const char* SSID = "YOUR_WIFI";
const char* PASS = "YOUR_PASSWORD";
const char* BROKER = "192.168.1.50"; // your MQTT broker
const char* SET_TOPIC = "home/relay1/set";
const char* STATE_TOPIC = "home/relay1/state";
const int RELAY_PIN = 26;
const bool ACTIVE_LOW = true;
WiFiClient net;
PubSubClient mqtt(net);
void setRelay(bool on) {
digitalWrite(RELAY_PIN, (on == ACTIVE_LOW) ? LOW : HIGH);
mqtt.publish(STATE_TOPIC, on ? "ON" : "OFF", true); // retained
}
void onMessage(char* topic, byte* payload, unsigned int len) {
String msg;
for (unsigned int i = 0; i < len; i++) msg += (char)payload[i];
if (msg == "ON") setRelay(true);
else if (msg == "OFF") setRelay(false);
}
void reconnect() {
while (!mqtt.connected()) {
if (mqtt.connect("esp32-relay1")) { // add (id, user, password) if your broker needs them
mqtt.subscribe(SET_TOPIC);
} else {
delay(2000);
}
}
}
void setup() {
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, ACTIVE_LOW ? HIGH : LOW);
WiFi.begin(SSID, PASS);
while (WiFi.status() != WL_CONNECTED) delay(300);
mqtt.setServer(BROKER, 1883);
mqtt.setCallback(onMessage);
}
void loop() {
if (!mqtt.connected()) reconnect();
mqtt.loop();
}
Check the broker's IP address and that port 1883 is reachable from the ESP32's Wi-Fi.
Flip ACTIVE_LOW.
Check the topic names match exactly, including capital letters.
No. Any MQTT app or tool can send ON and OFF.
The broker keeps the last state so new subscribers see it straight away.
Yes. Use a second topic pair and a second pin.
4 parts: ESP32-WROOM-32 38-Pin Development Board, 5V Relay Module with Optocoupler, Solderless Breadboard and Dupont Jumper Wires (40 pcs). On this page you can tick the parts you already have and add only the rest to your cart.
It is a intermediate project and takes about 1.5 hours. The build on this page has 6 steps.
No. This page has the complete, tested code (mqtt_relay.ino).
About ₹530 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Connecting to an MQTT broker with PubSubClient; Command and state topics; Reconnecting automatically.
Anything connected to 230 V mains must be installed by a qualified electrician in a closed, earthed enclosure. Test with a low-voltage lamp first.
Ideas that reuse this build: ESP32 MQTT temperature dashboard. 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.