The new version of Google Analytics allows you to push data to it with a new feature called the measurement protocol. The measurement protocol is basically a URL here is an example:
www.google-analytics.com/collect?v=1&tid=UA-XXXX-Y&cid=123&t=event&ec=home%20movement&ea=door%20open&el=bedroom
To learn more about Universal Analytics, the new version of Google Analytics, and the parts of the measurement protocol (URL) see my introductory post: Tracking Your Home in Google Analytics
Ok now we are back!
Tracking movement was cool but I want more exact information. I want to know if people are coming and going in my house when I’m not there. I want to know what time my house-keeper leaves or if anyone goes into my room.
Using an magnetic door sensor can give you a better reading of people coming in and out of your home, or business. The PIR sensor just senses change in inferred activity so if you’re moving around a lot it is going to to go off a lot.
This is a little bit more of a permanent fix since you install it in your door frame but there is a ton of other fun hacks you can use with it to make it worth it. Also, if you have an alarm system or any kind of magnetic locks you can relativity wire it into that but I recommend learning about electrical engineering before you’re ripping apart your wires.
1 Raspberry Pi
1 Door sensor (magnetic contact switch) or here is one that comes with sticky pads so you dont need to drill into your door.
1 Pi Cobbler
1 Breadboard
Optional: 22 AWG hookup wire – you need this is you really want to hook it up to your door.
The Door sensor uses a thing called a reed switch. This basically triggers when it notices a magnetic force near it. So technically you can connect this to anything that you want to track when the part with the wires separates from the part by itself. This will work if you put it on windows, fridges, drawers!
CLOSED:
OPEN:
It can me installed on to anything with a door or drawer.
Here is more about the reed switch as explained (really well) by Adafruit.com
The Door Switch, uses what is called a reed switch. These are two contacts inside a glass tube, that is then encased in plastic. When a magnet (the other white block) is placed near the reed switch, the contacts are drawn together and the switch closes. Since this is just a switch, the leads can be connected either way around.
We will use the Pi’s ability to create an internal pull-up resistor on the reed-switch pin, so we don’t need an external pull-up resistor.
The reed switch is pretty common for door sensors so thats why I said you can wire your home security system into Google Analytics by splitting the wires into a Raspberry Pi or Arduino Board.
Connect the PiCobbler to the Pi and Breadboard
Connect the Door Sensor to the port in front of pin 18 and GND (Ground)
import urllib2 import time import RPi.GPIO as io io.setmode(io.BCM) door_sensor = 18 sensorTrigger = True io.setup(door_sensor, io.IN, pull_up_down=io.PUD_UP) # function for the door opening def door_open(): print("Door Open") urllib2.urlopen("http://www.google-analytics.com/collect?v=1&tid=UA-40202040-2&cid=1111&t=event&ec=doors&ea=open&el=office").close # function for the door closing def door_close(): print("Door Close") urllib2.urlopen("http://www.google-analytics.com/collect?v=1&tid=UA-40202040-2&cid=1111&t=event&ec=doors&ea=closed&el=office").close while True: if io.input(door_sensor): # if door is opened if (sensorTrigger): door_open() # fire GA code sensorTrigger = False # make sure it doesn't fire again if not io.input(door_sensor): # if door is closed if not (sensorTrigger): door_close() # fire GA code sensorTrigger = True # make sure it doesn't fire again
$ sudo python doorsensor.py
It’s coming through.
Make sure its working in the terminal and then go to Google Analtyics real-time events.
You should see the category as doors and action as open and close.