Collecting and Handling 911 Event Data

Seattle has a pretty awesome approach to data availability and transparency through  The city has thousands of data sets available (from in-car police video records to land zoning to real-time emergency feeds) and Socrata, a Seattle-based company, has worked with the city (and many other cities) to allow developers to engage this data however they like.  I spent some time playing around with some of the data sets and decided it’d be nice to know when police and fire events occurred near my apartment.
I setup a script to pull the fire and police calls for events occurring within 500 meters of my apartment and started storing them into a local database (Socrata makes it so simple – amazing work by that team).  While reading it from the API, I check the proximity of the event to my address and also the type of event (burglary, suspicious person, traffic stop, etc) and trigger emails for the ones I really want to know about (such as a near by rape, burglary, shooting, vehicle theft, etc).  I decided to store all events, even traffic stops, just because.  I may find a use for it later – who knows…
After I’ve scrubbed through and sent any notifications for events I care about, I display the data in a simple table in my existing home dashboard and highlight red any rows for events which are within certain square area of my apartment.
To add a nice visual, I also plot the most recent events on a map using the Google Maps API.  Police events are noted with blue pins, fire events are noted with red pins:
Clicking the pins will give us some details about the event:
All told, it was a pretty simple project which helped me gain some experience with the Google Maps API and also poke around with some of the data the city provides.  I’m sure I’ll be doing a bit more of that in the future.  These two projects have been integrated back into my home automation dashboard so I can continue to build on them in the future.

Location to HTTP – Send GPS data to remote script

I wanted to capture my phone’s location information and store it remotely (on my server) so I could do with it as I please.  Most of the apps I found were severely limited in that they only mapped within the app, were riddled with ads, crashed frequently, or were untrustworthy (they’re storing my location, after all).  So, I decided to build my own app that did what I wanted.

Location to HTTP

Get it on Google Play
Location to HTTP is an app I’m working on which allows users to input a remote script URL to capture a device’s location information via POST.

How it works

  1. Create a remote script to capture the variables $lat and $lon via POST (example below).
  2. Download the app and enter the URL of that script.
  3. The app will automatically send the location information every 5-10 minutes to the script.

Possible Uses

  • Track your phone’s location so you can retrace your steps if you lose it.
  • Store your location history for displaying in custom interfaces.
  • Share your location with friends/family when travelling or hiking.
  • Trigger home automation events based on your location (turn on the heat, turn on the lights, unlock the doors, feed the pets, water the plants, etc.).
  • Measure the amount of time you spend at specific locations.

Sample Script

The beauty (and utility) of this app is that you can do whatever you like with the output.  In my use case, I want to store my location history to track my travel history.  I do this using a simply PHP script and MySQL database.
Database Structure

CREATE TABLE IF NOT EXISTS `phone_location` (
`lat` varchar(200) NOT NULL,
`lon` varchar(200) NOT NULL,
`time` varchar(200) NOT NULL,
`address` varchar(200) NOT NULL,

PHP Script

//Prevent caching and set content-type to json so app can display it correctly.
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header('Content-Type: application/json');
//Pull address for latitude and longitude from Google API (max 25,000 calls per day for free)
function getaddress($lat,$lon) {
$url = ''.trim($lat).','.trim($lon);
$json = @file_get_contents($url);
$status = $data->status;
return $data->results[0]->formatted_address;
} else {
return false;
//Set the response we want to display in the app
$timestamp = date('Y-m-d h:i:s');
$success_response = "Successfully captured location @ $timestamp PST!";
//Connect to MySQL database
$conn = mysqli_connect("", "", "", "");
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
//Capture the latitude ($lat) and longitude ($lon) passed from the app
//Get the address from Google API
$address= getaddress($lat,$lon);
$address= "Not found";
//Insert record into database
$sql = "INSERT INTO phone_location (lat, lon, time, address)
VALUES ('$lat', '$lon', '$timestamp', '$address')";
if (mysqli_query($conn, $sql)) {
echo json_encode($success_response);
} else {
echo json_encode("Error: Unable to create posting.");

Google API Integration for Address Data

As shown in the example above, you can easily get address information using the Google API and reverse geocoding.

Webcam Captures Text and Stores in MySQL Database

While visiting my family recently, I saw my dad entering numbers from each of the 5-8 ticket receipts he receives daily to keep track of the work he’s done, report for payroll, etc.  I knew there had to be an easier way to collect this information without having to key each ticket manually or without using a clunky, slow scanner.  After a bit of research, I found an API for OCR from Haven OnDemand and I wrote a simple script to use the camera on his laptop to snap pictures of the tickets, scrape the text and position of the text from the tickets, store it all in a MySQL database, and retain the image of the tickets in a digital archive.
Demo: Snapping image via webcam and storing text


The script itself is actually very simple:

$name = date('Y-m-d_H:i:s');
$file = file_put_contents( $newname, file_get_contents('php://input') );
if (!$file) {
print "Unable to write image to directory.";
$filePath = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['REQUEST_URI']) . '/' . $newname;
$result_json = file_get_contents("<dedacted>&url=$filePath&mode=scene_photo");
foreach($json_a[text_block] as $p){
$sql="insert into image (name,pxleft,pxtop,pxwidth,pxheight,result) values ('$name','$result_left','$result_top','$result_width','$result_height','$result_text')";
print "$filePath\n";

While the script works well, the API isn’t great at picking up small text or parsing large amounts of text.  It lacks the accuracy (only about 80% accurate) needed to confidently rely on its interpretation of the text.  To compound the issues with the API, the images from the webcam are low quality – shaky hands, varying lighting, etc. drop the quality of the images I’m trying to scrape text from.
This will be a project I keep playing with to improve the accuracy and speed of snapshots and storage.  Until I can figure out a way to improve the accuracy, though, this isn’t very practical.

Data Visualization and Demo

As mentioned previously, my goal wasn’t to just create a home controller/dashboard but to also collect as much data as possible while doing so.  So tonight, I started playing around with a few different visualizations of the data I’ve collected thus far.  It took a few hours but I’m satisfied with the current state.
I’m doing simple dumps of the most recent music played by my Amazon Echo; most recent programming watched via DirecTv; visualizing the daily average, minimum, and maximum temperature and humidity levels in my apartment; visualizing by hour of day the average, min, and max temperature for the current month vs the previous month; breaking down the amount of time I spend at home by day of week (and telling on myself that I like to leave work early on Fridays :)); and visualizing my TV watching habits by hour of day and day of week.
I recorded a video of this all and also included the DirecTv control demo at the end.


Home Automation and NFC Tags

NFC has proven to be a pretty useless technology for cell phones (unless you’re one of the people who use you Google Wallet/Apple Pay).  Nevertheless, I decided to buy some tags and play with them because they’re so damn cheap (just over a dollar each, depending on the type).
One useful application of NFC tags is setting “scenes” using my existing home automation setup.  By setting a tag where I usually place my phone at night, I can trigger several events all at once.  When I play my phone on my nightstand, the following events are triggered:

  1. If it’s a weekday, set my alarm for 7:00 AM.
  2. If an alarm was set, the phone will adjust its volume and say “Alarm set to 7 AM”.
  3. Using the same text to speech, the phone will say “Goodnight, Kevin.”
  4. After pausing for a few seconds, it’ll POST to a simple script I wrote  and turn all the lights in the apartment off before setting it’s volume to mute for the remainder of the night.

It’s a simple way of automating my night time routine and is likely the most practical use of NFC tags with home automation (and it’s not super practical, at that).  If you want to recreate, here’s how I did it…

NFC Tag and App

I decided to go with the WhizTags brand because they boast more writeable space (888 bytes of writeable memory vs the standard 144 bytes). For reading and writing the tag, I went with the NFC Tools app. There’s no real reason for using this app – it just looked to be the most stable after a quick search.

Creating the Task

One of the benefits of the NFC Tools app is that you can export import json tasks.  Here’s the json for the task I noted

[ {
    "":"bedtime post",
    "":[ {
        "tasks.profile.fields": {
            "field1": "5"
        "tasks.profile.config": {
            "itemUpdate": "false", "requestType": "53", "itemTask": "5", "itemDescription": "5", "itemHash": "16686621-29d1-46bf-80f0-2d8905abcfdb"
        "tasks.profile.fields": {
            "field1": "0"
        "tasks.profile.config": {
            "itemUpdate": "false", "requestType": "819", "itemTask": "1", "itemHash": "b63e98a5-7385-4ae5-a71e-a76949092649", "itemDescription": "1 second"
        "tasks.profile.fields": {
            "field4": "true", "field1": "true", "field3": "true", "field8": "1", "field2": "true", "field5": "true", "field7": "false", "field6": "false"
        "tasks.profile.config": {
            "itemUpdate": "false", "requestType": "92", "itemTask": "f9", "itemDescription": "MON,TUE,WED,THU,FRI\nPerform the tasks below", "itemHash": "eb2223cb-c5a2-4d59-a1a8-961e1bb13dde"
        "tasks.profile.fields": {
            "field3": "0", "field1": "7am", "field2": "7"
        "tasks.profile.config": {
            "itemUpdate": "false", "requestType": "41", "itemTask": "7:0;7am", "itemDescription": "7am - 07:00", "itemHash": "ba40261a-84e3-437d-b1e4-b44dbc6e0b8f"
        "tasks.profile.fields": {
            "field1": "Alarm set for seven am."
        "tasks.profile.config": {
            "itemUpdate": "false", "requestType": "85", "itemTask": "Alarm set for seven am.", "itemHash": "02fc2b51-36ba-497a-adea-1620b5799d8c", "itemDescription": "Alarm set for seven am."
        "tasks.profile.fields": {
            "field1": "1"
        "tasks.profile.config": {
            "itemUpdate": "false", "requestType": "819", "itemTask": "2", "itemDescription": "2 seconds", "itemHash": "60c8cd67-00bb-4850-a5c2-cbe5883f3de4"
        "tasks.profile.fields": {
            "field1": "1"
        "tasks.profile.config": {
            "itemUpdate": "false", "requestType": "90", "itemTask": "1", "itemHash": "78382993-811f-4692-b110-61ff20406731", "itemDescription": "Close your conditional block"
        "tasks.profile.fields": {
            "field1": "Goodnight, Kevin."
        "tasks.profile.config": {
            "itemUpdate": "false", "requestType": "85", "itemTask": "Goodnight, Kevin.", "itemDescription": "Goodnight, Kevin.", "itemHash": "acab1f45-d379-4af4-99f9-0ccb63db4ae3"
        "tasks.profile.fields": {
            "field1": "2"
        "tasks.profile.config": {
            "itemUpdate": "false", "requestType": "819", "itemTask": "3", "itemDescription": "3 seconds", "itemHash": "37320287-c034-4742-a959-705ac3399800"
        "tasks.profile.fields": {
            "field1": "0"
        "tasks.profile.config": {
            "itemUpdate": "false", "requestType": "53", "itemTask": "0", "itemHash": "d7a566bd-107b-4dae-aff4-f2ce832f06e6", "itemDescription": "0"
        "tasks.profile.fields": {
            "field1": "http:\/\/\/nfc\/goodnight.php", "field2": "status=false;"
        "tasks.profile.config": {
            "itemUpdate": "true", "requestType": "110", "itemTask": "http:\/\/\/nfc\/goodnight.php|status=false;", "itemHash": "e1e00d48-16fa-4942-9d9e-298b806a65c2", "itemDescription": "Request: http:\/\/\/nfc\/goodnight.php\nPOST parameters :\nName: status \/ Value: false", "itemTaskExtra": null

The Script

The script used to turn the lights off is a modified version of this script, which I posted earlier.  Instead of controlling a single device, I simply added in all the lights in my home and added in the POST var.

Expanding the Home Dashboard

In the previous post, I outlined the Home Dashboard touchscreen for controlling lights, temperature/humidity, displaying Amazon Echo information, displaying who’s home (via bluetooth sniffing), and displaying what was being watched on DirecTv.  As this dashboard is intended to be a sudo remote control for my home, I thought it made sense to be able to actually control the TV with it.
Screenshot 2016-09-01 at 10.18.31 PMScreenshot 2016-09-01 at 10.18.09 PM
After a bit of tweaking and some UI work, the end result is a super-quick (HTTP response is ~30 milliseconds which feels nearly as quick as the standard DirecTv remote) interaction between device and DirecTv receiver.
From my home dashboard/touchscreen controller, I can select the title that’s currently playing to launch the remove control (pictured above).  You’ll notice that the touchscreen controller can do everything the standard remote can do, including guide browsing, DVR browsing, etc.

Home Dashboard using a Raspberry Pi

After creating a desktop home automation dashboard and, later, a live stream “digital picture frame”, I got the idea to combine the two into an always-on control panel that condenses everything I care about into a single kiosk which can sit on my end table or nightstand.

What it does

It’s essentially a condensed UI of the desktop version linked above which uses the same databases and processes.

  • Current indoor temperature and humidity (via DHT11 sensor)
  • If my Amazon Echo is playing music, it’ll display the artist, song, and album
  • If I’m watching TV, it’ll show the title, channel, and image/movie poster
  • Display unique icons for each person in the house (by sniffing for their phone’s bluetooth signal)
  • It’ll show the status of my lights (on/off) and update if that status changes (using the Wink API)
  • Through touch screen, allow me to control my lights in near real-time.


Materials Used

How it works

Much of this (temperature, humidty, DirecTv and Wink control) is covered in “The Foundation” post.  Specific to collecting information from the Amazon Echo, I use IfTTT and the Maker channel.  Each time my Echo plays a song, I POST to a script similar to the one below which stores the song in a MySQL database.  I can then query that, determine if the song is still playing, and publish it to the UI.

$conn = mysqli_connect(<credentials>);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
$sql = "INSERT INTO echo_history (artist, song, album, timestamp)
VALUES ('$artist', '$song', '$album', '$timestamp')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);

My Maker recipe looks something like this: IF then to URL

recordmusic.php?artist= {{ArtistName}}&song={{SongName}} &album={{AlbumName}} &timestamp={{PlayDateTime}}

Method POST.
That’s about it for the controller – quite simple and is probably the most practical project I’ve done thus far.

Using a Pi to measure TV habits

As noted here, I’m using the the DirecTV SHEF API and a Raspberry Pi to poll my DirecTV receivers every minute and store what they’re doing into a MySQL database. After ~6 months of storing that data, I thought it’d be interesting to analyze some of it.  After all, do I really watch enough HBO and Starz to warrant paying for them every month?

Channel Preferences

  • 25,900 minutes of TV watched (~2.25 hours per day…eek; still less than the national average!).
  • 2,612 minutes of that was recorded (10%).
  • NBC is our favorite channel (3,869 minutes watched, 15%).
  • E! is our second favorite channel (1,911 minutes watched, 7.37%). Gotta keep up with those Kardashians.
  • Premium movie channels (HBO, Starz, Encore, Cinemax, etc) were watched 6,870 minutes (26.52%) – apparently worth the money.
  • Premium movie channels were recorded only 571 minutes (lots of ad hoc movie watching, I guess).
  • NBC is our most recorded channel (479 minutes) followed by HGTV (391 minutes) and ABC (330).

Time Habits

  • Sunday is the most watched day (no surprise here) with 7,157 minutes watched (28%)
  • Saturday is the second with 5,385 (21%)
  • Wednesday is the least watch with 1,144 (4.4%)
  • April was our biggest TV month with 6,413 minutes watched (24.76%)
  • June was our lowest month with 1,197 (4.62%) — July is around 10%.  The excitement of summer faded fast, apparently.
  • 8PM is our biggest TV hour with 1,312 minutes (15.14%).  This is followed by 7pm (13%) and 6pm (10%).
  • 6AM is our lowest TV hour with 68 minutes watched (0.26%).  This is followed by 5am (0.49%) and 4am (0.90%).

This is pointless data but it’s always interesting to assess your own habits.  And if you’re wondering, it took roughly 60 minutes (0.23%) to query and publish this post :).

Webcam Stream as Digital Picture Frame

I don’t have a quality window view in my office building and found myself checking the local news’ weather cams throughout the day so I thought it’d be cool to have a live outdoor view streaming to my desk.  Using a Pi 3, Pi Displaya case, and camera, I built a digital picture frame which streams a live video feed from my apartment window to my desk at work.  While going through the process, I hit a few hiccups and a ton of outdated resources online so I thought I’d share my learnings and provide documentation for anyone wishing to recreate.

The following will setup a Pi to run Chromium in Kiosk mode on startup while silencing errors and hiding your cursor.  The assumptions made before beginning are that you’re able to SSH into both of the Pis and your Pis are connected to WiFi.  The guide I followed to setup my cam stream is solid so no need to re-invent the wheel – use this guide to setup your first Pi to stream your cam image.  The obvious callout is to make sure your stream resolution matches your display’s resolution.  The steps below will be for your picture frame Pi and assume you’ve already got your stream up and running…
Step 1
Run the update/upgrade and install Chromium (as explained here).
sudo apt-get update
sudo apt-get upgrade
Step 2
Install x11 server utilities and unclutter.
sudo apt-get install x11-xserver-utils unclutter
Step 3
Since we want this to be as hands-off as possible and to silence any power-saving settings or error messages on power cycles, we’ll edit the LXDE startup to disable this stuff.
sudo nano ~/.config/lxsession/LXDE-pi/autostart
and add the following lines (not the screensaver commented out):
#@xscreensaver -no-splash
@xset s off
@xset -dpms
@xset s noblank
@sed -i ‘s/”exited_cleanly”: false/”exited_cleanly”: true/’ ~/.config/chromium/Default/Preferences
The last line we’ll add in this file will be the address we want to display.  In the example below, we’ll sinply load  We use the incognito tag to prevent Chromium from showing an error in the event of a power cycle.
@chromium-browser –noerrdialogs –kiosk –incognito
Now, exit and write.
Step 4
Let’s reboot and apply the changes to see the results
sudo reboot

A month of tinkering

New Design

The original design wasn’t “clean” feeling and didn’t function too well on mobile or even tablet displays.  I changed that up a bit and the new design has a lot of transparent divs, bokeh background images, and some jquery to make actions a bit smoother.
new ui

Wink Integration Improvements

The initial integration of the Wink API wasn’t that great.  I was using PHP to trigger shell scripts which would then make the API call – quite messy and had several opportunities for failure.  This method also made a new request for a bearer token each time an action was taken so if I turned on three lights, I requested three unique tokens from the API.  I’ve since cleaned that up and now use a single token per session and the API calls are all made in a single PHP file.  This still isn’t the cleanest or safest way to do this but it works for my usecase.
While doing this, I also added the ability to dim some lights (such as the kitchen light which we leave on during the night).  The next step is to fetch the current state of the lights so that we can eliminate the on/off option and simply toggle it.  The problem with that is that the Wink Hub struggles to maintain accurate states for its devices.
Gaining my Wink bearer token for the session:

$ch_token = curl_init();
curl_setopt($ch_token, CURLOPT_URL, "");
curl_setopt($ch_token, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch_token, CURLOPT_HEADER, FALSE);
curl_setopt($ch_token, CURLOPT_POST, TRUE);
curl_setopt($ch_token, CURLOPT_POSTFIELDS, "{
  \"client_id\": \"<insert_here>\",
  \"client_secret\": \"<insert_here>\",
  \"username\": \"<insert_here>\",
  \"password\": \"<insert_here>\",
  \"grant_type\": \"password\"
curl_setopt($ch_token, CURLOPT_HTTPHEADER, array(
  "Content-Type: application/json"
$ch_token_response = curl_exec($ch_token);
$ch_token_json = json_decode($ch_token_response, true);

Wink Control:

if(is_numeric($new_state)){$action="brightness";} else {$action="powered";}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "".$device_id."/desired_state");
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, "{
  \"desired_state\": {
    \"".$action."\": ".$new_state."
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  "Content-Type: application/json",
  "Authorization: Bearer ".$bearer_token.""
$response = curl_exec($ch);
if($new_state=="true"){echo "Turned On"; } else if($new_state=="false") { echo "Turned Off"; } else { echo "Light Dimmed: $new_state"; }

DirecTv Changes

I integrated‘s API to pull images of the movies or shows that are currently on.  This currently works very well for movies but it often fails to find images for shows so I’ll loop back to fix that at some point in the future.  I also added in a link to view the title on IMDB for easy access.  An alarm was added to trigger a notification if the DVR is nearly full.

Mapping GPS Coordinates

Out of sheer curiosity, I decided to push my phone’s GPS coordinates to my server and plot them on a map.  I’m using the SendLocation app to push the coordinates to a script I have setup to listen to the app.  This may, perhaps, help me locate my phone one day if I ever lose it.  For now, though, it’s merely something for me to play with.  I’m also capturing things like speed so I can view when I’m in transit.  Essentially, this traces my steps.

MQ2 Sensor (Gas/Smoke)

I added an MQ2 sensor to the PI and set it up to store the current state as well as send me a text message and email if it detects something.  Overall, the setup is pretty simple and certainly isn’t life-saving but does serve the goal of being able to monitor home while away.
Here’s my Python file I use which I simply schedule a cron job to ensure it’s monitoring frequently enough to be useful (note the loop of 20 and sleep of 3 seconds so it runs for the entire minute before the cron fires again):

import time, sys
import RPi.GPIO as GPIO
import MySQLdb
import smtplib
def sendemail(from_addr, to_addr_list, cc_addr_list,
              subject, message,
              login, password,
    header  = 'From: %s\n' % from_addr
    header += 'To: %s\n' % ','.join(to_addr_list)
    header += 'Cc: %s\n' % ','.join(cc_addr_list)
    header += 'Subject: %s\n\n' % subject
    message = header + message

    server = smtplib.SMTP(smtpserver)
    problems = server.sendmail(from_addr, to_addr_list, message)
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
n = 20
def action(pin):
    print 'Sensor detected action!'
    db = MySQLdb.connect("","","","" )
    cursor = db.cursor()
    cursor.execute( 'insert into mq2(event) values("Gas detected")')
    sendemail(from_addr    = '',
          to_addr_list = ['att phone email address'],
          cc_addr_list = [''],
          subject      = 'Gas Detected',
          message      = 'Elevated levels of gas or smoke detected.',
          login        = '',
          password     = '')
GPIO.add_event_detect(11, GPIO.RISING)
GPIO.add_event_callback(11, action)
    while n > 0:
#        print 'alive'
#        print n
        n = n-1
except KeyboardInterrupt:

Other Changes

  • I’ve changed the interior camera to disable itself if anyone is home.  There’s no need for it to run if during that time and it also ensures an additional layer of security/privacy.
  • The date formats were updated to provide “friendlier” outputs.  I also added a time of day greeting for the active user – “Good morning/afternoon/evening/night”…
  • My favorite Google News RSS feeds were added to the left and a weather forecast link was added.  I also included a calendar with the eventual goal of integrating the Google Calendar API (I use Google Calendar quite heavily).  These are hidden by default in order to maintain the “clean” UI I’m going