Raspberry Pi Pico W: Send Messages to WhatsApp (MicroPython) | Random Nerd Tutorials (2024)

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.

Raspberry Pi Pico W: Send Messages to WhatsApp (MicroPython) | Random Nerd Tutorials (1)

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.

Raspberry Pi Pico W: Send Messages to WhatsApp (MicroPython) | Random Nerd Tutorials (2)

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

Raspberry Pi Pico W: Send Messages to WhatsApp (MicroPython) | Random Nerd Tutorials (3)

“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.

Raspberry Pi Pico W: Send Messages to WhatsApp (MicroPython) | Random Nerd Tutorials (4)

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)

View raw code

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.

Raspberry Pi Pico W: Send Messages to WhatsApp (MicroPython) | Random Nerd Tutorials (5)

After a few seconds, you should receive a new message on your WhatsApp account.

Raspberry Pi Pico W: Send Messages to WhatsApp (MicroPython) | Random Nerd Tutorials (6)

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
Raspberry Pi Pico W: Send Messages to WhatsApp (MicroPython) | Random Nerd Tutorials (2024)
Top Articles
Sarah Beeny: Being cancer-free is frightening but Catherine’s children will get her through it
Hoofd Communicatie & Public Affairs - VO-raad, Utrecht / Villamedia
Printable Whoville Houses Clipart
Faridpur Govt. Girls' High School, Faridpur Test Examination—2023; English : Paper II
Team 1 Elite Club Invite
Federal Fusion 308 165 Grain Ballistics Chart
³µ¿Â«»ÍÀÇ Ã¢½ÃÀÚ À̸¸±¸ ¸íÀÎ, ¹Ì±¹ Ķ¸®Æ÷´Ï¾Æ ÁøÃâ - ¿ù°£ÆÄ¿öÄÚ¸®¾Æ
South Park Season 26 Kisscartoon
DL1678 (DAL1678) Delta Historial y rastreo de vuelos - FlightAware
Here's how eating according to your blood type could help you keep healthy
Lesson 1 Homework 5.5 Answer Key
Which Is A Popular Southern Hemisphere Destination Microsoft Rewards
Call Follower Osrs
Chastity Brainwash
Mlb Ballpark Pal
10 Free Employee Handbook Templates in Word & ClickUp
Bowlero (BOWL) Earnings Date and Reports 2024
Available Training - Acadis® Portal
Tamilrockers Movies 2023 Download
Napa Autocare Locator
Inter-Tech IM-2 Expander/SAMA IM01 Pro
CDL Rostermania 2023-2024 | News, Rumors & Every Confirmed Roster
Cvs El Salido
Adt Residential Sales Representative Salary
Empire Visionworks The Crossings Clifton Park Photos
Isaidup
Employee Health Upmc
Bidrl.com Visalia
Restaurants In Shelby Montana
Vht Shortener
By.association.only - Watsonville - Book Online - Prices, Reviews, Photos
Ugly Daughter From Grown Ups
Used 2 Seater Go Karts
1475 Akron Way Forney Tx 75126
Life Insurance Policies | New York Life
Gr86 Forums
Fandango Pocatello
Lowell Car Accident Lawyer Kiley Law Group
Forager How-to Get Archaeology Items - Dino Egg, Anchor, Fossil, Frozen Relic, Frozen Squid, Kapala, Lava Eel, and More!
Http://N14.Ultipro.com
ATM Near Me | Find The Nearest ATM Location | ATM Locator NL
Easy Pigs in a Blanket Recipe - Emmandi's Kitchen
Lcwc 911 Live Incident List Live Status
Rocket Lab hiring Integration & Test Engineer I/II in Long Beach, CA | LinkedIn
Ehc Workspace Login
Walmart Careers Stocker
Server Jobs Near
A Snowy Day In Oakland Showtimes Near Maya Pittsburg Cinemas
Cars & Trucks near Old Forge, PA - craigslist
M Life Insider
Grandma's Portuguese Sweet Bread Recipe Made from Scratch
Fetllife Com
Latest Posts
Article information

Author: Lakeisha Bayer VM

Last Updated:

Views: 5403

Rating: 4.9 / 5 (49 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Lakeisha Bayer VM

Birthday: 1997-10-17

Address: Suite 835 34136 Adrian Mountains, Floydton, UT 81036

Phone: +3571527672278

Job: Manufacturing Agent

Hobby: Skimboarding, Photography, Roller skating, Knife making, Paintball, Embroidery, Gunsmithing

Introduction: My name is Lakeisha Bayer VM, I am a brainy, kind, enchanting, healthy, lovely, clean, witty person who loves writing and wants to share my knowledge and understanding with you.