How to create a smart socket using Telegram and ESP8266

16 August 2024 4 minutes Author: Cyber Witcher

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.

Hardware and software

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.

Connecting equipment

Connect the ESP8266 to the relay:

  1. Connect one of the digital pins of the ESP8266 (eg D1) to the IN pin on the relay.

  2. Connect the GND pin of the ESP8266 to the GND on the relay.

  3. Connect VCC(V+) relay pin to 3.3V ESP8266.

Connect the relay to the outlet:

  • 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.

Telegram bot settings

Create a bot in Telegram:

  1. Open Telegram and search for @BotFather bot.

  2. Send the /newbot command and give it a name and a unique username.

  3. After that, you will receive an API token that will be used in the code.

Programming ESP8266

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.

Install the necessary libraries:

  1. Open the Arduino IDE, go to Sketch -> Include Library -> Manage Libraries.

  2. Find and install Universal Telegram Bot і ArduinoJson.

Write the code for ESP8266:

#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);
  }
}

Loading and testing

  1. Upload the code to the ESP8266: Connect the ESP8266 to the computer via USB and upload the sketch using the Arduino IDE.

  2. 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.

Result

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.

Other related articles
Found an error?
If you find an error, take a screenshot and send it to the bot.