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. Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
Kepler Kit |
450+ |
You can also buy them separately from the links below.
SN |
COMPONENT |
QUANTITY |
LINK |
---|---|---|---|
1 |
1 |
||
2 |
Micro USB Cable |
1 |
|
3 |
1 |
||
4 |
Several |
||
5 |
1(S8050) |
||
6 |
2(1KΩ, 10KΩ) |
||
7 |
1 |
||
8 |
Active Buzzer |
1 |
|
9 |
1 |
||
10 |
18650 Battery |
1 |
|
11 |
Battery Holder |
1 |
|
12 |
1 |
2. 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.

3. 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.
Visit IFTTT to log in or create an account.
Click on Create.
Add an If This event.
Search for Webhooks.
Tap Receive a web request.
Fill in the event name (e.g. SecurityWarning), and click Create trigger.
Add a Then That event.
Search for Email.
Click Send me an email.
Fill in Subject and Body, then click on Create action.
Click Continue to complete the configuration.
Modify the title name and you’re finished.
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.
4. Run the Script
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.
Copy it to the
secrets.py
script in Raspberry Pi Pico W.Note
If you don’t have
do_connect.py
andsecrets.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' }
Open the
3_ifttt_mail.py
file under the path ofkepler-kit-main/iot
, then click File -> Save as or pressCtrl+Shift+S
.Select Raspberry Pi Pico in the popup window that appears.
Set the file name to
main.py
. A prompt will appear if the same file already exists on your Pico W.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)