Experimentation's with a TelegramBot polling an ESP32.
These Bots can be found or created via the Telegram App Messaging service...
(in our case I have called it "MRLbot" )
MRLbot & ESP32 Marriage possibilities :-
- Send Text Messages remotely (both ways)
- Send Pictures .... (both ways, using Spiffs or SD card)
- Send Controls and Status messages (both ways)
- Control GPIO pins both Input&output
Creation of a Bot is achieved Via an entity/user called "Botfather" who you can find by searching persons in the search bar of the Telegram App (its simple to set up and process/guidance is well documented).
Once the TelegramBot is created the ESP32 needs the Telegram Library (I recommend Brian Lough's)
During the Bot registration process you will be sent a BOTtoken which is unique to your Bot and should be kept safe .... unless you would want multiple persons to use it !!!
Here is some code for controlling 3 Led's and sending Hall_Effect Sensor values back |
// An example of bot that receives commands and turns on and off RGB LED // Initialize Wifi connection to the router // Initialize Telegram BOT WiFiClientSecure client; int Bot_mtbs = 1000; //mean time between scan messages const int RedledPin = 18; int RedledStatus = 0; void handleNewMessages(int numNewMessages) { for (int i=0; i<numNewMessages; i++) { String from_name = bot.messages[i].from_name; if (text == "/Redledon") {digitalWrite(RedledPin, HIGH); RedledStatus = 1; bot.sendMessage(chat_id, "Red Led is ON", ""); } if (text == "/start") {
void setup() { // Attempt to connect to Wifi network: // Set WiFi to station mode and disconnect from an AP if it was Previously while (WiFi.status() != WL_CONNECTED) { Serial.println(""); pinMode(RedledPin, OUTPUT); delay(10); digitalWrite(RedledPin, LOW); void loop() { numNewMessages = bot.getUpdates(bot.last_message_received + 1); Bot_lasttime = millis(); |
Once uploaded the ESP32 will poll the Telegram server at intervals (in our case every second (Circa))
Then on your Phone/PC or Tablet via the Telegram App you can bring up the MRLbot and interact with it.
I have extended the Command list to be able to control a RGB led.
Typing "/start" brings up the list of commands
A while back I was using the ESP32's inbuilt Magnetic Hall Effect Sensor... this can be read and also the analoge values sent back to your Telegram Chat.
(Could easily be used as a simple intruder alert (Magnet on door) ;-)
Another neat parameter is the ability to include some json formating for custom keyboard buttons :-
In this example you can store a list of URL pictures on your ESP and recall them via the Telegram App
String test_photo_url = "http://myrobotlab.org/sites/default/files/logo.png";
if (text == "/get_test_photo") { bot.sendPhoto(chat_id, test_photo_url, ""); }
I am awaiting some more ESP32_Cams to be able to send Polled camera picture back to my Chat.,, Stay Tuned.
(N.B. Telegram gives you a LOT of control over what happens to your messages, tons more control than others that will remain nameless).