Latest Updates

random

Home Automation using ESP8266 and Telegram

In this project we are going to perform home automation using smartphone telegram app

BOM:
  1. NodeMCU
  2. Relay
  3. Jumper wire
  4. Telegram
Hardware circuit: 

Relay Module
NodeMCU
GND
GND
VCC
VCC
DIN
GPIO5 (D1)

Steps: -
1) Include Telegram bot library:
Need telegram bot library to integrate with telegram app

2) Setup telegram app:
So here we need to Install telegram app on your phone and search for botfather. Through botfather create new bot.  You will get an API token from botfather, which is the authentication key to connect to Telegram API.

  • After installation of telegram app search for botfather channel and follow instruction
  • Create new bot type /newbot and then generate name for your bot as following



Finally, you will get the api token key to integrate esp8266 with telegram.

Source code:

Source Code Explanation:

1) Include all the necessary libraries to connect with telegram and esp8266 wifi library
  • #include <ESP8266WiFi.h>
  • #include <WiFiClientSecure.h>
  • #include <UniversalTelegramBot.h>
2) Define network credentials
  • char ssid[] = "YOUR_WIFI_SSID";     
    char password[] = "YOUR_PASSWORD"; 
3) Define telegram bot token id
  • #define BOTtoken "YOUR_TELEGRAM_TOKEN"
4) Initialize the telegrambot, wificlient, and telegram bot time variable
  • WiFiClientSecure client;
    UniversalTelegramBot bot(BOTtoken, client);
    int Bot_mtbs = 1000;
    long Bot_lasttime;
    bool Start = false;
5) Check for new message and status of message according to input string appropriate action will be performed by esp8266
  • void handleNewMessages(int numNewMessages)
    {
      Serial.print("NewMessages ");
      Serial.println(String(numNewMessages));

      for (int i=0; i  {
        String chat_id = String(bot.messages[i].chat_id);
        String text = bot.messages[i].text;

        String from_name = bot.messages[i].from_name;
        if (from_name == "") from_name = "Guest";

        if (text == "test")
          {
            bot.sendMessage(chat_id, "Hello");
          }
        if (text == "lamp on")
          {
            bot.sendMessage(chat_id, "Lamp ON");
            Serial.println("Lamp ON");
            digitalWrite(16, 0); 
          }
         if (text == "lamp off")
          {
            bot.sendMessage(chat_id, "Lamp OFF");
            Serial.println("Lamp Off");
            digitalWrite(16, 1);
          }
      }
    }
6) Status function will initialize the necessary setting serial monitor and wifi network connection

7) In the loop() check for the new arrival message and time stamp of last message and new arrived message according to that perform task repeatedly

  • void loop()
    {
      if (millis() > Bot_lasttime + Bot_mtbs)
      {
        int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
        while(numNewMessages)
        {
          Serial.println("");
          handleNewMessages(numNewMessages);
          numNewMessages = bot.getUpdates(bot.last_message_received + 1);
        }
        Bot_lasttime = millis();
      }
    }
If you are still facing issue regarding this topic Feel free to Ask Doubts in the Comment Box Below and Don't Forget to Follow us on 👍 Social Networks😉
Home Automation using ESP8266 and Telegram Reviewed by Avotrix.Author on Tuesday, July 07, 2020 Rating: 5

1 comment:

  1. I agree with a lot of the points you made in this article. If you are looking for the Home Automation Hyderabad, then visit swifthomeauomation. I appreciate the work you have put into this and hope you continue writing on this subject.

    ReplyDelete

All Rights Reserved by Avotrix © 2018 - 2019
Developed and created by Avotrix

Contact Form

Name

Email *

Message *

Powered by Blogger.