Saturday, May 22, 2021

ESP8266 and ESP32 Sending messages to WhatsApp !!!

 For an index to all my stories click this text.

Have your ESP send messages to WhatsApp !!!

Please be aware that there is a small problem with the code in this story. The HTTPClient library has been changed. For an update on how to alter the program on this page look here : HTTPClient library update

As you know I have several ESP's running various IOT tasks in my home. Some are connected to my Domoticz home automation system and some are not. And what I want from some of these ESP's is to send me a message on my phone when something has happened. Something means somebody ringing my door, a door opening, a temperature rising too high etc.

I used to use IFTTT for that but since they limited their free service so much that it is not workable anymore I stopped using their service. Hey I am Dutch remember. I like things that are free.

So I was looking for an alternative to have my ESP's sending messages to my phone. Blynk certainly is an option and I already wrote some stories on that. And then I stumbled upon a great service that allows my ESP's sending messages to my WhatsApp account.

Whatsapp has a develloper program in which you can get access to their API's. That could be used. However this is only intended for corporations and not for private persons like me. Next to that there is a possibillity to have ESP's send messages to Whatsapp using Twillio. Twillio however is a paid service. And I am looking for a free service.

And out of the blue I found one. It is called CallMeBot. As this project demonstrates it works like a charm. There is only one small limitation. You are (at this moment) only allowed to send 25 messages to Whatsapp in 240 minutes. That is about one message per 10 minutes. For real life purposes that is enough. When intensively testing this might be a small problem.

CallMeBot

I presume that you already have installed WhatsApp on your phone, and are familiar with how it works. To send messages to WhatsApp from your ESP8266 or ESP32 you need to make use of a service called CallMeBot. And that is free !!! You can find their website here: https://www.callmebot.com/

CallMeBot uses a Bot which can be freely translated as a software robot. It operates as an intermediary between a microcontroller or computer or anything that can send a get-requests and WhatsApp. Sounds complicated but in reality it is not.

Activate the CallMeBot.

The first step is to add the Bot to your contacts list on your phone. Just do this the same way as you would do when adding a new person in your phone's contact list. You can give this contact any name you like. I gave mine, how surprising,  the name CallMeBot. Add the telephone number. You can find it at the CallMeBot webpage but I'll copy it here for you: +34 644 20 47 56

Edit: a reader pointed out to me that after I wrote this story CallMeBot had changed the telephone number in: +34 644 97 54 14

 
So please check the CallMeBot webpage for the actual phonenumber when you are reading this.
The reason why you need to do this is that this way the Bot will show up in your WhatsApp contacts.

So indeed now open WhatsApp and search in your contacts for CallMeBot or whatever name you chose.

Now you need to give the CallMeBot permission to send messages to you. This is done by sending the following message to the CallMeBot: I allow callmebot to send me messages. Please notice that in the message callmebot is spelled without capitals.



After a short while you will get a message back. This might take some time, depending on how busy the service is. When I activated the service it took almost 2 minutes before the Bot replied.

If you are wondering how I made this screenshot and how to copy/paste info from Whatsapp on my PC: Use the Whatsapp web version.

The reply consists actually of 2 messages. The first one is:

CallMeBot API Activated for +XXXXXXXXXXX
Your apikey is: YYYYYYY


The CallMeBot informs you that the Bot is activated for Phonenumber XXXXXXXXXXX which is your phonenumber, and that your API key is YYYYYYY.
Make sure to save the API key in a safe place. You will need it later on when we are going to send messages to Whatsapp.

You can now send messages using the API.
https://api.callmebot.com/whatsapp.php?phone=+XXXXXXXXXXXX&text=This+is+a+test&apikey=YYYYYY


This is the second message the CallMeBot sends. And this is a very important one as we are going to use the information in that message to build the ESP's software. Not only that we can use this for a first test.

First test

The great question off course always is: does it work. Well there is an easy way to test that.

Look at the second message CallMeBot send:

You can now send messages using the API.
https://api.callmebot.com/whatsapp.php?phone=+XXXXXXXXXXXX&text=This+is+a+test&apikey=YYYYYY


So, let us try that. Just paste this line in your browser and let's see what happens.



According to the webpage the message is send.



And yes there it is. The message was indeed send to the Callmebot contact in WhatsApp !!!! So this works. Next step is to get it working on the ESP.

Breadboard setup

As this is a test I am going to use a simple setup. I am going to attach a button to the ESP and thats all.



The button is attached with a pull-up resistor to D1 of the Wemos.

The ESP8266 program

I start with the complete listing and then get into the details.

// Simple trigger to send a message
// to Whatsapp 

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

