Smart devices like power outlets are becoming more and more common in our daily lives. In this article, we will look at how to create a smart socket with the ability to control via Telegram yourself, using the ESP8266 module. This will allow you to remotely turn on and off devices connected to the outlet without having to spend money on expensive commercial solutions.
For this project you will need:
ESP8266 module (e.g. NodeMCU or ESP-01).
Relay for power control (220V relay).
A regular outlet or an extension cord.
A power source for the ESP8266 (e.g. via USB or battery).
Arduino IDE for ESP8266 programming.
A Telegram account to create a bot.
Connect one of the digital pins of the ESP8266 (eg D1) to the IN pin on the relay.
Connect the GND pin of the ESP8266 to the GND on the relay.
Connect VCC(V+) relay pin to 3.3V ESP8266.
Connect one of the socket wires to COM on the relay.
Connect the second wire to the NO (Normally Open) or NC (Normally Closed) contact of the relay depending on how you want to control the outlet.
Close the circuit by connecting the wire from NO or NC to power.
Open Telegram and search for @BotFather bot.
Send the /newbot command and give it a name and a unique username.
After that, you will receive an API token that will be used in the code.
Now that everything is connected, we can move on to writing the code for the ESP8266. We will use the WiFi, ESP8266TelegramBOT and ArduinoJson libraries.
Open the Arduino IDE, go to Sketch -> Include Library -> Manage Libraries.
Find and install Universal Telegram Bot і ArduinoJson.
#include <ESP8266WiFi.h> #include <WiFiClientSecure.h> #include <UniversalTelegramBot.h> #include <ArduinoJson.h> const char* ssid = "your_SSID"; const char* password = "your_PASSWORD"; const char* botToken = "your_bot_token"; // Ваш токен від BotFather WiFiClientSecure client; UniversalTelegramBot bot(botToken, client); const int relayPin = D1; void setup() { Serial.begin(115200); pinMode(relayPin, OUTPUT); digitalWrite(relayPin, LOW); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi"); client.setInsecure(); // Використовується для роботи з HTTP API Telegram } void loop() { int numNewMessages = bot.getUpdates(bot.last_message_received + 1); while (numNewMessages) { for (int i = 0; i < numNewMessages; i++) { String chat_id = String(bot.messages[i].chat_id); String text = bot.messages[i].text; if (text == "/on") { digitalWrite(relayPin, HIGH); bot.sendMessage(chat_id, "Розетка ввімкнена", ""); } if (text == "/off") { digitalWrite(relayPin, LOW); bot.sendMessage(chat_id, "Розетка вимкнена", ""); } } numNewMessages = bot.getUpdates(bot.last_message_received + 1); } }
Upload the code to the ESP8266: Connect the ESP8266 to the computer via USB and upload the sketch using the Arduino IDE.
Sending commands via Telegram: Open a chat with your bot in Telegram. Send the command /on to enable the outlet or /off to disable it.
You have created a smart socket that can be controlled via Telegram using an ESP8266 module. This project is a great option for beginners as it helps you learn the basics of working with the Internet of Things (IoT) and interacting with the physical world through programming. Using this approach, you can create other smart devices that are easy to control remotely.
Disclaimer. This article is created for informational purposes only. All advice and instructions are provided for educational purposes and we are not responsible for any possible consequences related to the implementation of this project. Always use safety precautions when working with electronic components.