HomeAutomation System Using Telegram & ESP32 with feedback.

HomeAutomation System Using Telegram & ESP32 with feedback.

In this tutorial we build a homeautomation system using esp32 and Telegram.

We are going to control our homeappliances from anywhere  in world using Telegram. .

Introducing Telegram

Telegram Messenger is a cloud-based instant messaging and voice over IP service. You can easily install it in your smartphone (Android and iPhone) or computer (PC, Mac and Linux). It is free and without any ads. Telegram allows you to create bots that you can interact with.

Bots are third-party applications that run inside Telegram. Users can interact with bots by sending them messages, commands and inline requests. You control your bots using HTTPS requests to Telegram Bot API

The ESP32 will interact with the Telegram bot to receive and handle the messages, and send responses. In this tutorial you’ll learn how to use Telegram to send messages to your bot to control the ESP outputs from anywhere (you just need Telegram and access to the internet).

Components required

1.ESP32 Board.

2.Designed PCB.(You could also use relay instead of PCB.)

3.BC547 Transistor.

4.IN4007 Diode.

5. 1k ohm Resistance.

6. 5v Relay.

Universal Telegram Bot Library

To interact with the Telegram bot, we’ll use the Universal Telegram Bot Library created by Brian Lough that provides an easy interface for the Telegram Bot API.

For all the details about the library, take a look at the Universal Arduino Telegram Bot Library GitHub page.

ArduinoJson Library

You also have to install the ArduinoJson library. 


#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>   // Universal Telegram Bot Library written by Brian Lough: https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot
#include <ArduinoJson.h>

// Replace with your network credentials
const char* ssid = "xxxxxxxx";
const char* password = "xxxxxxxxxx";

// Initialize Telegram BOT
#define BOTtoken "xxxxxxxxxxxxxxxxxxxxx"  // your Bot Token (Get from Botfather)

// Use @myidbot to find out the chat ID of an individual or a group
// Also note that you need to click "start" on a bot before it can
// message you
#define CHAT_ID "xxxxxxxxx"

WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);

// Checks for new messages every 1 second.
int botRequestDelay = 1000;
unsigned long lastTimeBotRan;

const int Relay1 = 26;
const int Relay2 = 27;
const int Relay3 = 14;
const int Relay4 = 12;
const int Relay5 = 13;

String Relay1State = "HIGH";
String Relay2State = "HIGH";
String Relay3State = "HIGH";
String Relay4State = "HIGH";
String Relay5State = "HIGH";

// Handle what happens when you receive new messages
void handleNewMessages(int numNewMessages) {

  for (int i=0; i<numNewMessages; i++) {
    // Chat id of the requester
    String chat_id = String(bot.messages[i].chat_id);
    if (chat_id != CHAT_ID){
      bot.sendMessage(chat_id, "Unauthorized user", "");
    // Print the received message
    String text = bot.messages[i].text;

    String from_name = bot.messages[i].from_name;

    if (text == "START") {
      String welcome = "Welcome, " + from_name + ".\n";
      welcome += "Use the following commands to control your outputs.\n\n";
      welcome += "light1on to turn light1 ON \n";
      welcome += "light1off to turn light1 OFF \n";
      welcome += "light2on to turn light2 ON \n";
      welcome += "light2off to turn light2 OFF \n";
      welcome += "light3on to turn light3 ON \n";
      welcome += "light3off to turn light3 OFF \n";
      welcome += "light4on to turn light4 ON \n";
      welcome += "light4off to turn light4 OFF \n";
      welcome += "light5on to turn light5 ON \n";
      welcome += "light5off to turn light5 OFF \n";
     // welcome += "/state to request current GPIO state \n";
      bot.sendMessage(chat_id, welcome, "");

    if (text == "LIGHT1ON") {
      bot.sendMessage(chat_id, "Light1 state set to ON", "");
      Relay1State = "ON";
     if (text == "LIGHT1OFF") {
      bot.sendMessage(chat_id, "Light1 state set to OFF", "");
      Relay1State = "OFF";
      if (text == "LIGHT2ON") {
      bot.sendMessage(chat_id, "Light2 state set to ON", "");
      Relay2State =  "ON";
     if (text == "LIGHT2OFF") {
      bot.sendMessage(chat_id, "Light2 state set to OFF", "");
      Relay2State ="OFF";
      if (text == "LIGHT3ON") {
      bot.sendMessage(chat_id, "Light3 state set to ON", "");
      Relay3State =  "ON";
     if (text == "LIGHT3OFF") {
      bot.sendMessage(chat_id, "Light3 state set to OFF", "");
      Relay3State = "OFF";
      if (text == "LIGHT4ON") {
      bot.sendMessage(chat_id, "Light4 state set to ON", "");
      Relay4State =  "ON";
     if (text == "LIGHT4OFF") {
      bot.sendMessage(chat_id, "Light4 state set to OFF", "");
      Relay4State = "OFF";
      if (text == "LIGHT5ON") {
      bot.sendMessage(chat_id, "Light5 state set to ON", "");
      Relay5State = "ON";
     if (text == "LIGHT5OFF") {
      bot.sendMessage(chat_id, "Light5 state set to OFF", "");
      Relay5State ="OFF";
///////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////

 if (text == "LIGHT1STATE") {
  bot.sendMessage(chat_id, "Light1 is " + Relay1State + "");
     if (text == "LIGHT2STATE") {
  bot.sendMessage(chat_id, "Light2 is " + Relay2State + "");
     if (text == "LIGHT3STATE") {
  bot.sendMessage(chat_id, "Light3 is " + Relay3State + "");
     if (text == "LIGHT4STATE") {
  bot.sendMessage(chat_id, "Light4 is " + Relay4State + "");
     if (text == "LIGHT5STATE") {
  bot.sendMessage(chat_id, "Light5 is " + Relay5State + "");


void setup() {

  #ifdef ESP8266

  pinMode(Relay1, OUTPUT);
  pinMode(Relay2, OUTPUT);
  pinMode(Relay3, OUTPUT);
  pinMode(Relay4, OUTPUT);
  pinMode(Relay5, OUTPUT);
  digitalWrite(Relay1, LOW);
  digitalWrite(Relay2, LOW);
  digitalWrite(Relay3, LOW);
  digitalWrite(Relay4, LOW);
  digitalWrite(Relay5, LOW);
  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("Connecting to WiFi..");
  // Print ESP32 Local IP Address

void loop() {
  if (millis() > lastTimeBotRan + botRequestDelay)  {
    int numNewMessages = bot.getUpdates(bot.last_message_received + 1);

    while(numNewMessages) {
      Serial.println("got response");
      numNewMessages = bot.getUpdates(bot.last_message_received + 1);
    lastTimeBotRan = millis();

Network Credentials

nsert your network credentials in the following variables.

const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

Telegram Bot Token

Insert your Telegram Bot token you’ve got from Botfather on the BOTtoken variable.

#define BOTtoken "XXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"  // your Bot Token (Get from Botfather)

Telegram User ID

Insert your chat ID. The one you’ve got from the IDBot.


Video Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *