Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon

Sending Notifications using Raspberry Pi Zero

Save for later
  • 6 min read
  • 12 Oct 2016

article-image

In this article by Marco Schwartz, author of Building Smart Homes with Raspberry Pi Zero, we are going to start diving into a very interesting field that will change the way we interact with our environment: the Internet of Things (IoT). The IoT basically proposes to connect every device around us to the Internet, so we can interact with them from anywhere in the world.

(For more resources related to this topic, see here.)

Within this context, a very important application is to receive notifications from your devices when they detect something in your home, for example a motion in your home or the current temperature. This is exactly what we are going to do in this article, we are going to learn how to make your Raspberry Pi Zero board send you notifications via text message, email, and push notifications. Let's start!

Hardware and software requirements

As always, we are going to start with the list of required hardware and software components for the project.

Except Raspberry Pi Zero, you will need some additional components for each of the sections in this article.

For the project of this article, we are going to use a simple PIR motion sensor to detect motion from your Pi.

Then, for the last two projects of the article, we'll use the DHT11 sensor.

Finally, you will need the usual breadboard and jumper wires.

This is the list of components that you will need for this whole article, not including the Raspberry Pi Zero:

On the software side, you will need to create an account on IFTTT, which we will use in all the projects of this article. For that, simply go to:

https://ifttt.com/

You should be redirected to the main page of IFTTT where you'll be able to create an account:

sending-notifications-using-raspberry-pi-zero-img-0

Making a motion sensor that sends text messages

For the first project of this chapter, we are going to attach a motion sensor to the Raspberry Pi board and make the Raspberry Pi Zero send us a text message whenever motion is detected. For that, we are going to use IFTTT to make the link between our Raspberry Pi and our phone. Indeed, whenever IFTTT will receive a trigger from the Raspberry Pi, it will automatically send us a text message.

Lets first connect the PIR motion sensor to the Raspberry Pi. For that, simply connect the VCC pin of the sensor to a 3.3V pin of the Raspberry Pi, GND to GND, and the OUT pin of the sensor to GPIO18 of the Raspberry Pi.

This is the final result:

sending-notifications-using-raspberry-pi-zero-img-1

Let's now add our first channel to IFTTT, which will allow us later to interact with the Raspberry Pi and with web services. You can easily add new channels by clicking on the corresponding tab on the IFTTT website. First, add the Maker channel to your account:

sending-notifications-using-raspberry-pi-zero-img-2

This will basically give you a key that you will need when writing the code for this project:

sending-notifications-using-raspberry-pi-zero-img-3

After that, add the SMS channel to your IFTTT account. Now, you can actually create your first recipe. Select the Maker channel as the trigger channel:

sending-notifications-using-raspberry-pi-zero-img-4

Then, select Receive a web request:

sending-notifications-using-raspberry-pi-zero-img-5

As the name of this request, enter motion_detected:

sending-notifications-using-raspberry-pi-zero-img-6

As the action channel, which is the channel that will be executed when a trigger is received, choose the SMS channel:

sending-notifications-using-raspberry-pi-zero-img-7

For the action, choose Send me an SMS:

Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at ₹800/month. Cancel anytime

sending-notifications-using-raspberry-pi-zero-img-8

You can now enter the message you want to see in the text messages:

sending-notifications-using-raspberry-pi-zero-img-9

Finally, confirm the creation of the recipe:

sending-notifications-using-raspberry-pi-zero-img-10

Now that our recipe is created and active, we can move on to actually configuring Raspberry Pi so it sends alerts whenever a motion is detected. As usual, we'll use Node.js to code this program.

After a minute, pass your hand in front of the sensor: your Raspberry Pi should immediately send a command to IFTTT and after some seconds you should be able to receive a message on your mobile phone:

sending-notifications-using-raspberry-pi-zero-img-11

Congratulations, you can now use your Raspberry Pi Zero to send important notifications, on your mobile phone!

Note that for an actual use of this project in your home, you might want to limit the number of messages you are sending as IFTTT has a limit on the number of messages you can send (check the IFTTT website for the current limit). For example, you could use this for only very important alerts, like in case of an intruder coming in your home in your absence.

Sending temperature alerts through email

In the second project of the article, we are going to learn how to send automated email alerts based on data measured by the Raspberry Pi.

Let's first assemble the project. Place the DHT11 sensor on the breadboard and then place the 4.7k Ohm resistor between pin 1 and 2 of the sensor. Then, connect pin 1 of the sensor to the 3.3V pin of the Raspberry Pi, pin 2 to GPIO18, and pin 4 to GND. This is the final result:

sending-notifications-using-raspberry-pi-zero-img-12

Let us now see how to configure the project. Go over to IFTTT and create add the Email Channel to your account:

sending-notifications-using-raspberry-pi-zero-img-13

After that, create a new recipe by choosing the Maker channel as the trigger:

sending-notifications-using-raspberry-pi-zero-img-14

For the event, enter temperature_alert and then choose Email as the action channel:

sending-notifications-using-raspberry-pi-zero-img-15

You will then be able to customize the text and subject of the email sent to Pi. As we want to send the emails whenever the temperature in your home gets too low, you can use a similar message:

sending-notifications-using-raspberry-pi-zero-img-16

You can now finalize the creation of the recipe and close IFTTT. Let's now see how to configure the Raspberry Pi Zero. As the code for this project is quite similar to the one we saw in the previous section, I will only highlight the main differences here.

It starts by including the required components:

var request = require('request');
var sensorLib = require('node-dht-sensor');

 

Then, give the correct name to the event we'll use in the project:

var eventName = 'temperature_low';

We also define the pin on which the sensor is connected:

var sensorPin = 18;

As we want to send alerts based on the measured temperature, we need to define a threshold. As it was quite warm when I made this project, I have assigned a high threshold at 30 degrees Celsius, but you can, of course, modify it:

var threshold = 30;

Summary

In this article, we learned all the basics about sending automated notifications from your Raspberry Pi. We learned, for example, how to send notifications via email, text messages, and push notifications. This is really important to build a smart home, as you want to be able to get alerts in real-time from what's going on inside your home and also receive regular reports about the current status of your home.

Resources for Article:


Further resources on this subject: