The Idea
I got tired of getting up to turn off the fan at night. Sounds lazy, I know. But that's honestly how this project started — lying in bed, too warm, too comfortable to move.
So I built a Telegram-controlled relay switch using my ESP32. Now I control four appliances from my phone — fan, lights, water pump, and a desk lamp — without any monthly subscription or cloud service.
Here's the full build.
What You'll Need
Hardware:
- ESP32 DevKit v1
- 4-channel 5V relay module
- Jumper wires (male to male)
- USB power adapter (5V 2A minimum)
- Any AC appliance to control (start with a lamp — safer)
Software:
- Arduino IDE (2.x recommended)
- Libraries: WiFi.h (built-in), UniversalTelegramBot, ArduinoJson
How It Works
The ESP32 connects to your WiFi. It then polls the Telegram Bot API every 1.5 seconds using an HTTPS GET request. When you send /fan on from your phone, the ESP32 reads the message, triggers GPIO pin 26, and the relay clicks ON.
Simple. Reliable. No middleman.
Circuit Connections
| Relay Module Pin | ESP32 Pin |
|---|---|
| IN1 | GPIO 26 |
| IN2 | GPIO 27 |
| IN3 | GPIO 14 |
| IN4 | GPIO 12 |
| VCC | 5V (VIN) |
| GND | GND |
⚠️ Never connect AC mains directly without proper insulation. Use pre-wired relay boards with terminal blocks.
Setting Up Your Telegram Bot
- Open Telegram → search @BotFather
- Send
/newbot→ give it a name → get your token - Search @userinfobot → send
/start→ copy your Chat ID - Replace the token and chat ID in the code above
Troubleshooting
Bot not responding?
- Check your WiFi credentials
- Make sure Chat ID matches exactly (can be negative for group chats)
- Some ISPs block Telegram — try a different network
Relay clicking but appliance not working?
- Check if your relay is active LOW or active HIGH
- Most blue relay modules are active LOW (LOW = ON)
ESP32 keeps restarting?
- Relay module drawing too much current from 3.3V pin
- Power the relay VCC from 5V (VIN pin), not 3.3V
What's Next?
Once this works, you can add:
- A small OLED display showing appliance status
- Scheduled ON/OFF using NTP time
- Voice control via Google Assistant + IFTTT
The foundation is solid. Build on it.
