Saturday, 21 January 2017

My Garage Door is now part of the Internet of Things

I am a firm believer in my phone being the only thing I need to have with me…. EVER!

… and I am looking forward with great anticipation to the day I can finally defeat my constant and unwanted companions: wallet and keys.

So when for security reasons, the apartment building where my family lives decided to install a big, thick, blocking-my-way garage door, I was a little miffed when they handed me this:

Doors seem to be a perennial problem for me (see my previous posts here).  One that I hope to beat into submission with cleaver techno-hackery.

So yeah… 

So, how to do it?

Well, I have a storage locker in the garage that is in line of site to the garage door.  And, I can use a Raspberry Pi wired up with a simple relay block to “press” the button on my garage remote.

But how to communicate with it from my phone?

Well, Bluetooth doesn’t have the range, so that probably means I need to connect it to the internet somehow.

Now, I don’t really love the idea of having to make my little Raspberry Pi accessible over the public internet (for many reasons). Fortunately, there’s a pretty good alternative:  Google Cloud Messaging (GCM).  

Note, I wrote most of this article a while ago and am just getting around to cleaning it up, I guess it’s called Firebase Cloud Messaging now (FCM), but it’s the same thing really.  I’m going to keep calling it GCM for now (because that’s what I wrote it with), and if your inner marketer needs to, you can perform the mental substitution yourself.

GCM is used to push messages to client devices (generally cellphones).  Now the reason things like GCM exist in the first place is because mobile devices like cellphones don’t usually have permanent IP addresses you can reach them at.  They often switch between networks, hide behind firewalls, and are generally assigned dynamic (and therefore unpredictable) addresses.   Cellphones are the itinerant vagabonds of the internet.

In GCM you generally have a server that publishes messages to the GCM service, and a bunch of clients (cell phones) that can receive the messages from the GCM service. This is possible, since the client devices maintain constant connectivity to the GCM service.

Now, one really cool feature of GCM is that it also supports upstream messaging.  Basically, if you decide to support it, your clients (phones) can send messages upstream to the server.  It does this using XMPP (more info here).

So here’s what I did…  I implemented a very simple Android application that registers itself with GCM that can send upstream messages to a server.  It is literally a home screen widget that acts as a button.  It does absolutely nothing else.

You can find the source code on Github:

The Raspberry Pi is connected to my home LAN, since I was able to use an Ethernet over power line adapter to bridge the Raspberry Pi into my home network from the garage.

And the server application is a (relatively) simple Python script that runs on the Raspberry Pi, which triggers the relay (and opens the door) whenever it receives a message over GCM.  

You can find the server Python script here.

Actually, it does a few other little things like some simple minded authentication against a user/password file, checking the time stamp (to make sure the request isn’t old), and ignoring requests that come too close together (to avoid button mashing).

And here it is:


 I’ve been using my garage opener app reliably for many months now, without much issue.  No longer do I have a need for a clunky remote in my pocket!

In summary, Google Cloud Messaging (ahem!  Firebase Cloud Messaging) is a really cool (and unexpected) way to implement IoT type applications without a lot of hassle.  Libraries, SDKs, and sample code are all readily available, the service is free, and it works reliably.  I will definitely consider using it again for similar projects.

Actually, if I had to do this all over again, I would probably try the new Android Things preview image for the Raspberry Pi 3.  Firebase Cloud Messaging (FCM) is much more seamlessly integrated and easy to use.  Hopefully I'll have some time to hack something together that uses this cool new framework.


  1. Nice!

    Butt dialing will now have the related "butt opening".

    Why can't the car makers have your phone unlock and start your car? My 3-count of wallet, keys, phone is down to two most of the time because I don't take a wallet, but I still need that key.

  2. Great work, James! I covered this in my regular 'Week in Firebase' blog post. Hope you don't mind that I used your image. :)

    1. Thanks! No worries. Here's a link to your post for those that are interested: