Linux Format

If This Then That…

Les Pounder shows us how to use webhooks to trigger online services to communicat­e with one another.

- Les Pounder is a freelance maker who works with organisati­ons such as the Raspberry Pi Foundation to promote maker skills. He also blogs at bigl.es

Les Pounder shows us how to use webhooks to trigger online services to communicat­e with one another and fire off Telegram messages.

In this month’s tutorial we will be creating a project that will alert us to any movement via a passive infrared (PIR) sensor. This will trigger a message to be sent to a service called IFTTT, which is used to receive inputs from various other services and then send outputs to other services. This message to IFTTT will trigger an output and image to be sent to a

Telegram account alerting us to an intruder.

With the Raspberry Pi powered off, connect the PIR sensor to the GPIO using the three jumper wires. Connect GND to GND, VCC to the 5V pin of the Raspberry Pi. Lastly, connect the output pin of the sensor to GPIO17 of the Raspberry Pi. Now connect the camera to the CSI port, just next to the Ethernet/usb ports. You will need to gently lift the plastic clip and slide the cable in with the blue tab facing the USB ports. Then secure the cable with the plastic clip. Ensure that the camera does not touch the Pi at any time as it may cause a short circuit.

Power up your Pi and from the main desktop go to Preference­s > Raspberry Pi Configurat­ion and once there click on the Interfaces tab and enable the Pi Camera interface. When prompted reboot and wait for the Pi to return to the desktop.

Software setup

On the Raspberry Pi, open a web browser and visit https://web.telegram.org and create a new account. Keep the Telegram web app window open, as this will be used to debug and view the output of the project.

Now open a new browser window/tab and visit https://ifttt.com sign up for a free account. IFTTT stands for IF This, Then That, and it is used to trigger actions based on inputs. In this project we will create a webhook, a real-time link from our code to the web that will trigger a message on Telegram that alerts us about an intruder.

To create the trigger, click on your account avatar icon in the top right of the screen, then select Create. Now click on If + and type in webhook, and click on the blue icon to launch. In the next screen click on Receive A Web Request, and then on the next screen give the event name as “trigger”, without the quotation marks. Next, click on Then + and search for Telegram. Click on the selection and then click on Send Photo as the action. In the next screen the target chat should be with @IFTTT, however, as this is the first time we have used it we need to connect IFTTT with Telegram – just follow the process and it will connect up. Once connected it will take us back to the previous screen, so ensure that the target chat is with @IFTTT and then in the Photo URL section click on Add Ingredient and select Value1. Ensure that Value1 has a capital V. In the Caption section, change the caption to “Movement detected” and then click Create Action, then on the next screen click Finish to create and start the service.

Now click on Home, and in the Filter services box type Webhooks and click on the filtered icon. In the next screen click on Documentat­ion and you will see how the webhook is constructe­d. In the Make a POST or GET section click on {event} and change it to trigger. Now on the bottom line, which starts curl -X POST, select from https to the end of the line, then copy the link and save it in a notepad for later use. When you’re done click < Back to service.

The last step in the setup is to create an account with www.filestack.com, which will be used to store images uploaded from the Pi. These images are then attached to the Telegram message. Sign up for a free account, and when logged in to the Dashboard your API key will be in the top right corner. We will need this later.

To use filestack with Python we need to install the library. In a terminal type the following and then press Enter to run:

$ sudo pip3 install filestack-python

Then close the terminal.

Coding the project

To write the Python code for this project, choose your favourite Python 3 editor – we used Thonny – and create a new file called trigger.py. Remember to save very regularly.

The first block of code consists of the imports for the libraries that will make up the project. Requests is used to send a webhook to IFTTT. The gpiozero library is used to interface with the PIR sensor. The picamera library is for the camera, pause from signal is used to stop the project closing after it runs. Lastly, filestack is used to send camera images to the service. import requests from gpiozero import Motionsens­or from picamera import Picamera from signal import pause from filestack import Client

Next, we will need to create an object called client,

which will contain a link to Filestack, including our API Key. client = Client(“your API KEY HERE”)

We now create an object to work with Picamera, and then set the resolution of the camera to 1920x1080. camera = Picamera() camera.resolution = (1920, 1080)

Onwards, and the next step is to create a function called send_alert, which will contain all of the steps necessary to send an image via webhooks to IFTTT and then to Telegram. When the function is called, the first step is to take a picture and save it as image.jpg, which is then uploaded to Filestack as a new_filelink. This is then printed to the Python shell for debugging. def send_alert(): camera.capture(“image.jpg”) new_filelink = client.upload(filepath=”image.jpg”) print(new_filelink.url)

To send the image to IFTTT we need to use the requests library to post it to the webhook address that we made a note of earlier. This link contains our trigger word, trigger and our API key for IFTTT, but it also contains a small section of JSON, which contains the key “value1” that IFTTT expects as the URL for the

photo. So the value attached to that key is the new_ filelink.url object, the URL for the image on Filestack.

r = requests.post(“https://maker.ifttt.com/trigger/ trigger/with/key/your API KEY HERE”, json={“value1” : new_filelink.url})

To handle any errors when sending the image we use the requests status code feature to query the returned code. If it is 200 then all is well, while anything else will trigger an error.

if r.status_code == 200:

print(“alert Sent”) else:

print(“error”)

The function is now complete and here are the final three lines of the project. We create an object called pir,

and in there we tell Python where our PIR sensor is connected, GPIO17. Then we tell the code that when motion is detected, it should load the send_alert function that we have just created. Lastly, the code should pause and wait for the next trigger to set off the alarm.

pir = Motionsens­or(17) pir.when_motion = send_alert pause()

With the code completed, save and run. You should now be able to set off the sensor by moving your hand across it. Please note that it is very sensitive, so point it away from yourself for testing. Each time the sensor is triggered, the camera will take a picture and post it to Telegram – no matter where you are in the world, you will be alerted!

 ??  ?? Telegram is a great real-time communicat­ions tool, and here we are using it to send images from our Raspberry Pi sensor trap!
Telegram is a great real-time communicat­ions tool, and here we are using it to send images from our Raspberry Pi sensor trap!
 ??  ??
 ??  ??

Newspapers in English

Newspapers from Australia