Hackster is hosting Impact Spotlights: Robotics. Watch the stream live on Thursday!Hackster is hosting Impact Spotlights: Robotics. Stream on Thursday!
Techatronic
Published

Telegram Bot using NodeMCU | esp8266 Automation

This is an automation-based project in which we are controlling the appliances using a Telegram bot.

IntermediateFull instructions provided2 hours2,471
Telegram Bot using NodeMCU | esp8266 Automation

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
Resistor 220 ohm
Resistor 220 ohm
×1
5 mm LED: Red
5 mm LED: Red
×1
5 mm LED: Green
5 mm LED: Green
×1
High Brightness LED, White
High Brightness LED, White
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×1
USB-A to Mini-USB Cable
USB-A to Mini-USB Cable
×1
Arduino UNO
Arduino UNO
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Code snippet #1

Plain text
 //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);  
 }  

Code snippet #2

Plain text
 //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);  
 }  

Github

https://github.com/shurillu/CTBot

Credits

Techatronic
127 projects • 143 followers
Electronic engineer

Comments