How to Send Alerts From Raspberry Pi Pico W to a Phone or Tablet
HomeHome > Blog > How to Send Alerts From Raspberry Pi Pico W to a Phone or Tablet

How to Send Alerts From Raspberry Pi Pico W to a Phone or Tablet

Dec 08, 2023

Custom alerts, messages from your Pico W to your mobile device.

The $8 Raspberry Pi Pico W is a marvelous little board. During the global chip supply shortage it plugged a rather large Raspberry Pi shaped hole in the Raspberry Pi inventory and provided makers with a low cost, easy to use IoT (Internet of Things) platform. Sure it's not the first Internet connected microcontroller (we can discuss that honor in the comments) but for many Raspberry Pi fans it was their first step into the wonderful world of microcontrollers.

Whereas the Raspberry Pi runs a full Linux OS, the Raspberry Pi Pico W relies on firmware to provide a layer on which we can write our code. MicroPython is one of the officially supported languages, and it has seen many improvements to accommodate the Raspberry Pi Pico. One improvement was the inclusion of urequests (micro requests) which enables our Pico W to send and receive HTTP requests.

In this how to, we are going to use urequests along with ntfy.sh, a free service and app for Android and Apple devices that can intercept messages from IoT devices and display them on your device. We are going to use it to send alerts to our Android cell phone, alerts that are triggered by a PIR (Passive Infrared) sensor that detects movement in a room.

Ntfy.sh is a remarkably effective and simple service. Instead of installing a specific Python / MicroPython module, it works with requests to send messages from the Raspberry Pi Pico W using a specific topic (very similar to how MQTT works). Our Android / iOS device is subscribed to this topic and receives the notifications from the Pico W.

We are limited to sending text on the Raspberry Pi Pico W, but this project can also be used with Raspberry Pi SBCs and the official Raspberry Pi camera to send images (or any other form of attachment) when an event happens. Say if a bird is feeding from your bird feeder or somebody is in your back garden. For an example on how to use images with ntfy.sh, take a look at our how to use dictionaries tutorial.

The circuit for this project is incredibly simple, on purpose. There are only three connections between the PIR sensor and the Raspberry Pi Pico.

Connect the wires between the Pico and the PIR sensor before moving on.

1. Install ntfy.sh for your Android / iOS device.

2. Open the app and click on + to create a new subscription.

3. Create a new topic and click Subscribe. We chose to use th-test. Create a topic that is personal to you. Also note that topics may not be password protected, so do not send sensitive data.

4. Leave the app open on your device.

Now we move to coding the Raspberry Pi Pico W.

5. Follow these steps to download the latest version of MicroPython for the Raspberry Pi Pico W. The most important steps are to download and install the UF2 firmware image and to set up Thonny. The rest are optional.

6. Open Thonny and click on the Stop button to refresh the connection. This ensures that the Python Shell is open and working correctly.

7. Create a new blank file.

8. Import the network module and then import urequests, renaming it to requests. The network module enables our Pico W to connect to the internet. Urequests is a MicroPython version of Python’s requests module which we will use to send a message from the Pico W. Urequests is now pre-installed on the latest UF2 firmware release.

9. Import the sleep function from time, and Pin from machine. The sleep function will be used to add a pause to the project code. The machine module contains functions and classes that enable our code to control and read GPIO pins.

10. Create an object, pir, and use it to create a connection to the PIR output pin at GPIO 16. The pin needs to be set as an input, and we should pull the GPIO pin high (3V) so that when the PIR is triggered, the signal will pull the pin low.

11. Create an object, wlan to represent the Wi-Fi connection, then turn the Wi-Fi on and connect to your Wi-Fi access point.

12. Add a three second delay after making the Wi-Fi connection. This isn’t strictly necessary, the connection process is a “blocker” meaning that it has to complete before it can move to this line. But we have had better luck connecting when there is a pause, than without.

13. Print the Wi-Fi connection status. True means we are connected, False means we are not.

14. Create a while True loop and use it to print the current status of the PIR GPIO pin. We set the pin to be high (pulled up) in our code, so the value should be 1.

15. Create a conditional statement that checks if the PIR sensor has been triggered.Triggering the sensor will force the GPIO pin connected to the sensor output pin to pull low (0) and our code is looking for this change.

16. Use requests to post a message to ntfy. Note that we need to specify the topic name, in our case https://ntfy.sh/th-test, as part of the function’s argument. The next argument, data is the message the user will see.

17. Inside the request specify “headers” that include a title for the notification. A priority (5 being the highest priority) and tags, which are used to add a rotating light emoji.

18. Print a message to the Python shell. This is purely for debug purposes.

19. Add an else condition that simply uses pass to let the code loop.

20. Add a delay to the code, inside the while True loop, but outside of the conditional tests. This delay can be 10 seconds, or hours. It all depends on how often you want to be alerted.

21. Save the code to the Raspberry Pi Pico as alarm.py and click Run to start. If you want this code to automatically run when the Pico W powers up, save the file as main.py. MicroPython is configured to run main.py whenever the board is powered up.

22. Place your hand in front of the sensor to trigger the alert.

23. Your Android / iOS device will receive a notification.

Join the experts who read Tom's Hardware for the inside track on enthusiast PC tech news — and have for over 25 years. We'll send breaking news and in-depth reviews of CPUs, GPUs, AI, maker hardware and more straight to your inbox.

Les Pounder is an associate editor at Tom's Hardware. He is a creative technologist and for seven years has created projects to educate and inspire minds both young and old. He has worked with the Raspberry Pi Foundation to write and deliver their teacher training program "Picademy".

Raspberry Pi Pico W Powers Unique Unicorn Timepiece

SB Components Announce Micro RP2040 For Smaller Raspberry Pi Pico Projects

Radeon RX 6750 GRE Could Just Be An Overclocked RX 6700

By Ash HillAugust 26, 2023

By Ash HillAugust 26, 2023

By Aaron KlotzAugust 25, 2023

By Denise BertacchiAugust 25, 2023

By Stewart BendleAugust 25, 2023

By Jarred WaltonAugust 25, 2023

By Mark TysonAugust 25, 2023

By Jarred WaltonAugust 25, 2023

By Ash HillAugust 25, 2023

By Mark TysonAugust 25, 2023

By Anton ShilovAugust 25, 2023

Install ntfy.shCreate a new topic and click Subscribe. Leave the app open Follow these steps to download the latest version of MicroPython for the Raspberry Pi Pico W. Open Thonny click on the Stop button Create a new blank file.Import the network module and then import urequests, renaming it to requests. Import the sleep function from time, and Pin from machine. Create an object, pir, and use it to create a connection to the PIR output pin at GPIO 16.Create an object, wlan to represent the Wi-Fi connection, then turn the Wi-Fi on and connect to your Wi-Fi access point.Add a three second delay after making the Wi-Fi connection.Print the Wi-Fi connection status.Create a while True loop and use it to print the current status of the PIR GPIO pin. Create a conditional statement that checks if the PIR sensor has been triggered.Use requests to post a message to ntfy.Inside the request specify “headers” that include a title for the notificationPrint a message to the Python shell.Add an else conditionAdd a delay to the code, inside the while True loop, but outside of the conditional tests. Save the code to the Raspberry Pi Pico as alarm.py and click Run to start. Place your hand in front of the sensor to trigger the alert.Your Android / iOS device will receive a notification.