Razaq Nawfal
Published © GPL3+

LED with Telegram

This is coding

BeginnerProtip1 hour178
LED with Telegram

Things used in this project

Hardware components

LED (generic)
LED (generic)
×2
Female/Female Jumper Wires
Female/Female Jumper Wires
×4
NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1

Story

Read more

Code

LED_TELE

C/C++
// Download CTBot and TelegramBot in your Arduino libraries
// Contact @BotFather to get the token

#include "CTBot.h"
  CTBot myBot;

  String ssid = ""; // Your WiFi name
  String pass = ""; // You WiFi password
  String token = ""; // You will get token in Telegram Bot
  uint8_t redLed = D1;
  uint8_t greenLed = D2;

void setup() {
Serial.begin(115200);
Serial.println("Starting TelegramBot...");
myBot.wifiConnect(ssid, pass);
myBot.setTelegramToken(token);
// Check if all things are ok
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 on")) {
digitalWrite(redLed, HIGH);
myBot.sendMessage(msg.sender.id, "Red Light is now ON");
}
else if (msg.text.equalsIgnoreCase("Red off")) {
digitalWrite(redLed, LOW); 
myBot.sendMessage(msg.sender.id, "Red Light is now OFF");
}
 if (msg.text.equalsIgnoreCase("Green on")) {
 digitalWrite(greenLed, HIGH);
 myBot.sendMessage(msg.sender.id, "Green Light is now ON");
 }
 else if (msg.text.equalsIgnoreCase("Green 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

Abdur Razaq

Posted by Razaq Nawfal

Comments