Hey geeks, hope you are doing fine. In this article, we are going to make a very interesting project that is automation using a telegram bot. In this project, we are going to control three LEDs with the help of a telegram bot. Instead of LEDs, you can also connect other appliances as per your need. You can read the full article on our website.
Before starting please download the telegram application on your smartphones and make an account on it. Then go to the search bar and search for botfather and open the account with a blue tick.
Now click on start to initiate the bot and you find a menu.
Then click on the new bot to create a new project.
Now it will ask you to provide a suitable username and then a unique name to the bot you want to make. For the username use _bot at last as shown below. After this, you will get a unique token id which you have to copy. Then paste this id in the code. Then open your bot.
After opening type start and then it will reply to you with the message as shown below in the screenshot. Now send the commands on the app such as light on/off as per your need and the LEDs are turning on and off according to your actions. See the screenshots given below.
When you type RED LIGHT ON the red LED will turn on as shown below.
when you type GREEN LIGHT ON the green LED will turn on.
And if you type WHITE LIGHT ON the white LED will turn on.
- NodeMCU esp8266
- Three LEDs of different colors
- 220-ohm resistor
- Jumper wires and a breadboard
- USB cable for uploading the code
NOTE: Please upload the code which is given below to the NodeMCU.
//Techatronic.com
#include "CTBot.h"
CTBot myBot;
String ssid = "xxxxxxxxxxxx"; // REPLACE SSID WITH YOUR WIFI SSID
String pass = "xxxxxxxxxxxx"; // REPLACE Password YOUR WIFI PASSWORD, IF ANY
String token = "---------------------------------------------------"; // REPLACE Token WITH YOUR TELEGRAM BOT TOKEN
uint8_t greenled = 5;
uint8_t redled = 4;
uint8_t whiteled = 12;
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(redled, OUTPUT);
digitalWrite(redled, LOW);
pinMode(greenled, OUTPUT);
digitalWrite(greenled, LOW);
pinMode(whiteled, OUTPUT);
digitalWrite(whiteled, LOW);
}
void loop() {
// a variable to store telegram message data
TBMessage msg;
// if there is an incoming message...
if (myBot.getNewMessage(msg)) {
if (msg.text.equalsIgnoreCase("RED LIGHT ON")) { // if the received message is "LIGHT ON"...
digitalWrite(redled, HIGH); // turn on the LED
myBot.sendMessage(msg.sender.id, "RED Light is now ON"); // notify the sender
}
else if (msg.text.equalsIgnoreCase("RED LIGHT OFF")) { // if the received message is "LIGHT OFF"...
digitalWrite(redled, LOW); // turn off the led
myBot.sendMessage(msg.sender.id, "RED Light is now OFF"); // notify the sender
}
else if (msg.text.equalsIgnoreCase("GREEN LIGHT ON")){
digitalWrite(greenled, HIGH); // turn on the LED
myBot.sendMessage(msg.sender.id, "GREEN Light is now ON"); // notify the sender
}
else if (msg.text.equalsIgnoreCase("GREEN LIGHT OFF")){
digitalWrite(greenled, LOW); // turn off the led
myBot.sendMessage(msg.sender.id, "GREEN Light is now OFF"); // notify the sender
}
else if (msg.text.equalsIgnoreCase("White LIGHT ON")){
digitalWrite(whiteled, HIGH); // turn on the LED
myBot.sendMessage(msg.sender.id, "WHITE Light is now ON"); // notify the sender
}
else if (msg.text.equalsIgnoreCase("WHITE LIGHT OFF")){
digitalWrite(whiteled, LOW); // turn off the led
myBot.sendMessage(msg.sender.id, "WHITE Light is now OFF"); // notify the sender
}
else { // otherwise...
// generate the message for the sender
String reply;
reply = (String)"Welcome " + msg.sender.username + (String)". Try LIGHT ON or LIGHT OFF.";
myBot.sendMessage(msg.sender.id, reply); // and send it
}
}
// wait 500 milliseconds
delay(500);
}
We hope that you like this project. Also, check tutorials on Arduino and Raspberry Pi on our website.
HAPPY LEARNING!
Comments