If there is one issue that permeates every culture, is not wanting to answer the door. We used to have countless hours of discussion about who would do it and led to a myriad of problems: relationships were broken, projects were lost, accidents happened on the way to answer the door phone! (Well, not really. Nothing of this happened, but IT COULD HAVE). This clearly had to be fixed. Thinking a bit, we realized we could ease the annoyance of this task if we could avoid having to do the enormous, unbearable exercise of standing up and walking to the phone. With this in mind, we decided to make the best of our #TryoHackathon16 and set out to solve the issue using a RaspberryPi.
We started the #TryoHackathon16 session by brainstorming ideas on how to answer the door from our desks, minimizing the effort done. In some way we needed to drive the electronic lock of the door remotely. Since we had a Raspberry Pi model B lying around and 2 people who had already worked with it in the team, it became our ultimate option.
Raspberry Pi & Circuits
Our main initial hurdle was to deal with the electronic solution in order to be able to open the door from the Raspberry Pi. Without further knowledge than an electronic circuits high school course, we set to gain some understanding of what we needed to do, “only” with what is available online. Unsurprisingly, there are plenty of sites with good tutorials about the Raspberry Pi’s GPIO (General Purpose Input/Output).
As we torn down the door phone, we found out that it worked in a pretty simple way. The button closes a circuit mechanically, which opens the door. In order to unlock the door from the Raspberry Pi, we needed to electronically close that circuit. The most simple and straightforward way to do it is to use a relay. Relays are a rather old technology but were perfect (and cheap!) for our case. The problem was that our local providers only had 5V relays and we needed something to work with 3.3v (what the GPIO pins works with). Luckily, as there is virtually a StackOverflow answer to almost every question, we followed this solution.
After having the circuit wired, we were able to control the relay with the GPIO pins from a Python script by setting the pin up and down. Boom! Magic happens. The relay was activated by running the script and it felt similar to writing some code and have it pass every test on its first run. Later, we wired the relay to the phone circuit and using the exact same script we were able to open the door.
August 2018: Please note that this post was written for an older version of Flask. Changes in the code might be necessary to adapt it to the latest versions and best practices.
from gpiozero import LED from time import sleep from flask import Response, request, abort from multiprocessing import Process ... ... def open_door_thread(): gpio = LED(17) gpio.on() sleep(5) gpio.off() @app.route('/open', methods=['POST']) def open_door_endpoint(): token = request.values['token'] # Flask has a better way of handling token authentication, but for the # purpose of testing a concept, it works. if token == 'XXXXXXXXXXXXXXXXXXX': p = Process(target=open_door_thread) p.start() resp = "aaaaand.... Opened!" return Response(resp, content_type='text/plain; charset=utf-8') # Not authorized abort(401)
At that moment, we were about to leave electronics circuits behind and we were entering the software realm since the next step was to communicate with the Raspberry Pi and trigger it remotely. Setting up a Flask endpoint to open the door took us about 60 seconds. When this endpoint is hit it closes the circuit for five seconds, opening the door, and after those 5 seconds it releases the circuit again leaving the door locked.
One of the ideas we had was to use Slack as an interface. So we defined a command,
/porteitor (as a reference to the cult class-b film “Portero Universal”) which performs a POST against our endpoint. Slack uses an Authentication Token that is sent with every request, which adds a layer of security because nobody wants strangers opening their door, right?. Having this also means that now we needed to add https to our solution, in order to avoid sending that secret in plain text over the internet. We got our self-signed certificates and installed them in an Nginx buuuuut… we found out that Slack does not allow to use self-signed certificates with their commands. Luckily, the awesome Let’s Encrypt (a trusted Certificate Authority) came to our rescue and we were able to get a free certificate for Nginx in an extremely easy way thanks to this tutorial. Finally, we solved some infrastructure nuances by using tmux and autossh (just a hint if you plan on doing the same).
Right now we keep working on this project, with some other ideas to implement on it. We want to develop an iOS app/widget and an Android one to open the door from our cellphones without needing to start up our Slack phone client. Also, we are working on answering the door by capturing the audio and plugging it to a HTTP stream, but this is still in progress.
All in all, it has been an extremely fun project were we got an insight into an area we do not normally work on. Once more we enjoyed working with Raspberry Pi’s, which are beautiful, easy to deal with, and extremely powerful. More importantly, we created something useful to us and tapped into the power of the IoT (in this context, it is fun to watch this 90s song’s video).
We realized both that technology has come a long way over the last years and that what it is important is not to be lazier, but to spend that time doing better things (Note: slouching on the couch does not count as a “better” thing). There are yet lots of things that can be automated as there are also new devices to work with (Raspberry Zero being particularly interesting). I plan to dive deeper in this amazing world, finding new cool projects to keep working on.
Like what you read?
Subscribe to our newsletter and get updates on Deep Learning, NLP, Computer Vision & Python.