3. Security System via @IFTTT

With this project, we make a security device that uses a PIR Sensor to detect when a burglar or stray animal breaks into your home. You will receive an email alert if this is the case.

Webhook will be used as the most basic service. A POST request is sent to IFTTT’s service from Raspberry Pi Pico W. Using IFTTT, we will create an Applet to intercept the webhook and send an email.

1. Build the Circuit

Warning

Make sure your Li-po Charger Module is connected as shown in the diagram. Otherwise, a short circuit will likely damage your battery and circuitry.

../_images/3.ifttt_mail_bb.png

2. SET IFTTT

IFTTT is a free service that provides a variety of ways to connect different data services together.

We’ll create an Applet that reacts to a webhook (custom URL) that sends data to IFTTT. IFTTT will then send an email for you.

Please follow the steps below on IFTTT.

  1. Visit IFTTT to log in or create an account.

    ../_images/ifttt-1.png
  2. Click on Create.

    ../_images/ifttt-2.png
  3. Add an If This event.

    ../_images/ifttt-3.png
  4. Search for Webhooks.

    ../_images/ifttt-4.png
  5. Tap Receive a web request.

    ../_images/ifttt-5.png
  6. Fill in the event name (e.g. SecurityWarning), and click Create trigger.

    ../_images/ifttt-6.png
  7. Add a Then That event.

    ../_images/ifttt-7.png
  8. Search for Email.

    ../_images/ifttt-8.png
  9. Click Send me an email.

    ../_images/ifttt-9.png
  10. Fill in Subject and Body, then click on Create action.

    ../_images/ifttt-10.png
  11. Click Continue to complete the configuration.

    ../_images/ifttt-11.png
  12. Modify the title name and you’re finished.

    ../_images/ifttt-12.png
  13. Now you will be automatically redirected to the Applet details page, where you can see that the Applet is currently connected and you can toggle the switch to start/close it.

    ../_images/ifttt-13.png

3. Run the Script

  1. Now that we have created the IFTTT Applet, but we also need the API Key which can be obtained from the Webhooks Settings to allow Pico W to access IFTTT.

    ../_images/ifttt-14.png
  2. Copy it to the secrets.py script in Raspberry Pi Pico W.

    ../_images/3_ifttt4.png

    Note

    If you don’t have do_connect.py and secrets.py scripts in your Pico W, please refer to 1. Access to the Network to create them.

    secrets = {
    'ssid': 'SSID',
    'password': 'PASSWORD',
    'webhooks_key':'WEBHOOKS_API_KEY'
    }
    
  3. Open the 3_ifttt_mail.py file under the path of kepler-kit-main/iot, then click File -> Save as or press Ctrl+Shift+S.

    ../_images/3_ifttt1.png
  4. Select Raspberry Pi Pico in the popup window that appears.

    ../_images/3_ifttt2.png
  5. Set the file name to main.py. A prompt will appear if the same file already exists on your Pico W.

    ../_images/3_ifttt3.png
  6. You can now unplug the USB cable and use the Li-po Charger Module to power the Raspberry Pi Pico W. You will hear a buzzer tick when the script is running. The buzzer will keep sounding if the PIR module detects a person/creature passing by, and an email alert will be sent to you. The script can be restarted by pressing the button.

How it works?

The Raspberry Pi Pico W needs to be connected to the Internet, as described in 1. Access to the Network. For this project, just use it.

from do_connect import *
do_connect()

Reads data from the PIR module and calls the motion_detected() function if it detects someone passing by (data from 0 to 1).

import machine

sensor=machine.Pin(17,machine.Pin.IN)

sensor.irq(trigger=machine.Pin.IRQ_RISING, handler=motion_detected)

Next the Pico W sends data to IFTTT. As you can see, the message that you send to IFTTT is a url string. IFTTT identifies the sender by secrets['webhooks_key'], the triggered event is identified by event. So, make sure they are correct.

import urequests
from secrets import *

event='SecurityWarning'
message=f"https://maker.ifttt.com/trigger/{event}/with/key/{secrets['webhooks_key']}"

def motion_detected(pin):
    urequests.post(message)
    print(message)
    global warn_flag
    warn_flag=True
    sensor.irq(handler=None)

When motion_detected() is called, the variable warn_flag is set to True, causing the buzzer to keep going.

while True:
    if warn_flag==True:
        alarm.toggle()
        time.sleep_ms(50)

The purpose of the button here is to restart the script.

button=machine.Pin(16,machine.Pin.IN)

def reset_device(pin):
    machine.reset()

button.irq(trigger=machine.Pin.IRQ_RISING, handler=reset_device)