// An example of bot that receives commands and turns on and off RGB LED
// Also sends back Hall_Effect Sensor Values of ESP32
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
// Initialize Wifi connection to the router
char ssid[] = "YOUR WIFI NETWORK"; // your network SSID (name)
char password[] = "YOUR NETWORK PASSWORD"; // your network key
// Initialize Telegram BOT
#define BOTtoken "THIS IS OBTAINED WHEN YOU REGISTER YOUR BOT - DO NOT SHARE PUBLICLY" // your Bot Token (Get from Botfather)
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
int Bot_mtbs = 1000; //mean time between scan messages
long Bot_lasttime; //last time messages' scan has been done
bool Start = false;
const int RedledPin = 18;
const int GreenledPin = 19;
const int BlueledPin = 21;
int val=0;
int RedledStatus = 0;
int GreenledStatus = 0;
int BlueledStatus = 0;
void handleNewMessages(int numNewMessages) {
Serial.println("handleNewMessages");
Serial.println(String(numNewMessages));
for (int i=0; i<numNewMessages; i++) {
String chat_id = String(bot.messages[i].chat_id);
String text = bot.messages[i].text;
String from_name = bot.messages[i].from_name;
if (from_name == "") from_name = "Guest";
if (text == "/Redledon") {digitalWrite(RedledPin, HIGH); RedledStatus = 1; bot.sendMessage(chat_id, "Red Led is ON", ""); }
if (text == "/Redledoff"){ RedledStatus = 0; digitalWrite(RedledPin, LOW); bot.sendMessage(chat_id, "Red Led is OFF", ""); }
if (text == "/Redstatus"){ if(RedledStatus){ bot.sendMessage(chat_id, "Red Led is ON", ""); } else { bot.sendMessage(chat_id, "Red Led is OFF", ""); } }
if (text == "/Greenledon") {digitalWrite(GreenledPin, HIGH); GreenledStatus = 1; bot.sendMessage(chat_id, "Green Led is ON", ""); }
if (text == "/Greenledoff"){ GreenledStatus = 0; digitalWrite(GreenledPin, LOW); bot.sendMessage(chat_id, "Green Led is OFF", ""); }
if (text == "/Greenstatus"){ if(GreenledStatus){ bot.sendMessage(chat_id, "Green Led is ON", ""); } else { bot.sendMessage(chat_id, "Green Led is OFF", ""); } }
if (text == "/Blueledon") {digitalWrite(BlueledPin, HIGH); BlueledStatus = 1; bot.sendMessage(chat_id, "Blue Led is ON", ""); }
if (text == "/Blueledoff"){ BlueledStatus = 0; digitalWrite(BlueledPin, LOW); bot.sendMessage(chat_id, "Blue Led is OFF", ""); }
if (text == "/Bluestatus"){ if(BlueledStatus){ bot.sendMessage(chat_id, "Green Led is ON", ""); } else { bot.sendMessage(chat_id, "Blue Led is OFF", ""); } }
if (text == "/start") {
String welcome = "Welcome to Universal Arduino Telegram Bot library.\n";
welcome += "This is Flash Led Bot example.\n\n";
welcome += "/Redledon :switch Red Led ON\n";
welcome += "/Redledoff :switch Red Led OFF\n";
welcome += "/Redstatus :status Red LED\n";
welcome += "/Greenledon :switch Green Led ON\n";
welcome += "/Greenledoff :switch Green Led OFF\n";
welcome += "/Greenstatus :status Green LED\n";
welcome += "/Blueledon :switch Blue Led ON\n";
welcome += "/Blueledoff :switch Blue Led OFF\n";
welcome += "/Bluestatus :status Blue LED\n";
bot.sendMessage(chat_id, welcome, "Markdown");
}
}
}
void setup() {
Serial.begin(115200);
// Attempt to connect to Wifi network:
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
// Set WiFi to station mode and disconnect from an AP if it was Previously
// connected
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
pinMode(RedledPin, OUTPUT); delay(10); digitalWrite(RedledPin, LOW);
pinMode(GreenledPin, OUTPUT); delay(10); digitalWrite(GreenledPin, LOW);
pinMode(BlueledPin, OUTPUT); delay(10); digitalWrite(BlueledPin, LOW);
}
void loop() {
if (millis() > Bot_lasttime + Bot_mtbs) {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
int val = hallRead(); Serial.println(val);
String chat_id = String(bot.messages[0].chat_id);
if (val>10){bot.sendMessage(chat_id, String(val), ""); RedledStatus = 0;GreenledStatus = 0;BlueledStatus = 0;digitalWrite(RedledPin, LOW); digitalWrite(GreenledPin, LOW); digitalWrite(BlueledPin, LOW);}
while(numNewMessages) {
Serial.println("got response");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
Bot_lasttime = millis();
}
}
|