In this project, you’ll learn how to send messages to your WhatsApp account using an ESP32 from anywhere using a SIM card with a data plan. We’ll use the LILYGO T-SIM7000G ESP32 that combines the ESP32 chip, the SIM7000G module, a microSD card slot, a battery holder, and a charger on the same board.
We’ll show you how to connect it to the internet using your SIM card data plan and make an HTTPS GET request to a free API called CallMeBot to send WhatsApp messages.
Compatibility
This board supports 2G, LTE CAT-M1, and NB-IoT protocols. You can go to the following links to check if any of these protocols are supported in your country:
Project Overview
Here’s a high-level overview of how the project works:
- You have a LILYGO T-SIM7000G ESP32 board that has a SIM card with a data plan that can be connected to the internet;
- When the LILYGO T-SIM7000G ESP32 first boots, it establishes an internet connection and makes an HTTPS GET request to the CallMeBot API;
- Then, the CallMeBot API sends the message content to your WhatsApp account;
- Finally, you receive the message in your WhatsApp.
Introducing the LILYGO T-SIM7000G ESP32
The LILYGO T-SIM7000G is an ESP32 development board with a SIM7000G chip. This adds GPS, GPRS, LTE CAT-M1, and NB-IoT protocols to your board. This means that with this board you can send SMS, get location and time using GPS, and connect it to the internet using a SIM card data plan. You can find more information by reading our in-depth guide.
Where to buy the LILYGO T-SIM7000G ESP32?
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!
Preparing the LILYGO T-SIM7000G ESP32 Board
Compatibility
This board supports 2G, LTE CAT-M1, and NB-IoT protocols. You can go to the following links to check if any of these protocols are supported in your country:
Before proceeding, you need to follow the next two steps:
1. Insert a nano SIM card into your board;
2. Connect the Full Band LTE antenna to the correct antenna port as illustrated in the following image:
APN Details
To connect your SIM card to the internet, you need to have your phone plan provider’s APN details. You need the domain name, username, and password.
In my case, I’m using Vodafone Portugal. If you search for “GPRS APN settings” followed by your phone plan provider name, (in my case: “GPRS APN Vodafone Portugal”), you can usually find in a forum or their website all the information that you need.
It might be a bit tricky to find the details if you don’t use a well-known provider. So, you might need to contact them directly.
Introducing WhatsApp
“WhatsApp Messenger, or simply WhatsApp, is an internationally available American freeware, cross-platform centralized instant messaging and voice-over-IP service owned by Meta Platforms.” It allows you to send messages using your phone’s internet connection, so you can avoid SMS fees.
WhatsApp is free and is available for Android and iOS. Install WhatsApp on your smartphone if you don’t have it already.
CallMeBot WhatsApp API
To send messages to your WhatsApp account with the ESP32, we’ll use a free API service called CallMeBot service. You can learn more about CallMeBot at the following link:
Basically, it works as a gateway that allows you to send a message to yourself. This can be useful to send alert messages from the ESP32.
All the information about how to send messages using the API, can be found here.
Getting the CallMeBot API KEY
Before starting to use the API, you need to get the CallmeBot WhatsApp API key. Follow the next instructions.
- Add the phone number +34 621 331 709 to your Phone Contacts. (Name it as you wish) — please double-check the number on the CallMeBot website, because it sometimes changes.
- Send the following message: “I allow callmebot to send me messages” to the new Contact created (using WhatsApp of course);
- Wait until you receive the message “API Activated for your phone number. Your APIKEY is XXXXXX” from the bot.
Note: if you don’t receive the API key in 2 minutes, please try again after 24hs or double-check the CallMeBot phone number here. The WhatsApp message from the bot will contain the API key needed to send messages using the API
CallMeBot API
To send a message using the CallMeBot API you need to make a request to the following URL (but using your information):
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.
You can learn more by reading the official documentation.
Libraries
The ESP32 communicates with the SIM7000G chip by sending AT commands via serial communication. You don’t need a library, you can simply establish a serial communication with the module and start sending AT commands.
However, it might be more practical to use a library. For example, the TinyGSM library knows which commands to send, and how to handle AT responses, and wraps that into the standard Arduino Client interface—that’s the library we’ll use in this tutorial.
Installing the TinyGSM Library
Open your Arduino IDE and go to Sketch > Include Library > Manage Libraries. The Library Manager should open. Search for TinyGSM. Select the TinyGSM library by Volodymyr Shymanskyy.
Installing the ArduinoHttpClient Library
You also need to install the ArduinoHttpClient library to make HTTPS requests. Search for ArduinoHttpClient and install it.
Learn more about HTTPS requests with the ESP32: ESP32 HTTPS Requests (Arduino IDE)
Installing the UrlEncode Library
Finally, install the UrlEncode library to encode your messages. Search for UrlEncode and install it.
Sending Messages using SIM Card to WhatsApp – LILYGO T-SIM7000G ESP32 Code
The following example code sends a message to your WhatsApp account when the ESP32 first boots. This is a simple example to show you how to send messages. After understanding how it works, the idea is to incorporate it into your projects to send notifications or useful data.
- Copy the following code to your Arduino IDE.
/*
Rui Santos & Sara Santos - Random Nerd Tutorials
Complete project details at https://RandomNerdTutorials.com/esp32-send-messages-whatsapp-sim7000g/
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. Based on the library example: github.com/vshymanskyy/TinyGSM/blob/master/examples/HttpsClient/HttpsClient.ino
*/
// Select your modem
#define TINY_GSM_MODEM_SIM7000SSL
#define TINY_GSM_RX_BUFFER 1024 // Set RX buffer to 1Kb
// Set serial for debug console (to the Serial Monitor, default speed 115200)
#define SerialMon Serial
#define SerialAT Serial1
#include <TinyGsmClient.h>
#include <ArduinoHttpClient.h>
#include <UrlEncode.h>
// Define the serial console for debug prints, if needed
#define TINY_GSM_DEBUG SerialMon
// #define LOGGING // <- Logging is for the HTTP library
// Add a reception delay, if needed.
// This may be needed for a fast processor at a slow baud rate.
// #define TINY_GSM_YIELD() { delay(2); }
// set GSM PIN, if any
#define GSM_PIN ""
// flag to force SSL client authentication, if needed
// #define TINY_GSM_SSL_CLIENT_AUTHENTICATION
// Set your APN Details / GPRS credentials
const char apn[] = "";
const char gprsUser[] = "";
const char gprsPass[] = "";
// Server details
const char server[] = "api.callmebot.com";
const int port = 443;
// The phone number should be in international format (including the + sign)
String phone_number = "REPLACE_WITH_YOUR_PHONE_NUMBER";
String api_key = "REPLACE_WITH_YOUR_API_KEY";
TinyGsm modem(SerialAT);
TinyGsmClientSecure client(modem);
HttpClient http(client, server, port);
// LilyGO T-SIM7000G Pinout
#define UART_BAUD 115200
#define PIN_DTR 25
#define PIN_TX 27
#define PIN_RX 26
#define PWR_PIN 4
#define SD_MISO 2
#define SD_MOSI 15
#define SD_SCLK 14
#define SD_CS 13
#define LED_PIN 12
void modemPowerOn(){
pinMode(PWR_PIN, OUTPUT);
digitalWrite(PWR_PIN, LOW);
delay(1000);
digitalWrite(PWR_PIN, HIGH);
}
void modemPowerOff(){
pinMode(PWR_PIN, OUTPUT);
digitalWrite(PWR_PIN, LOW);
delay(1500);
digitalWrite(PWR_PIN, HIGH);
}
void modemRestart(){
modemPowerOff();
delay(1000);
modemPowerOn();
}
void setup() {
// Set Serial Monitor baud rate
SerialMon.begin(115200);
delay(10);
// Set LED OFF
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
modemPowerOn();
SerialMon.println("Wait...");
// Set GSM module baud rate and Pins
SerialAT.begin(UART_BAUD, SERIAL_8N1, PIN_RX, PIN_TX);
delay(6000);
// Restart takes quite some time
// To skip it, call init() instead of restart()
SerialMon.println("Initializing modem...");
modem.restart();
// modem.init();
String modemInfo = modem.getModemInfo();
SerialMon.print("Modem Info: ");
SerialMon.println(modemInfo);
// Unlock your SIM card with a PIN if needed
if (GSM_PIN && modem.getSimStatus() != 3) {
modem.simUnlock(GSM_PIN);
}
Serial.println("Make sure your LTE antenna has been connected to the SIM interface on the board.");
delay(10000);
}
void loop() {
modem.gprsConnect(apn, gprsUser, gprsPass);
SerialMon.print("Waiting for network...");
if (!modem.waitForNetwork()) {
SerialMon.println(" fail");
delay(10000);
return;
}
SerialMon.println(" success");
if (modem.isNetworkConnected()) {
SerialMon.println("Network connected");
}
String message = "Hello from ESP32!";
String url_callmebot = "/whatsapp.php?phone=" + phone_number + "&apikey=" + api_key + "&text=" + urlEncode(message);
SerialMon.print(F("Performing HTTPS GET request... "));
http.connectionKeepAlive(); // Currently, this is needed for HTTPS
int err = http.get(url_callmebot);
if (err != 0) {
SerialMon.println(F("failed to connect"));
delay(10000);
return;
}
int status = http.responseStatusCode();
SerialMon.print(F("Response status code: "));
SerialMon.println(status);
if (!status) {
delay(10000);
return;
}
SerialMon.println(F("Response Headers:"));
while (http.headerAvailable()) {
String headerName = http.readHeaderName();
String headerValue = http.readHeaderValue();
SerialMon.println(" " + headerName + " : " + headerValue);
}
int length = http.contentLength();
if (length >= 0) {
SerialMon.print(F("Content length is: "));
SerialMon.println(length);
}
if (http.isResponseChunked()) {
SerialMon.println(F("The response is chunked"));
}
String body = http.responseBody();
SerialMon.println(F("Response:"));
SerialMon.println(body);
SerialMon.print(F("Body length is: "));
SerialMon.println(body.length());
// Shutdown
http.stop();
SerialMon.println(F("Server disconnected"));
modem.gprsDisconnect();
SerialMon.println(F("GPRS disconnected"));
// Do nothing forevermore
while (true) {
delay(1000);
}
}
- Insert your SIM card pin, if you have it. In my case, I disabled the pin.
#define GSM_PIN ""
- Insert your apn details on the following lines:
// Set your APN Details / GPRS credentials
const char apn[] = "";
const char gprsUser[] = "";
const char gprsPass[] = "";
For example, in my case:
const char apn[] = "net2.vodafone.pt";
const char gprsUser[] = "vodafone";
const char gprsPass[] = "vodafone";
- Replace with your phone number and API key. The phone number should be in international format (including the + sign).
String phone_number = "REPLACE_WITH_YOUR_PHONE_NUMBER";
String api_key = "REPLACE_WITH_YOUR_API_KEY";
- Go to Tools > Board and select ESP32 Dev Module.
- Finally, upload the code to your board.
Then, open the Serial Monitor at a baud rate of 115200. Press the on-board RST button to restart the board.
Wait until the board connects to the network (in my case, it may take up to 2 minutes). You should get something similar in your Serial Monitor—see the picture below.
You can see that it identifies the SIM7000G module and connects to the network successfully.
Finally, it establishes a connection with the CallMeBot API and should return the success response “Text to send: Hello from ESP32! Message queued. You will receive it in a few seconds“. If you see something similar in your Arduino IDE Serial Monitor, it means the code is running successfully.
How the Code Works
Let’s take a quick look at the parts of the code relevant to this example.
SIM Module
First, you need to define the module you’re using. The library is compatible with many different modules. To use the SIM7000G, include the following line:
#define TINY_GSM_MODEM_SIM7000SSL
Libraries
Include the TinyGSM, ArduinoHttpClient, and UrlEncode libraries.
#include <TinyGsmClient.h>
#include <ArduinoHttpClient.h>
#include <UrlEncode.h>
APN Details
Insert the SIM card pin and APN details (in our case, it’s the following APN, user and pass):
// set GSM PIN, if any
#define GSM_PIN ""
// Set your APN Details / GPRS credentials
const char apn[] = "net2.vodafone.pt";
const char gprsUser[] = "vodafone";
const char gprsPass[] = "vodafone";
Server Domain and Resource
Define the server (CallMeBot API endpoint) and port where you want to make the HTTPS GET request:
// Server details
const char server[] = "api.callmebot.com";
const int port = 443;
Initialize TinyGSMClient
Create a TinyGsmClient instance:
TinyGsm modem(SerialAT);
TinyGsmClientSecure client(modem);
HttpClient http(client, server, port);
SIM7000G pinout
The following lines set the module baud rate and pinout:
#define UART_BAUD 115200
#define PIN_DTR 25
#define PIN_TX 27
#define PIN_RX 26
#define PWR_PIN 4
#define SD_MISO 2
#define SD_MOSI 15
#define SD_SCLK 14
#define SD_CS 13
#define LED_PIN 12
Power the modem
In the setup(), you need to include the following instructions to turn on the modem:
pinMode(PWR_PIN, OUTPUT);
digitalWrite(PWR_PIN, HIGH);
delay(300);
digitalWrite(PWR_PIN, LOW);
Start Serial Communication
Start a serial communication with the modem:
SerialAT.begin(UART_BAUD, SERIAL_8N1, PIN_RX, PIN_TX);
Restart and Initialize the Modem
Call the following function to restart the modem:
SerialMon.println("Initializing modem...");
modem.restart();
You can also use the modem.init(). The difference between restart() and init() according to documentation: “restart() generally takes longer than init() but ensures the module doesn’t have lingering connections”.
Get Modem Info
You can use the getModemInfo() to get information about the modem.
String modemInfo = modem.getModemInfo();
SerialMon.print("Modem Info: ");
SerialMon.println(modemInfo);
If you’re using the same model, it will print in the Arduino IDE Serial monitor the following information (SIM7000G):
Unlock and Connect GPRS
If your SIM card has a pin, the next command will use the pin and unlock the card:
if (GSM_PIN && modem.getSimStatus() != 3) {
modem.simUnlock(GSM_PIN);
}
Next, connect the GPRS using the APN details:
modem.gprsConnect(apn, gprsUser, gprsPass);
SerialMon.print("Waiting for network...");
if (!modem.waitForNetwork()) {
SerialMon.println(" fail");
delay(10000);
return;
}
SerialMon.println(" success");
To check if it is connected, you can use the isNetworkConnected() method:
if (modem.isNetworkConnected()) {
SerialMon.println("Network connected");
}
Configure the WhatsApp Message
You can customize the String message with your desired WhatsApp message. We’ll be using the sample text “Hello from ESP32!” and then, we’ll use the urlEncode() function to encode it.
String message = "Hello from ESP32!";
String url_callmebot = "/whatsapp.php?phone=" + phone_number + "&apikey=" + api_key + "&text=" + urlEncode(message);
Perform the HTTPS GET Request
Making an HTTPS GET request is easy using the ArduinoHttpClient library, you just need to call http.get(url_callmebot).
SerialMon.print(F("Performing HTTPS GET request... "));
http.connectionKeepAlive(); // Currently, this is needed for HTTPS
int err = http.get(url_callmebot);
if (err != 0) {
SerialMon.println(F("failed to connect"));
delay(10000);
return;
}
If the request is successful, it will print the Response status code: 200 in your Serial Monitor.
int status = http.responseStatusCode();
SerialMon.print(F("Response status code: "));
SerialMon.println(status);
if (!status) {
delay(10000);
return;
}
The following snippet is for debugging purposes, it prints the response header and content length.
SerialMon.println(F("Response Headers:"));
while (http.headerAvailable()) {
String headerName = http.readHeaderName();
String headerValue = http.readHeaderValue();
SerialMon.println(" " + headerName + " : " + headerValue);
}
int length = http.contentLength();
if (length >= 0) {
SerialMon.print(F("Content length is: "));
SerialMon.println(length);
}
if (http.isResponseChunked()) {
SerialMon.println(F("The response is chunked"));
}
The response body is the actual response from CallMeBot.
String body = http.responseBody();
SerialMon.println(F("Response:"));
SerialMon.println(body);
SerialMon.print(F("Body length is: "));
SerialMon.println(body.length());
The CallMeBot API should reply with the following success response.
Finally, stop the http connection and disconnect the GPRS modem from the Internet.
http.stop();
SerialMon.println(F("Server disconnected"));
modem.gprsDisconnect();
SerialMon.println(F("GPRS disconnected"));
That’s it, those are the most relevant code parts of this sketch.
Demonstration
After uploading your code, 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.
Go to your WhatsApp account. After a few seconds, you should receive the ESP32 message.
Wrapping Up
In this tutorial, you learned how to use the CallMeBot API with the ESP32 LILYGO T-SIM7000G to send messages to your WhatsApp account. This can be useful to send sensor readings regularly to your account, 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.
You may also like the following SIM7000G tutorials:
- Getting Started with LILYGO T-SIM7000G ESP32 (LTE, GPRS, and GPS)
- LILYGO T-SIM7000G ESP32: Get GPS Data (Latitude, Longitude, Altitude, and more)
- ESP32: Make HTTPS Requests using SIM Card – LILYGO T-SIM7000G
Learn more about the ESP32 with our resources:
- Learn ESP32 with Arduino IDE
- Build Web Servers with ESP32 and ESP8266
- Firebase Web App with ESP32 and ESP8266
- Free ESP32 Projects and Tutorials
Thanks for reading.
Well this is nice but how to input reciecer phone number without coding……to send some AT command or something. This is what could be interesting, dont you think?
Thanx for answear.
Hey Rui
Love the work you and Sara do!
You may want to put an advisory for anyone looking at this to check with their mobile provider if GSM G2/G3 is still available on their networks and if so for how long as most countries are sundowning this tech to open up the bands for G4/G5
In your own country the process is happening as we speak (theportugalnews.com/news/2023-09-04/3g-being-turned-off-in-portugal-from-today/81054 )
Here’s a global list (Last updated: March 8, 2023) FYI Scroll down for the tables by geographic region (onomondo.com/blog/2g-3g-sunset/#americas )
Today we all should be looking at 4G LTE technologies (600 MHz, 700 MHz, 1.7/2.1 GHz, 2.3 GHz, and 2.5 GHz) or if you can afford it G5
Cheers
macca
Hi.
We have a note about that at the beginning of the article.
Regards,
Sara
Could this code if modify work with ttgo SIM800?
hi, pls let me ask what should i change if i want to use sim800L board. thank you
Very good post! I used a LILYGO T-SIM A7608SA-H, which supports 4G and it worked here. Just needed to add one line in the beginning of the code (“#define TINY_GSM_MODEM_SIM7600”) and changed the value of ‘port’ to 80. Then, everything worked like shown in “Demonstration”. Thank a lot Sara and Rui!
That’s great.
Thanks for your feedback.
Regards,
Sara