Send Pico W alerts to an Android or iOS device with ntfy.sh

The Raspberry Pi Pico W is an affordable IoT platform, and with the urequests module, you can send and receive HTTP requests.

Send Pico W alerts to an Android or iOS device with ntfy.sh
Published by Ethan @ PC Game Spotlight 9 months ago


The Raspberry Pi Pico W

The Raspberry Pi Pico W is an affordable, low-power device with endless possibilities. Despite it running on firmware, you can still program it using MicroPython.

One of the main benefits of using MicroPython is that it comes with a number of modules that you can use to interact with hardware. In this project, we’ll make use of the urequests module to send HTTP requests.

The Pico W already comes with urequests installed, but it does not yet come with the ntfy.sh service. To get this up and running, we’ll need to download and install the latest version of MicroPython and set up Thonny. If you’re unsure how to do this, take a look at our guide on how to install MicroPython on the Raspberry Pi.

Once you’ve written your code and tested it on the Pico W, you’ll want to save it to the device. To do this, use the following command:

import os  with open('main.py', 'w') as f:      f.write('your code here')  

You can also specify a filename, but the Pico W will automatically run the code under a main function.

Pico W PIR sensor alert code

To get started, grab the following items:

  • PIR sensor circuit

Connect the PIR sensor to the Pico W as shown in the diagram. You’ll be using just three connections.

Pico W PIR sensor alert code

To set this up, you’ll first need to install the ntfy.sh app on your mobile device. Then, create a new subscription, and add a custom topic.

Now, on the Pico W, download and install the latest version of MicroPython. Then, set up Thonny.

Open up a new file and write the following code:

import machine  import urequests  import time  # PIR sensor  pir = machine.Pin(6, machine.Pin.IN)  
# Wi-Fi ssid = 'your_ssid' password = 'your_password' # Connect to Wi-Fi sta_if = network.WLAN(network.STA_IF) sta_if.active(True) sta_if.connect(ssid, password) while True: if pir.value(): print('Motion detected') response = urequests.post('https://ntfy.sh', json={'message': 'Motion detected'}) print(response.text) time.sleep(1)

Here, we’re importing the necessary modules and creating connections to the PIR sensor and Wi-Fi. Then, we’re using a while loop to continuously check the status of the PIR GPIO pin, which is connected to the digital pin 6 on the Pico W.

If the sensor is triggered, we’re using the urequests module to send a message to ntfy.sh. You can specify a title, priority, and tags, but we’re keeping it simple and just using the message content.

You can also add headers to the request, such as a title, priority, and tags. Just add a new line with the following and specify the appropriate values:

response.headers['Title'] = 'Alert'  response.headers['Priority'] = 'High'  response.headers['Tags'] = 'Motion, Pico W'

Now, trigger the PIR sensor to receive a notification on your mobile device.

This is just the beginning of the possibilities you have with ntfy.sh, and you can expand this project to send multiple images or other attachments using a Raspberry Pi and camera. For more tutorials and cutting-edge projects, explore additional resources available.

Similar Articles