Skip to content

Raspberry Pi Pico W Twilio: Send SMS via API | ShillehTek

October 22, 2023

Video Tutorial (Optional)

Watch first if you want to follow along and see the Raspberry Pi Pico W sending an SMS using the Twilio API.

Project Overview

Send SMS Messages - Raspberry Pi Pico W: In this tutorial, you use a Raspberry Pi Pico W and the Twilio SMS API to send text messages from MicroPython over WiFi without installing the Twilio package on the Pico.

The Twilio Python package is popular, but it cannot fit on the Pico W due to limited storage. Instead, you use the Pico W s built-in WiFi and make lightweight API requests to Twilio.

  • Time: 20 to 40 minutes
  • Skill level: Beginner
  • What you will build: A MicroPython script on Pico W that connects to WiFi and sends an SMS by calling the Twilio REST API

Parts List

From ShillehTek

External

Note: This approach avoids installing the Twilio MicroPython package on the Pico W by sending HTTP requests to the Twilio API instead.

Step-by-Step Guide

Step 1 - Install Thonny on your Raspberry Pi Pico W setup

Goal: Get an IDE ready for writing and running MicroPython code on the Pico W.

What to do: Install Thonny from https://thonny.org/. If you already have an IDE that can connect to the Pico W and run MicroPython, you can skip this step.

Expected result: You can open your IDE and you are ready to connect to the Pico W.

Step 2 - Create your Twilio account

Goal: Get the Twilio credentials needed to authenticate API requests that send SMS messages.

What to do: Create an account at https://www.twilio.com/. During setup, Twilio provides a Twilio phone number, an account ID, and a token that you will need in your code.

Expected result: You have a Twilio phone number, account ID, and token available for the script.

Step 3 - Connect to the Pico W and run the script function

Goal: Run the MicroPython script that connects to WiFi and sends an SMS using the Twilio API.

What to do: Connect to your Raspberry Pi Pico W in Thonny (or your preferred IDE). Then open the script from the GitHub link and run it:

Thonny IDE connected to a Raspberry Pi Pico W with MicroPython SMS script parameters shown
Example view of the script and function parameters in the IDE.

The function logic is split into two parts.

1-) Making the internet connection: The script uses your WiFi network name and password and waits up to 10 seconds for a connection. If it cannot connect, it throws an error.

MicroPython code on Raspberry Pi Pico W showing WiFi connection setup for sending SMS via Twilio API
WiFi connection logic used by the Pico W before making the API request.

2-) Hit the Twilio API with the SMS you want to send: The script uses the MicroPython requests library to make a POST request to Twilio. You must pass the correct headers so the API can parse the payload, and you must include your Twilio authentication details to avoid auth errors.

MicroPython requests POST call from Raspberry Pi Pico W to Twilio API to send an SMS message
Example of the POST request format used to send the SMS via Twilio.

Twilio API details are available here: https://www.twilio.com/docs/usage/api.

Expected result: When you run the function, you should see an HTTP status code in the 200s. If so, your recipient likely received the SMS message.

Conclusion

You set up a Raspberry Pi Pico W to send SMS messages by connecting to WiFi and calling the Twilio API from MicroPython. The key idea is using lightweight API requests instead of installing the full Twilio package on the Pico W s limited storage.

Want parts for your next Pico W IoT build? Grab what you need from ShillehTek.com. If you want help adapting this SMS approach to your own product or IoT workflow, check out our IoT consulting services.