Home Automation using Arduino, ESP8266 (Nodemcu) and Telegram

Nowadays, Home automation is the trending and the emerging era of IoT (Internet of Things). Everyone tries to automate home in some manner then that can be remote controlled or manual. And which makes life easier for them.

There are many techniques to control home appliances remotely.

In this article, we will see how to control esp8266 using telegram. You can even control the ESP32 with little modification.
Bots are third-party applications that run inside Telegram application. Users can interact with bots by sending them messages, commands or media. You control your bots using HTTPS requests to Telegram Bot API.

So here we will create the telegram bot first which convert the message into HTTPS request and it will send the request to Nodemcu. In case of response, Bot will will extract the message from HTTPS request.

Create Telegram bot

Follow the following steps to create Telegram bot,

  1. Search for the BotFather, and Click on Start/Restart

2. Click on newbot command as follows

3. Give Some name to newly created bot and also give unique name to bot as follows

4. Copy the token from the message we received from botfather

5. Open the newly created bot from the link we received in the same message

Hardware Connection using Nodemcu

As per the connection, we are using D0 and D1 of the nodemcu to control the relay. But in the code we are using custom designed 4-Channel relay with ESP8266 on board. So, pins used in the code as per the board we designed.

Schematic of the 4 Channel ESP8266 board

You can also get the schematic and gerber files on github.

I have designed this board using Kicad software which is open source and easy to use.

Code

#include "CTBot.h"
CTBot myBot;

#define LIGHT_PIN1 5  
#define LIGHT_PIN2 12
#define LIGHT_PIN3 4  
#define LIGHT_PIN4 13 

String ssid = "stechiez";     
String pass = "qwerty123";
String token = "1321031443:AAG289f4etk7m8ujtmHuDuBFwTg_giNSi_A";   // REPLACE myToken WITH YOUR TELEGRAM BOT TOKEN
uint8_t led = 2;          


void setup() {
	// initialize the Serial
	Serial.begin(115200);
	Serial.println("Starting TelegramBot...");

	// connect the ESP8266 to the desired access point
	myBot.wifiConnect(ssid, pass);

	// set the telegram bot token
	myBot.setTelegramToken(token);

	// check if all things are ok
	if (myBot.testConnection())
		Serial.println("\ntestConnection OK");
	else
		Serial.println("\ntestConnection NOK");

	// set the pin connected to the LED to act as output pin
	pinMode(led, OUTPUT);
	digitalWrite(led, HIGH); // turn off the led (inverted logic!)

  //Set mode top Output for the following pins
	pinMode(LIGHT_PIN1, OUTPUT);
  pinMode(LIGHT_PIN2, OUTPUT);
  pinMode(LIGHT_PIN3, OUTPUT);
  pinMode(LIGHT_PIN4, OUTPUT);



}

void loop() {
	// a variable to store telegram message data
	TBMessage msg;

	// if there is an incoming message...
	if (myBot.getNewMessage(msg)) {

		if (msg.text.equals("/hall_light_on")) {          
			digitalWrite(LIGHT_PIN3, HIGH);                             
			myBot.sendMessage(msg.sender.id, "Hall Light is now ON");
		}
    else if (msg.text.equals("/hall_light_off")) {    
      digitalWrite(LIGHT_PIN3, LOW);                           
      myBot.sendMessage(msg.sender.id, "Hall Light is now OFF");
    }
    else if (msg.text.equals("/gallery_light_on")) {    
      digitalWrite(LIGHT_PIN4, HIGH);                           
      myBot.sendMessage(msg.sender.id, "Gallary Light is now OFF");
    }
    else if (msg.text.equals("/gallery_light_off")) {    
      digitalWrite(LIGHT_PIN4, LOW);                           
      myBot.sendMessage(msg.sender.id, "Gallary Light is now OFF");
    }
    
    else if (msg.text.equals("/helpme")) {    
      digitalWrite(led, HIGH);                           
      myBot.sendMessage(msg.sender.id, "Try sending following commands\n");
      myBot.sendMessage(msg.sender.id, "/hall_light_on\n");
      myBot.sendMessage(msg.sender.id, "/hall_light_off\n");
      myBot.sendMessage(msg.sender.id, "/gallery_light_on\n");
      myBot.sendMessage(msg.sender.id, "/gallery_light_off\n");
      myBot.sendMessage(msg.sender.id, "/helpme\n");
      myBot.sendMessage(msg.sender.id, "/nothing\n");
    }   
		else {                                                 
			// generate the message for the sender
			String reply;
			reply = (String)"Welcome " + msg.sender.username + (String)"try /helpme";
			myBot.sendMessage(msg.sender.id, reply);       
		}
	}
	// wait 500 milliseconds
	delay(500);
}

Tutorial

Leave a Reply

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