Automatic dusk-to-dawn light
Lights switch on by themselves at dusk and off again in the morning.
Shop by category
Shop all componentsTelegram bots are a simple, free way to control a gadget from outside your home network, with no port forwarding. The ESP32 asks the Telegram server for new messages every couple of seconds and acts on the commands. The sketch only obeys messages from your own chat ID, otherwise anyone who finds the bot could use it. Keep your bot token private.
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.
telegram_relay.inoVerify & upload in Electrolark#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
const char* SSID = "YOUR_WIFI";
const char* PASS = "YOUR_PASSWORD";
const char* BOT_TOKEN = "YOUR_BOT_TOKEN";
const String MY_CHAT_ID = "YOUR_CHAT_ID";
const int RELAY_PIN = 26;
const bool ACTIVE_LOW = true;
WiFiClientSecure client;
UniversalTelegramBot bot(BOT_TOKEN, client);
void setRelay(bool on) { digitalWrite(RELAY_PIN, (on == ACTIVE_LOW) ? LOW : HIGH); }
void setup() {
pinMode(RELAY_PIN, OUTPUT);
setRelay(false);
WiFi.begin(SSID, PASS);
client.setInsecure(); // skips certificate check; fine for a hobby bot
while (WiFi.status() != WL_CONNECTED) delay(300);
}
void loop() {
int n = bot.getUpdates(bot.last_message_received + 1);
for (int i = 0; i < n; i++) {
String chat = bot.messages[i].chat_id;
String text = bot.messages[i].text;
if (chat != MY_CHAT_ID) { bot.sendMessage(chat, "Not authorised", ""); continue; }
if (text == "/on") { setRelay(true); bot.sendMessage(chat, "Relay is ON", ""); }
if (text == "/off") { setRelay(false); bot.sendMessage(chat, "Relay is OFF", ""); }
}
delay(2000);
}
Check the token and that you pressed Start in the bot chat.
The chat ID in the sketch is wrong. Use the number from @myidbot.
Flip ACTIVE_LOW.
Yes, anywhere with mobile data, as long as the ESP32 has Wi-Fi.
It skips checking Telegram's certificate. Adequate for a lamp, not for anything critical.
Add a reconnect check in loop().
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 (telegram_relay.ino).
About ₹530 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Creating a bot with BotFather; Polling messages with UniversalTelegramBot; Restricting control to one chat ID.
Ideas that reuse this build: ESP32 web server relay switch. 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.