Not long ago, the Raspberry Pi Zero came out. An impossibly small computer, even more so than the original Raspberry Pi, but just as powerful.
Oh, and it’s £4. Four actual pounds. That’s exactly 1/100th of the cost of the first PC I built for myself back in 1998, and that PC was about 1/8th as powerful as a Raspberry Pi Zero. It’s almost disgusting.
For my first project, I decided to create a intruder alert system. Not a burglar alarm, as my office has one of those already, but a system to send me an alert each time someone comes in the office.
The electronics bit
Firstly, I needed to solder some headers onto the Zero. To save money and space, it doesn’t have the handy sockets for me to poke cables into that the full sized Raspberry Pi does. I used a cut down IC socket for this purpose, and soldered it into the waiting GPIO holes on the Zero’s board. It doesn’t make use of all the holes, but for this, I only needed two of them.
For setup purposes, I needed a monitor, keyboard, mouse and network adapter connected so did that with the necessary adapters and an USB hub, and with a couple of cables to act as the make-and-break “intruder switch”, I had this:
The software bit
With Raspbian installed on the Pi’s MicroSD card, I set about writing a piece of python. It’s pretty simple – it loops until the two wires (connected to the GPIO sockets 17 and Ground) are no longer connected (that is, the door opens) then triggers an alert by sending a special URL request to IFTTT (see next section).
import RPi.GPIO as GPIO import time import requests GPIO.setmode(GPIO.BCM) GPIO.setup(17,GPIO.IN) input = GPIO.input(17) while True: if (GPIO.input(17)): r= requests.get('https://maker.ifttt.com/trigger/door_open/with/key/secretkeygoeshere') time.sleep(1)
Basically, the first three lines include the necessary libraries, the next three set up the GPIO sockets correctly, and the rest loops while checking for a circuit break, requesting the crafted IFTTT URL when necessary. It’s pretty simple.
The trigger bit
Finally, there’s the “how do I get the alert” bit. As you can see, I’m using IFTTT (IF This, Then That), a web service I use a lot – and probably should do a post on, actually – which allows things to trigger or action when other things trigger or action. In this case, I set up a trigger for the Maker channel which causes an action with the Pushbullet channel. This IFTTT recipe, in fact:
Obviously, you’ll need Pushbullet installed too (on your computer or phone or something, not on the raspberry Pi!), or you could use one of the other actions (like an iOS Alert, or an email) instead if that suits you. Make note of the “secret URL” the Maker channel gives you, and put that in the python script above.
With the Zero set up, the script running, and the whole thing running headless (so only power and wifi adapter connected), I taped it to the door as a prototype and waited. Soon, Pushbullet was recording the times the door opened – a success!