ESP32: Send Messages to WhatsAppCallMeBot WhatsApp API

To send out a message making use of the CallMeBot API you require to make a POST request to the complying with URL (but utilizing your info):

ESP32 Send Messages to WhatsApp Arduino IDE

For the official documents, you can examine the following link:

Introducing WhatsApp

WhatsApp logo

As we’ve seen formerly, the message to be sent out demands to be URL inscribed.

First, include the required collections:

CallMeBot WhatsApp API

Insert your network qualifications on the adhering to variables:

Insert your phone number and API secret.

Create and begin an HTTPClient on that URL:

Getting the CallMeBot API KEY

Specify the web content kind:

  1. Add the phone number +34 644 51 95 23 to your Phone Contacts. (Name it as you wish);
  2. Send the following message: “I allow callmebot to send me messages” to the new Contact created (using WhatsApp of course);
  3. Wait until you receive the message “API Activated for your phone number. Your APIKEY is XXXXXX” from the bot.
Get CallMeBot API key

Finally, send the HTTP blog post request.

The WhatsApp message from the robot will have the API crucial required to send messages making use of the API

CallMeBot API

To send a message using the CallMeBot API you require to make a POST demand to the complying with URL (but using your info):

https://api.callmebot.com/whatsapp.php?phone=[phone_number]&text=[message]&apikey=[your_apikey]
  • [phone_number]: phone number associated with your WhatsApp account in 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.

For the main paperwork, you can check the adhering to web link:

Installing the URLEncode Library

As we’ve seen formerly, the message to be sent out demands to be URL encoded.

Sending a message to WhatsApp using the CallMeBot API is extremely straightforward.

First, consist of the necessary libraries:

Install URL Encode Library Arduino IDE

Sending Messages to WhatsApp – ESP32 Code

Insert your network credentials on the complying with variables:

/* 
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp32-send-messages-whatsapp/
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.
  
  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*/

#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 = "https://api.callmebot.com/whatsapp.php?phone=" + phoneNumber + "&apikey=" + apiKey + "&text=" + urlEncode(message);    
  HTTPClient http;
  http.begin(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() {
  
}

Insert your phone number and API key.

How the Code Works

Start an httpclient and produce on that URL:

Specify the content kind:

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

Finally, send the HTTP message request.

const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

We additionally have tutorials for various other kinds of messages (e-mail and Telegram messages):

String phoneNumber = "REPLACE_WITH_YOUR_PHONE_NUMBER";
String apiKey = "REPLACE_WITH_YOUR_API_KEY";

sendMessage()

We hope you discover this tutorial valuable.

void sendMessage(String message){

Inside the function, we prepare the URL for the request with your information, phone number, API key, and message.

As we’ve seen previously, the message needs to be URL encoded. We’ve included the UrlEncode library to do that. It contains a function called urlEncode() that encodes whatever message we pass as argument (urlEncode(message)).

String url = "https://api.callmebot.com/whatsapp.php?phone=" + phoneNumber + "&apikey=" + apiKey + "&text=" + urlEncode(message);

Create and start an HTTPClient on that URL:

HTTPClient http;
http.begin(url);

Specify the content type:

// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");

Finally, send the HTTP post request. The following line sends the request and saves the response code:

int httpResponseCode = http.POST(url);

If the response code is 200, it means the post request was successful. Otherwise, something went wrong.

// 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);
}

Finally, free up the resources:

// Free resources
http.end();

setup()

In the setup(), initialize the Serial Monitor for debugging purposes.

Serial.begin(115200);

Connect to your local network and print the board IP address.

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());

Then, we can send a message to WhatsApp by simply calling the sendMessage() function. In this case, we’re sending the message Hello from ESP32!

// Send Message to WhatsAPP
sendMessage("Hello from ESP32!");

Demonstration

After inserting your network credentials, phone number and API key, you can upload the code to your board.

After uploading, open the Serial Monitor at a baud rate of 115200 and press the board RST button. It should successfully connect to your network and send the message to WhatsApp.

ESP Send Message to WhatsApp Serial Monitor

Go to your WhatsApp account. After a few seconds, you should receive the ESP32 message.

WhatsApp Receive Message from ESP32

Wrapping Up

In this tutorial, you learned how to use the CallMeBot API with the ESP32 to send messages to your WhatsApp account. This can be useful to send sensor readings regularly to your inbox, send a notification when motion is detected, send an alert message when a sensor reading is above or below a certain threshold, and many other applications.

We also have tutorials for other types of messages (email and Telegram messages):

We hope you find this tutorial useful.

Learn more about the ESP32 with our resources:

Thanks for reading.