const char* ssid = "YOUR-ROUTERS-NAME";
const char* password = "PASSWORD";

const int button = D1;
String tobesend;

void setup() 
  {
  pinMode(button, INPUT);
  
  Serial.begin(115200);
  delay(100);
    
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
    
  WiFi.begin(ssid, password);
    
  while (WiFi.status() != WL_CONNECTED) 
    {
      delay(500);
      Serial.print(".");
    }
  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());  
}

void loop() 
{
  if (digitalRead(button) == LOW)
    {
      Serial.print("connecting to ");
      Serial.println("whatsapp");


WiFiClient client;
HTTPClient http; //Declare an object of class HTTPClient //Specify request destination tobesend = "http://api.callmebot.com/whatsapp.php?"; tobesend = tobesend + "phone=+XXXXXXXXXXX"; tobesend = tobesend + "&text=The+button+on+the+ESP+was+pressed"; tobesend = tobesend + "&apikey=YYYYYY"; http.begin(client,tobesend); int httpCode = http.GET(); //Send the request if (httpCode > 0) { //Check the returning code String payload = http.getString(); //Get the request response payload Serial.println(payload); //Print the response payload } http.end(); //Close connection } }



As you can see it is just a normal program that starts the ESP8266Wifi library and the ESP8266HTTPClient library.

const char* ssid = "YOUR-ROUTERS-NAME";
const char* password = "PASSWORD";


Don't for get to put your routers credentials in here.

On the ESP8266 the button is attached to D1.
A string called tobesend is defined which will contain the URL that we are going to send to send a text to the CallMeBot.

In the setup nothing special either.
The button is defined as an INPUT, the serial port is opened and the wifi communication is started as we have done in many previous programs.

The loop is where the magic happens.

if (digitalRead(button) == LOW)

First we test if the button is pressed.

 HTTPClient http; //Declare an object of class HTTPClient

The HTTPClient library is activated and attached to http.

      tobesend = "http://api.callmebot.com/whatsapp.php?";
      tobesend = tobesend + "phone=+XXXXXXXXXXX";
      tobesend = tobesend + "&text=The+button+on+the+ESP+was+pressed";
      tobesend = tobesend + "&apikey=YYYYYY";

Remember from the beginning of this story that you can send messages to whatsapp by using the following line:
https://api.callmebot.com/whatsapp.php?phone=+XXXXXXXXXXXX&text=This+is+a+test&apikey=YYYYYY

Well here is the part where we build that line into the variable tobesend.
I splitted the line over multiple small lines to make it more useable in the program. Do not forget to alter the XXXXXXXX in your phone number and the YYYYYY in the apikey that was send to your whatsapp account.

I suppose you have noticed that the CallMeBot webpage showed that you need to use https (secure http). However the ESP is not up to that and luckily http works too !!!



As you can see it works flawless !!!!

The program is simple enough to alter to match your own needs. Attach a PIR to the ESP and have it send warnings when someone enters your room. Attach A Dallas DS18B20 and have the ESP send a warning when the temperature gets to high or to low. Use the filament sensor from this story: http://lucstechblog.blogspot.com/2020/05/filament-sensor-with-alarm-on-your-phone.html  and be warned if your 3D's printer filament is broken. There are hundreds of possibilities here.

Changing and expanding the message.

You can alter the text which has to be send in anything you like. There are however some rules for sending messages over http.

Spaces are not allowed. In our example we replaced the spaces with + which works just fine. You can also replace spaces with %20.

tobesend = tobesend + "&text=This%20is%20a%20test%0A";

This is how you would do that. The last part %0A is the newline character. This makes sure any text that follows is put on a new line.

Remember that you can only send text so any value that needs to be send to Whatsapp should be converted to a string.

temperature = 20;
tobesend = tobesend + String (temperature);


This is a valid way to convert a value to a String and add it to the text to be send.

ESP32

If you want to use the ESP32 to send messages to Whatsapp you need to make just small modifications to the program.

Just change:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>


Into :

#include <WiFi.h>
#include <HTTPClient.h>


And use a different pin for attaching the button.

const int button = 34;

I normally use IO pin 34 for connecting a button but you can use any IO pin you like.

Concluding.

CallMeBot is a great way to send messages from your ESP's to your phone. WhatsApp notifies you when a message is received so you can get up to date info on anything that is important to you.

I am really happy that this works !!!
As I use Whatsapp a lot on my phone I it this is a most welcome addition.

Just be aware of the limitation of 25 messages per 240 minutes. That is a bit more as one message per 10 minutes. The limitation seems an obstruction when testing intensively but in real life projects it should not be a problem.

I am sure you find many ways to use this.

Till next time
Have fun.


Luc Volders