My apartment building lobby door lock is controlled by an intercom panel in my unit. To unlock the door, the switch completes a circuit to an electromagnet in the lobby. And for someone to ring my unit’s doorbell, a switch in the lobby completes a circuit to power a really annoying buzzer in my unit. I can also talk and listen to a speaker in the lobby.
If I’m at work I can’t get a package delivered or receive guests. Also, I’d been looking for a fun Raspberry Pi project for a few years and finally it struck me: use the Raspberry Pi to control these switches.
To start, I took the panel off the wall to see how it worked and used a voltmeter to test what all the wires mapped to and at what voltages. I only cared about two functions: unlocking the lobby door and receiving a doorbell signal. There are five wires and four functions, leaving the fifth wire as ground, all at 52V:
- Unlock door
- Listen for doorbell
- Talk to lobby speaker
- Listen to lobby speaker
When selecting a Raspberry Pi or Arduino to control these circuits, I went with the Raspberry Pi because it’s a full linux computer and seemed the most flexible. I picked the Raspberry Pi Zero W for the smaller form factor and builtin WIFI. SSH access is provided via ethernet emulation over one of it’s USB ports.
I bought the following for one day shipping from Amazon:
- CanaKit Raspberry Pi Zero W (Wireless) with Official Case and Power Supply
- SanDisk Mobile Class4 MicroSDHC Flash Memory Card- SDSDQM-B35A Without Adapter 16GB
- DIKAVS Break-away 0.1" 2x20-pin Strip Dual Male Header for Raspberry Pi Zero GPIO (Pack of 10)
Raspberry Pi Add-On Board
Now I needed a way to switch and read the intercom circuits. “HAT” is the name of a full-sized add-on board and “pHAT” is the name of the Pi Zero form factor add-on board. Pinout is a great resource with a fairly comprehensive list of pHAT boards. I figured I needed a “relay” to unlock the door and a “analog to digital converter” to read the doorbell signal. One board fit the requirements perfectly:
With the hypothesis of how all this would fit together, I bought the components and started on the software.
I followed two guides to set up a headless Raspian with WIFI enabled and SSH over USB emulated ethernet:
- Use Your Raspberry Pi As A Headless System Without A Monitor
- Connect To A Raspberry Pi Zero With A USB Cable And SSH
Writing the Software
With the hardware and OS sorted, I started experimenting with controlling the Automation pHAT using their online docs and Python function reference. I used LEDs and resistors to experiment with the functionality in isolation from the intercom.
pHAT Python Server
The pHAT Python API access must be single threaded but because I need to poll for the doorbell and for commands to unlock the door in parallel, I made a multithreaded Python server with a message queue for commands using
The doorbell goes into the Analog-Digital-Converter (ADC) and a resistive divider is used to bring the 52V into the requisite 24V range. The ADC digital reading of 6.0–6.3 is when the doorbell is ringing for whatever reason. Everything else is noise. Here’s some example output from a one second polling Python test:
6.08 <-- doorbell starts ringing
7.62 <-- doorbell stops ringing
The door lock is controlled by the single relay available on the pHAT which doesn’t require bringing the voltage down with a resistor.
Apple HomeKit Integration
I then created a HAP-NodeJS accessory to expose the Door Python server to Apple HomeKit Protocol (HAP) for Siri and Home App control. I use
systemd to keep the HAP server running. I also use the HAP accessory to timeout the door being unlocked and relock it. This is to mimic the behavior of the physical switches which spring back to neutral (locked) when not being held.
Representing a door lock with HAP-NodeJS in HomeKit was easy but getting the doorbell event to work was very fragile. It turns out that a standalone smart doorbell is not valid in HomeKit anymore. The doorbell must be accompanied by a pseudo camera, speaker, and microphone. Now I get iOS notifications when the doorbell rings!
It was also extremely difficult getting NodeJS and all the dependencies installed correctly. Once I got everything installed and working, I wrote Ansible roles to codify exactly what is required.
See the GitHub repo for the full install layout.
I was able to attach the final buildout to the preexisting circuit board screw terminals so the intercom panel still works as before but with the addition of Siri integration! Siri and Apple HomeKit turned out to be a huge win. It meant I didn’t need to run any external servers to expose my services to the internet, write any mobile apps, or do anything special to integrate with Siri. Alexa was much more complex and I ended up removing that support.
I was able to use my Apple TV to bridge all of my HomeKit devices, including my custom Lobby Door, to my iOS devices through the internet. No more missed packages and a fun project over the holidays.
Right now the Automation pHAT must poll for doorbell events. I believe there is a way to use the Raspberry Pi GPIO pins to create an event-driven approach.
Perhaps I’d also like to attempt to get the “talk” and “listen” functionality working with an additional audio-focused pHAT.
HomeKit supports BTLE and I believe the Raspberry Pi Zero W does as well. It would be nice to use the lighter-weight protocol instead of WiFi.