Pages

Friday, February 2, 2024

ESPEAsy part 5: ESPEasy to Telegram

For an index to all my stories click this text

After my previous stories on ESPEasy I received a few mails from readers that wanted to know if it is possible to send data direct from ESPEasy to Telegram. The advantage in that is that you can send alarm notifications direct to your phone.

If you do not know what ESPEasy is I urge to at least read my introduction tutorial and the tutorial about communication between two ESP's with ESPEasy. You will be amazed about the amount of features and drivers for sensors are crammed in this alternative operating system for the ESP8266 and ESP32. You can find these stories here:
http://lucstechblog.blogspot.com/2023/10/espeasy-part-1-iot-without-programming.html

http://lucstechblog.blogspot.com/2023/12/espeasy-part2-send-data-from-esp-to-esp.html

I also did a story on how to send data from ESPEasy to WhatsApp.
http://lucstechblog.blogspot.com/2024/01/espeasy-part-4-espeasy-send-data-to.html

Some readers pointed out to me that sending data from ESPEasy to Telegram should be easier as Telegram can communicate through a Bot. Well actually that is true and also not true.
Indeed Telegram can communicate through a Bot however that communication is done over HTTPS (secure HTTP) and ESPEasy does not (yet ???) support HTTPS.
The ESPEasy developers have stated that HTTPS communication requires a lot of memory and memory is scarce in ESPEasy as it is already crammed with features.

So as HTTPS communication is (as for now) out of the question rests just one other option at this moment and that is sending data to Telegram using the CallMeBot service.

CallMeBot

CallMeBot is a free service that is especially created to have electronic devices like the ESP8266 and ESP32 or programs send messages to WhatsApp, Telegram, Signal and Facebook. You can find it's website here: https://www.callmebot.com

We have to use the CallMeBot API to send messages from ESPEasy to CallMeBot. In a previous story I showed how to send data from an ESP8266 to WhatsApp and that explained how this works. Please re-read this story before you go on. You can re-read it here: http://lucstechblog.blogspot.com/2021/05/esp8266-and-esp32-sending-messages-to.html

That story showed that to send a message to WhatsApp through CallMeBot we have to use the following format:
http://api.callmebot.com/whatsapp.php?phone=+XXXXXXXXXXXX&text=This+is+a+test&apikey=YYYYYY

For Telegram there is a similar command:

https://api.callmebot.com/text.php?user=@myusername&text=This+is+a+test+from+CallMeBot

You can see the difference. CallMeBot does not sends the message to a Phone number but to your username. So start with setting your username in Telegram if you have not done so yet.



On your phone in the main Telegram menu click the hamburger icon at the top left of your screen. And then click General Settings.



Look at your profile settings and give yourself a username. Choose it carefully as all your contacts can use this username to send messages to you.

When done open your webbrowser to the CallMeBot webpage: https://www.callmebot.com/



Choose in the drop down menu Send Text Messages.



On this page you will find the explanation on how to use this service. And you can see the api call you need to use to send data to Telegram. As shown before in this text the api call looks like this:

https://api.callmebot.com/text.php?user=@myusername&text=This+is+a+test+from+CallMeBot

There is a green button on the page that says "Try to Send a test message now!" Click that button.



Fill in your username and your Text Message to Send and at the bottom you can see what URL you need to use to send the message.


Before the message is send a new screen opens which asks for a confirmation and Authentication. Press the green button and the message will be send to Telegram on your phone.



A confirmation message will be send to your phone. The above message is in Dutch as my phone is set into Dutch.



Now lets try a test message. Send the text First test.



And if all went well the message will appear in Telegram on your phone.

Breadboard


For this simple test we will only use a switch connected to D5 on our Wemos D1 Mini.

ESPEasy device setup.

I Will not go into all the details about installing the software on your ESP8266. That has been covered before on this weblog. Please re-read the stories in the following links for that:
http://lucstechblog.blogspot.com/2023/10/espeasy-part-1-iot-without-programming.html

To use the button on the breadboard got to the devices tab. Press Add and choose an input switch as the device. This also has been covered in the pre-mentioned stories. So re-read those for the details.



First let's have a look at the upper part of the devices screen.

The switch is called sw1 and it is enabled. We use the internal pull-up resistor of the Wemos D1 so check this button. The button on the breadboard has been attached to D5 which is GP14. The type is a switch and it is active when pressed so Push Button Active Low.



At the bottom make sure that Send to Controller 1 is checked and rename the variable (Values) to sw1val

ESPEasy Controller setup

The controller is a software driver that actually sends the data to another device or webservice. This is explained in the before mentioned previous stories so please re-read those for detailed information.

So switch over to the Controller Tab and Add a new Controller. As we are going to send over HTTP choose Generic HTTP as the protocol.



To locate the controller we do not use the IP adress but Use Hostname. And the Controller Hostname is api.callmebot.com
As we are sending data over HTTP the port number is 80.

We are sending data and commands to a service outside our own network so it might take a second or two to get a confirmation or acknowledgement. Therefore set the Client Timeout to 1000 ms (1 second)

The only thing left to do is to set the text we are going to publish. We have seen that the general command is:
https://api.callmebot.com/text.php?user=@myusername&text=This+is+a+test+from+CallMeBot
The hostname already has been set so we need to edit the text:
text.php?user=@myusername&text=This+is+a+test+from+CallMeBot

Replace user=@myusername with the username you have chosen for yourself.

The text we are going to send is  an indication of what the data is and the data itself. The indication is simply button= and the data from the button is found in sw1#sw1val.
So the complete call will be:

text.php?user=@myusername&text=button+=+[sw1#sw1val]

Make sure to have enabled checked otherwise the Controller is not activated and then chose Submit.

Done !!!

Press the button attached to the ESP8266 and



There you are. We just send some messages to Telegram from ESPEasy.

You can have multiple data in one command and put each of them on a new line using %0A. Here is an example that would send the data from two buttons:

text.php?user=@myusername&text=button+=+[sw1#sw1val]+%0A+button2+=+[sw2#sw2val]



And here is the result.

That's it for now. Till next time
Have fun.

Luc Volders