This time around I decided to do a very simple but very useful project using the Raspberry Pi and Adafruit’s Magnetic Contact Switch. “Magnetic Contact Switch” is really just a fancy way of saying “Door Sensor” – one side has a switch that is closed whenever the other side, which has a magnet, is brought within range. You can detect whether or not the switch is closed, which means that we can see if something (like a door or lid) is open or closed!
Now you can know if someone’s been stealing pencils out of your locker – and prove it!
The Raspberry Pi is a great way to check out our switch’s status. And if you’re young and playing around with a Raspberry Pi, props to you – you’re going to be the next Bill Gates.
Note: I use “young” very loosely, so don’t be offended – I didn’t touch my first Pi until I was 22!
- Wire up a door sensor to the Pi
- Write a short Python script to detect when the door is open or closed
- Use Initial State to stream an alert whenever the door’s status changes
- Attach and hide your door sensor
Yes, that is my Hello Kitty lunchbox.
I used a Canakit so a lot of this was included:
- Raspberry Pi B+ w/ Raspbian loaded SD card
- 1 Magnetic Contact Switch (Door Sensor)
- HDMI cable + HDMI Monitor + Bluetooth Keyboard/Mouse (unless you already know your Pi’s IP address and can SSH in)
- WiFi Adapter/Dongle
- 1 Breadboard
- 40-pin GPIO Ribbon Cable (can use 5 individual male-to-female jumper cables instead)
- 40-pin T-Shaped GPIO to Breadboard Interface Board (not necessary if using jumper cables)
- 1 Portable Power Bank w/microUSB connector (5V/0.7-2Amps)
You need to connect:
- One of the wires (it doesn’t matter which) to GPIO 23 (pin 16)
- The other wire to GND (pin 20)
Either hook up the Pi to your monitor and keyboard or SSH in from your computer.The first thing we need to install is the GPIO library. This library allows us to use the pins on the Pi and detect things like the magnetic switch.
First, make sure your Pi’s software is up-to-date:
sudo apt-get update
Next, install a development kit that GPIO needs:
sudo apt-get install python-dev
Finally, install the library itself:
sudo apt-get install python-rpi.gpio
Remember that you enter all of these commands in the terminal or command window. Don’t forget to say yes (“Y”) whenever it asks you!
We need to install one more thing before we get started. The Initial State streamer will let us send alerts that we can see online whenever the switch is opened or closed. It’s super easy and takes less than 2 minutes.
Make sure you have an internet connection.
Type this command into the command line:
\curl -sSL https://get.initialstate.com/python -o - | sudo bash
Follow the prompts – if you say “Y” to the “Create an example script?” prompt, then you can designate where you’d like the script and what you’d like to name it. Your Initial State username and password will also be requested so that it can autofill your Access Key. If you say “n” then a script won’t be created, but the streamer will be ready for use.
Either use your example script to get your Access Key or create one from scratch and start streaming!
With everything installed, we can create our script! It’s nice and short.
import time # so we can use "sleep" to wait between actions import RPi.GPIO as io # import the GPIO library we just installed but call it "io" from ISStreamer.Streamer import Streamer # import the IS Streamer we just installed but call it "Streamer" ## name the bucket and individual access_key ## the bucket_key will send all of our messages to the same place ## the access_key tells Initial State to send the messages to you logger=Streamer(bucket_name="Locker Protector",bucket_key="locker_protector",access_key="Your_Access_Key_Here") ## set GPIO mode to BCM ## this takes GPIO number instead of pin number io.setmode(io.BCM) ## enter the number of whatever GPIO pin you're using door_pin = 23 ## use the built-in pull-up resistor io.setup(door_pin, io.IN, pull_up_down=io.PUD_UP) # activate input with PullUp ## initialize door door=0 ## this loop will execute the if statement that is true while True: ## if the switch is open if io.input(door_pin): logger.log("Door","Open") # stream a message saying "Open" logger.flush() # send the message immediately door=0 # set door to its initial value time.sleep(1) # wait 1 second before the next action ## if the switch is closed and door does not equal 1 if (io.input(door_pin)==False and door!=1): logger.log("Door","Close") # stream a message saying "Close" logger.flush() # send the message immediately door=1 # set door so that this loop won't act again until the switch has been opened
If you want to check that the switch is working before you try to stream anything, just replace logger.log(“Door”,”Open”) or logger.log(“Door”,”Close”) with print(“Door Open”) or print(“Door Close”) to see the output you get in the Pi’s command line.
I tested how far apart the switch and the magnet could be while still being “Closed”. You can see below that it’s almost an inch!
Once you get your switch program working, you can test it while streaming. I put my switches on my locker to see how they’d do.
See? Barely noticeable!
Below you can see how long and what time the locker was opened and closed on Initial State.
You can see that when the line goes up, the locker door has been opened. The graph will update in real-time, so you can watch to see if your door has been opened while you were away.
Using a power cord and ethernet connection is fine when you’re watching your bedroom door, but you’ll want to use WiFi and a portable battery if you’re setting this up in your locker. I talk about how to start a program right when you turn on the Pi and automatically connect to a wifi network in my post on a completely mobile Raspberry Pi GPS Streamer.
And there you have it! Now you can watch when you’re little sister sneaks into your room and prove to your parents that she did it.