In this tutorial, you’ll discover how to use the ESP32 NodeMCU board to send messages to your WhatsApp account. This can be helpful for various applications, including receiving alerts from the ESP32 with sensor readings, alert messages when a sensor reading is over or below a predetermined threshold, and more. The Arduino IDE will be used to program the ESP32, and the free CallMeBot API will be used to send the messages.
“WhatsApp Messenger, or simply WhatsApp, is an American cross-platform, freeware, centralized instant messaging and voice-over-IP service owned by Meta Platforms that is available internationally.” You can avoid paying SMS fees by using your phone’s internet connection to send texts. WhatsApp is free and accessible on iOS and Android devices. If you don’t already have WhatsApp on your smartphone, get it now.
Stage 1: Initial Setting UP CallmeBOT API for WhatsApp- ESP32 Communication
We’ll use a free API service called CallMeBot to send messages to your WhatsApp account with the ESP32. You must first obtain the CallmeBot WhatsApp API key before you can begin using the API. Follow the steps below.
Refer this for initial setup : Link
Step 1: Add the phone number +34 611 04 87 48 into your Phone Contacts. (Name it it as you wish)
Step 2: Send this message “I allow callmebot to send me messages” to the new Contact created (using WhatsApp of course)
Step 3: Wait until you receive the message “API Activated for your phone number. Your APIKEY is 123123” from the bot.
Note: If you don’t receive the ApiKey in 2 minutes, please try again after 24hs.
Step 4: The WhatsApp message from the bot will contain the apikey needed to send messages using the API. You can send text messages using the API after receiving the confirmation.
Stage 2 : CALLMeBOT API
To send a message via the CallMeBot API, make a POST request to the URL below.
https://api.callmebot.com/whatsapp.php?phone=[phone_number]&text=[message]&apikey=[your_apikey]
[phone_number] International format
[message] the message to be sent, should be URL encoded.
[your_apikey]: the API key you received during the activation process in the previous section.
Stage 3:Installing the URLEncode Library
As previously demonstrated, the message to be sent must be URL encoded. URL encoding transforms characters into a format suitable for transmission over the Internet. Only the ASCII character set can be used to send URLs over the Internet.
You can either encode the message yourself or use a library, which is much easier. We’ll be using the UrlEncode library, which can be found in the Arduino IDE.
Search for the URLEncode library in Sketch > Include Library > Manage Libraries.
ESP32 Code for Sending Messages to whatsapp
When the ESP32 first boots, the following example code sends a message to your WhatsApp account. This is a basic example of how to send messages. After you understand how it works, you can incorporate it into your own projects.
#include <WiFi.h>
#include <HTTPClient.h>
#include <UrlEncode.h>
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
// +international_country_code + phone number
// Portugal +351, example: +351912345678
String phoneNumber = "REPLACE_WITH_YOUR_PHONE_NUMBER";
String apiKey = "REPLACE_WITH_API_KEY";
void sendMessage(String message){
// Data to send with HTTP POST
String url = "http://api.callmebot.com/whatsapp.php?phone=" + phoneNumber + "&apikey=" + apiKey + "&text=" + urlEncode(message);
WiFiClient client;
HTTPClient http;
http.begin(client, url);
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Send HTTP POST request
int httpResponseCode = http.POST(url);
if (httpResponseCode == 200){
Serial.print("Message sent successfully");
}
else{
Serial.println("Error sending the message");
Serial.print("HTTP response code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
// Send Message to WhatsAPP
sendMessage("Hello from ESP32!");
}
void loop() {
}