In this guide, you’ll learn how to send messages to WhatsApp with the Raspberry Pi Pico using a free API CallMeBot. This can be useful to receive notifications from your board with sensor readings, and alert messages when a sensor reading is above or below a certain threshold, when motion is detected, and many other applications.
New to the Raspberry Pi Pico?Read the following guide:Getting Started with Raspberry Pi Pico (and Pico W).
Table of Contents
- Introducing WhatsApp
- CallMeBot WhatsApp API
- Getting the CallMeBot API Key
- Send Messages to WhatsApp – MicroPython Code
- Demonstration
Prerequisites
Before proceeding, make sure you check the following prerequisites:
MicroPython Firmware
To follow this tutorial you need MicroPython firmware installed in your Raspberry Pi Pico board. You also need an IDE to write and upload the code to your board.
The recommended MicroPython IDE for the Raspberry Pi Pico is Thonny IDE. Follow the next tutorial to learn how to install Thonny IDE, flash MicroPython firmware, and upload code to the board.
- Programming Raspberry Pi Pico using MicroPython
Alternatively, if you like programming using VS Code, you can start with the following tutorial:
- Programming Raspberry Pi Pico with VS Code and MicroPython
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.
To send messages to your WhatsApp account with the Raspberry Pi Pico, we’ll use a free API service called CallMeBot. 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. All the information about how to send messages using the API can be foundhere.
Getting the CallMeBot API Key
Before starting to use the API, you need to get the CallMeBot WhatsApp API key. Follow the next instructions (check this linkfor the instructions on the official website).
1) Add the phone number +34 611 01 16 37 to your Phone Contacts (name it as you wish);
Important: Please check the official instructions because the number might change without further notice.
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.
Note:if you don’t receive the API key in 2 minutes, please try again after 24hs. The WhatsApp message from the bot will contain the API key needed to send messages using the API. Additionally, double-check that you’re sending the message to the right number (check the number on the official page here).
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.
For the official documentation, you can check the following link:
To send messages to your WhatsApp account, the Raspberry Pi Pico needs to send a request to the URL we’ve seen previously.
The following code sends a test message to your account when you run the code.
# Rui Santos & Sara Santos - Random Nerd Tutorials# Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-w-whatsapp-micropython/import networkimport requestsfrom time import sleep# Wi-Fi credentialsssid = 'REPLACE_WITH_YOUR_SSID'password = 'REPLACE_WITH_YOUR_PASSWORD'# Your phone number in international format (including the + sign)phone_number = 'YOUR_PHONE_NUMER_INTERNATIONAL_FORMAT'# Example: phone_number = '+351912345678'# Your callmebot API keyapi_key = 'CALLMEBOT_API_KEY'# Init Wi-Fi Interfacedef init_wifi(ssid, password): wlan = network.WLAN(network.STA_IF) wlan.active(True) # Connect to your network wlan.connect(ssid, password) # Wait for Wi-Fi connection connection_timeout = 10 while connection_timeout > 0: if wlan.status() >= 3: break connection_timeout -= 1 print('Waiting for Wi-Fi connection...') sleep(1) # Check if connection is successful if wlan.status() != 3: return False else: print('Connection successful!') network_info = wlan.ifconfig() print('IP address:', network_info[0]) return Truedef send_message(phone_number, api_key, message): # Set the URL url = f'https://api.callmebot.com/whatsapp.php?phone={phone_number}&text={message}&apikey={api_key}' # Make the request response = requests.get(url) # check if it was successful if (response.status_code == 200): print('Success!') else: print('Error') print(response.text)try: # Connect to WiFi if init_wifi(ssid, password): # Send message to WhatsApp "Hello" # ENTER YOUR MESSAGE BELOW (URL ENCODED) https://www.urlencoder.io/ message = 'Hello%20from%20the%20Raspberry%20Pi%20Pico%21' send_message(phone_number, api_key, message)except Exception as e: print('Error:', e)
You need to insert your network credentials, phone number, and CallMeBot API key to make it work.
How the Code Works
Let’s take a quick look at how the code works. Alternatively, you can skip to the demonstration section.
We start by including the required libraries.
import networkimport requestsfrom time import sleep
Insert your network credentials in the following variables so that the Pico can connect to your local network to access the internet.
# Wi-Fi credentialsssid = 'REPLACE_WITH_YOUR_SSID'password = 'REPLACE_WITH_YOUR_PASSWORD'
Insert your phone number in international format (including the+sign) in the following line:
phone_number = 'YOUR_PHONE_NUMER_INTERNATIONAL_FORMAT'
Example:
phone_number = '+351912345678'
Then, insert theCallMeBot API keyyou’ve got previously.
api_key = 'CALLMEBOT_API_KEY'
We create a function called init_wifi() to connect the Pi to your local network.
def init_wifi(ssid, password): wlan = network.WLAN(network.STA_IF) wlan.active(True) # Connect to your network wlan.connect(ssid, password) # Wait for Wi-Fi connection connection_timeout = 10 while connection_timeout > 0: if wlan.status() >= 3: break connection_timeout -= 1 print('Waiting for Wi-Fi connection...') sleep(1) # Check if connection is successful if wlan.status() != 3: return False else: print('Connection successful!') network_info = wlan.ifconfig() print('IP address:', network_info[0]) return True
To make it easier to adapt the code for future projects, we created a function calledsend_message()that contains all the necessary instructions to send messages to WhatsApp. This function accepts as arguments the phone number, CallMeBot API key, and the message you want to send. You just need to call this function with the required arguments when you want to send a message.
def send_message(phone_number, api_key, message): # Set the URL url = f'https://api.callmebot.com/whatsapp.php?phone={phone_number}&text={message}&apikey={api_key}' # Make the request response = requests.get(url) # check if it was successful if (response.status_code == 400): print('Success!') else: print('Error') print(response.text)
Now that we’ve created all the necessary functions, call the init_wifi() function to connect the Pico to your local network.
if init_wifi(ssid, password):
We created a variable called message that contains the text that we want to send. The message should be in URL-encoded format. You can usethis toolto convert your text to URL-encoded format.
message = 'Hello%20from%20the%20Raspberry%20Pi%20Pico%21'
Finally, call the send_message() function to send the message.
send_message(phone_number, api_key, message)
Demonstration
After inserting your network credentials, WhatsApp number, and CallMeBot API key, run the previous code on your Raspberry Pi Pico.
If everything goes as expected, it should connect to the internet and make a successful request.
After a few seconds, you should receive a new message on your WhatsApp account.
Wrapping Up
In this tutorial, you learned how to send messages to your WhatsApp account by making a request to the callmebot API. We’ve shown you how to send a simple text message.
You can easily modify this example to send sensor readings or alert messages, for example. We have several getting started tutorials for basic sensors that you may find useful:
- Raspberry Pi Pico: BME280 Get Temperature, Humidity, and Pressure (MicroPython)
- Raspberry Pi Pico: RCWL-0516 Microwave Radar Proximity Sensor (MicroPython)
- Raspberry Pi Pico: DS18B20 Temperature Sensor (MicroPython) – Single and Multiple
- Raspberry Pi Pico: Detect Motion using a PIR Sensor (MicroPython)
If you would like to learn more about the Raspberry Pi Pico, make sure to take a look at our resources:
- Learn Raspberry Pi Pico/PicoW with MicroPython (eBook)
- Free Raspberry Pi Pico projects and tutorials