HomeAutomation System Using Telegram & ESP32 with feedback.

HomeAutomation System Using Telegram & ESP32 with feedback.

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

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

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

Designing the PCB.

To design the circuit and PCB, we used EasyEDA which is a browser based software to design PCBs.

Designing the circuit works like in any other circuit software tool, you place some components and you wire them together. 

Then, you assign each component to a footprint.

Having the parts assigned, place each component. When you’re happy with the layout, make all the connections and route your PCB.

Save your project and export the Gerber files.

Ordering the PCBs at PCBWay

This project is sponsored by PCBWay. PCBWay is a full feature Printed Circuit Board manufacturing service.

Once you have your Gerber files, you can order the PCB. Follow the next steps.

1. Download the Gerber files – click here to download the .zip file.

2. Go to PCBWay website and open the PCB Instant Quote page. 

3. PCBWay can grab all the PCB details and automatically fills them for you. Use the “Quick-order PCB (Autofill parameters)”.

4. Press the “+ Add Gerber file” button to upload the provided Gerber files.


And that’s it. You can also use the OnlineGerberViewer to check if your PCB is looking as it should.

Now select the shipping method , the one you prefer and has cost efficient.

You can increase your PCB order quantity and change the solder mask color. I’ve ordered the Green color.

PCBWay has lots of other staggering solder mask,

 Now they can produce pink, orange, grey, even the transparent solder mask. 

Apart from this they also provide Black core PCB.

Once you’re ready, you can order the PCBs by clicking “Save to Cart” and complete your order.

After approximately one week using the DHL shipping method, I received the PCBs at my place.

As usual, everything comes well packed, and the PCBs are really high-quality. 

The letters on the silkscreen are really well-printed and easy to read. Additionally, the solder sticks easily to the pads.

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. 

Download this code from my Github repository.

Code.





#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) {
  Serial.println("handleNewMessages");
  Serial.println(String(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", "");
      continue;
    }
    
    // Print the received message
    String text = bot.messages[i].text;
    Serial.println(text);

    String from_name = bot.messages[i].from_name;
    text.toUpperCase();   
    delay(10);

    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";
      digitalWrite(Relay1,HIGH);
    }
     if (text == "LIGHT1OFF") {
      bot.sendMessage(chat_id, "Light1 state set to OFF", "");
      Relay1State = "OFF";
      digitalWrite(Relay1,LOW);
    }
      if (text == "LIGHT2ON") {
      bot.sendMessage(chat_id, "Light2 state set to ON", "");
      Relay2State =  "ON";
      digitalWrite(Relay2,HIGH);
    }
     if (text == "LIGHT2OFF") {
      bot.sendMessage(chat_id, "Light2 state set to OFF", "");
      Relay2State ="OFF";
      digitalWrite(Relay2,LOW);
    }
      if (text == "LIGHT3ON") {
      bot.sendMessage(chat_id, "Light3 state set to ON", "");
      Relay3State =  "ON";
      digitalWrite(Relay3,HIGH);
    }
     if (text == "LIGHT3OFF") {
      bot.sendMessage(chat_id, "Light3 state set to OFF", "");
      Relay3State = "OFF";
      digitalWrite(Relay3,LOW);
    }
      if (text == "LIGHT4ON") {
      bot.sendMessage(chat_id, "Light4 state set to ON", "");
      Relay4State =  "ON";
      digitalWrite(Relay4,HIGH);
    }
     if (text == "LIGHT4OFF") {
      bot.sendMessage(chat_id, "Light4 state set to OFF", "");
      Relay4State = "OFF";
      digitalWrite(Relay4,LOW);
    }
      if (text == "LIGHT5ON") {
      bot.sendMessage(chat_id, "Light5 state set to ON", "");
      Relay5State = "ON";
      digitalWrite(Relay5,HIGH);
    }
     if (text == "LIGHT5OFF") {
      bot.sendMessage(chat_id, "Light5 state set to OFF", "");
      Relay5State ="OFF";
      digitalWrite(Relay5,LOW);
    }
///////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////

 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() {
  Serial.begin(115200);

  #ifdef ESP8266
    client.setInsecure();
  #endif

  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.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }
  // Print ESP32 Local IP Address
  Serial.println(WiFi.localIP());
}

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

    while(numNewMessages) {
      Serial.println("got response");
      handleNewMessages(numNewMessages);
      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.

#define CHAT_ID "XXXXXXXXXX"


Circuit Diagram for connection of bulbs.

Leave a Reply

Your email address will not be published.