In this tutorial, we will look at sending text messages from your Raspberry Pi (RPi). These text messages may contain useful information which we can use to trigger events elsewhere. We can also set it up in such a way that it sends alerts to notify us when something happens on our RPi. This tutorial will explain how this works, but we will use a free web service called IFTTT (If This, Then That).
What is IFTTT?
IFTTT stands for If This, Then That. As the name suggests, if a condition is met on our application, we must do something about it elsewhere. We can use such a service for different purposes on the RPi, but basically, we want to do something when a certain event happens on our RPi. For example, we can send a tweet when the RPi detects motion or when the temperature goes beyond a certain threshold.
How to setup IFTTT
In the following sections, I will show you how to setup your RPi for IFTTT.
- Go to the IFTTT website (https://ifttt.com/join) and create an account. You will receive several options of logging into IFTTT but I chose to log in with Google. You can use your email instead.
2. We can now create an action. Simply click on the “Create” tab as shown above.
3. Then, select the trigger for our IFTTT. Click on the “IF THIS” tab.
4. Now, choose a service for our trigger. For this demonstration, we will use the Webhook service. Webhooks allow web apps to communicate with each other. They are more like SMS notifications, but in this case, they are sent from one app to another when something happens. Just like regular SMS, webhooks transfer messages in real-time. Search for webhooks and click on the webhooks icon to select it as a service. Whenever a triggering event occurs on our RPi, the webhook identifies the event and sends information to the specified URL in the form of an HTTP request.
5. Click on the “Receive a web request” trigger. It’s the only available trigger that you will find under Webhooks.
6. Click “Connect”, enter “Event name” and click “Create trigger”.
7. Now, we need to create an action when our “If This” happens. So click on “Then That” to select a service for our action.
8. For this tutorial, we will use the “Notifications” service. Search for the notifications service and click on the icon. This will take you to a screen where you can select the type of notifications you wish to receive.
9. We intend to receive simple notifications, so we will select “Send a notification from the IFTTT app.”
10. On the next page, click “Connect,” then enter the message you want to send when an event occurs. For example, “The RPi has detected motion.” Do not worry about the default variables. For now, we just write the message that we want to send when the RPi detects motion.
11. On the next page, click “Continue” then click the “Finish” button and enter your phone number when prompted.
12. Click on the Webhooks icon to take you back to the Webhooks page.
13. At the top right-hand corner of the screen, click “Documentation.”
14. On the next page, copy your API key and the URL where we will send the webhooks notifications.
So far so good! The web service is set up and we are now ready to configure the RPi to send the alerts.
RPi Python Configuration
How to connect the RPi to IFTTT
Firstly, we need to install the “requests” module. Type
sudo pip3 install requests and run on your command prompt. Then, create a new Python file with the command:
sudo nano ifttt.py.
Next, we need to create a POST request to the webhook URL. We need to know the event name that we used earlier on, and we need to have the API key ready. I have highlighted these details in the image above.
Add the following lines to your file:
Save the file. But before you execute the script, install the IFTTT app on your device. The app is available on the App Store and Google play store. After that, you can now execute the script with
sudo python3 ifttt.py
Almost immediately, you should receive a notification on your device. If not, check if the API key and the event name are correct. And, of course, make sure your RPi is connected to the internet.
How Webhook Works
What is an HTML GET Request
The GET method is used to request data from a URL:
What is an HTML POST Request
We use the POST method to send data to a specific URL. For this example, we are sending the event name and the API key to the IFTTT URL. This is the code that we use to make posts:
How to Send Text Messages and More
We can do a lot of things with IFTTT applets. Our imagination is what limits us. In some applications, you may want to transfer multimedia messages. To do so, you can select the video or image service from the list and use it in your application.
For this sample project, we will send a text message when motion is detected by a PIR sensor connected to the RPi.
In the previous tutorial, we did a motion detection project with a PIR sensor. So now, we would edit that code to include the IFTTT requests. Follow every step that I have shown you in this tutorial and on your RPi, create a new Python file, and copy and paste the code below. But first, we are going to need:
- Raspberry Pi
- HC-SR501 PIR motion sensor
Connect the PIR out pin to RPi pin 4 and run the following code.
import RPi.GPIO as GPIO import time import requests GPIO.setmode(GPIO.BCM) pirPin = 4 GPIO.setup(PirPin, GPIO.IN) time.sleep(2) while True: if GPIO.input(pirPin): print("Motion Detected") requests.post('https://maker.ifttt.com/trigger/MotionDetected/with/key/coa5ZiiKnNjwVRP') time.sleep(1) print("No one")
We use an “If loop” to monitor the PIR out pin on RPi pin 4 until something has been detected. We then send a POST request to the IFTTT URL with the event name and the API key:
If you have set everything correctly, you should get the following notifications on your device.