Rafi Abdurachman
Published © GPL3+

Turn On And Off LED with Telegram bot

Turn On And Off LED with Telegram bot

BeginnerShowcase (no instructions)12 minutes619
Turn On And Off LED with Telegram bot

Things used in this project

Hardware components

LED (generic)
LED (generic)
×2
NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

The Code

C/C++
#include "CTBot.h"
CTBot myBot;
String ssid = "12345678";
String pass = "123456789";
String token = "1215549582:AAHhz3i87cb6AQD6WVM5-j4_SQ8uWxgRTVI";
uint8_t redLed = D1;
uint8_t greenLed = D2;

void setup() {
Serial.begin(115200);
Serial.println("Starting TelegramBot...");
myBot.wifiConnect(ssid, pass);
myBot.setTelegramToken(token);
if (myBot.testConnection())
Serial.println("\ntestConnection OK");
else
Serial.println("\ntestConnection NOK");
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
digitalWrite(redLed, LOW);
digitalWrite(greenLed, LOW);
}
void loop() {
TBMessage msg;
if (myBot.getNewMessage(msg)) {
if (msg.text.equalsIgnoreCase("Red Light on")) {
digitalWrite(redLed, HIGH);
myBot.sendMessage(msg.sender.id, "Red Light is now ON");
}
else if (msg.text.equalsIgnoreCase("Red Light off")) {
digitalWrite(redLed, LOW);

myBot.sendMessage(msg.sender.id, "Red Light is now OFF");
}

if (msg.text.equalsIgnoreCase("Green Light on")) {
digitalWrite(greenLed, HIGH);
myBot.sendMessage(msg.sender.id, "Green Light is now ON");
}
else if (msg.text.equalsIgnoreCase("Green Light off")) {
digitalWrite(greenLed, LOW);
myBot.sendMessage(msg.sender.id, "Green Light is now OFF");
}

else {
String reply;
reply = (String)"Welcome " + msg.sender.username + (String)". Try LIGHT ON or LIGHT OFF.";

myBot.sendMessage(msg.sender.id, reply);
}
}
delay(50);
}

Credits

Rafi Abdurachman
5 projects • 1 follower
Contact

Comments

Please log in or sign up to comment.