Send Smartphone Notifications from Raspberry PI: Alertzy
Last Updated on 2nd August 2024 by peppe8o
In this tutorial, I will show you how to send smartphone notifications from Raspberry PI computer boards with a simple and free app: Alertzy.
At the end of this guide, you will be able to send notifications from your Python scripts and get warned when any parameter is out of your customized threshold. I will use the Raspberry PI’s CPU temperature as trigger value, but you can configure this script with any parameter of your choice.
About Alertzy
Alertzy is a web tool enabling you to receive real-time alerts as push notifications on your smartphone (supports both iOS and Android devices). With a web call their API, composed with your customized message, title, and even images) the alert will be instantly delivered to your device even if it is out of the Raspberry PI LAN.
Combining it with a Raspberry PI call from a Python script, there are limitless use cases for it including home control, server/website monitoring, price alerts, critical reminders, and so on.
Alertzy API requires only HTTP libraries available in nearly every programming language (including Python) without the need to install any custom module.
Alertzy allows you to send 100 free notifications/day. This number is enough to get notifications about many monitored devices and variables in your home projects. If you need to send more notifications, the Alertzy team allows you to contact them.
Moreover, ALertzy allows you to pass a “group” variable useful to group and filter your notifications. Groups are automatically created and destroyed once you get a message in that group or delete all the notifications in that group, so you don’t need to define them explicitly.
I will use a Raspberry PI Zero 2W computer board for this tutorial, but you can use any Raspberry PI computer board.
What We Need
As usual, I suggest adding from now to your favourite e-commerce shopping cart all the needed hardware, so that at the end you will be able to evaluate overall costs and decide if to continue with the project or remove them from the shopping cart. So, hardware will be only:
- Raspberry PI Zero 2 W (including proper power supply or using a smartphone micro USB charger with at least 3A) or newer Raspberry PI Board
- high speed micro SD card (at least 16 GB, at least class 10)
- an Android or Apple smartphone
Step-by-Step Procedure
Install Alertzy on your Smartphone
For this step please refer to your smartphone procedure. You will find the Alertzy app both in the App Store and in the Google Play Store.
After finishing the installation, you will need to sign up with an email and a password. After this step, please go to the Account section of Alertzy, with the related link:
Here you will be able to identify the Account Key that we’ll use later in our Python script:
Take note of this key.
Prepare the Raspberry PI Operating System
The first step is installing the Raspberry PI OS Lite (I suggest the 64-bit version, for boards supporting it) to get a fast and light operating system (headless). If you need a desktop environment, you can also use the Raspberry PI OS Desktop, in this case working from its terminal app. Please find the differences between the 2 OS versions in my Raspberry PI OS Lite vs Desktop article.
Please make sure that your OS is up to date. From your terminal, use the following command::
sudo apt update -y && sudo apt upgrade -y
Prepare the Smartphone Notification script in Raspberry PI
You can get my notification script directly in your Raspberry PI with the following command:
wget https://peppe8o.com/download/python/alertzy/alert.py
Let’s look at the details of this script.
The first 2 rows will import the required libraries (already installed in your Raspberry PI). The requests library is required to send web requests with Python, while the “CPUTemperature” will allow us to easily get the Raspberry PI’s CPU temperature.
import requests
from gpiozero import CPUTemperature
The following line allows you to set your Account key get from the Alertzy app in your smartphone. It will be an alphanumeric string. Please note that if you need to send notifications to multiple smartphones, you can set here multiple account key joined with the underscore character. For example, you will join an “abc123def456” account key with a second “ghi789jkl012” key account with a resulting string “abc123def456_ghi789jkl012”:
account='YOUR_ACCOUNT_KEY'
Here comes a custom function, named “notify()” that will do the job of sending notifications to the smartphones. This will get as input the title of the notification, the message and the group, but you can edit this function by adding more elements like links, buttons, and so on according to the Alertzy documentation. The account key comes from the previous code line outside the custom function.
Please note that the messages are limited to 2,000 4-byte UTF-8 characters, with a title of up to 255 characters.
Once it sets the “file” payload, the function will send the notification with the request.post() method.
def notify(title, message, group):
global account
files = {
'accountKey': (None, account),
'title': (None, title),
'message': (None, msg),
'group': (None, group),
}
response = requests.post('https://alertzy.app/send', files=files)
return True
Here the main program starts. At the beginning, it gets the CPU object and composes the message to send:
cpu = CPUTemperature(min_temp=50, max_temp=90)
msg = "CPU temperature reached %2.1f°C" %cpu.temperature
At the end of the script, if the CPU temperature overcomes a specified threshold, it sends a notification to your smartphone. It is important to note that the threshold used in this script (30 degrees) is very low and it will surely trigger the message notification as it will be used just for testing the notification delivery. If you need to really monitor the CPU temperature, you should set it to your required value.
Another important step, with the custom function we created early in this script, the notification delivery requires only a compact function call:
if cpu.temperature > 30: notify("RPI Temperature", msg, "HW notifications")
Run the Script and Send your First Smartphone Notifications to Raspberry PI
After saving the script with your account key, you can run this script with the following Python command:
python3 alert.py
And you will receive the related notification on the top of your smartphone display:
Final Considerations
Of course, this script give you just an execution example for your projects and exits after sending the notification. Even for monitoring the CPU temperature, you must put the main program lines inside a “while” loop or set a corn job that runs at specified intervals. But you can use ALertzy with any Python trigger (like a room temperature overcoming a threshold with a DHT11 sensor, on a vibration trigger, on motion detection with a PIR sensor, and so on.
What’s Next
Interested in more cool projects for your Raspberry PI? Take a look at peppe8o Raspberry PI tutorials.
Enjoy!