Monday, January 1, 2024

Remote door opener using a Raspberry Pi

I recently postfitted a SOREX's fingerprint-based lock to our door. The comfort of not requiring any "thing" (key, remote, NFC token) whatsoever to get in makes live much more relaxed. However, that's a piece of technology that may fail. And indeed, recognizing fingerprints in some circumstances (e.g. when sweating, after working physically with the finger, when they're rather cold) sometimes fails. Registering multiple fingers mitigates most of these - but hey: There's a remote for it, and even one that is meant to be "installed". So when wiring this to a Raspberry Pi somehow, it should be possible to open the door with it.

The remote's description says: 

Wireless switch with potential-free contact for door opening

Reading up the term "potential-free" basically says: You need to shorten the two wires to make it open the door. I quickly discarded using the Raspberry Pi's GPIO pins for that: As far as I understand, they're not meant to do just that. However, the GPIOs can be used to control a relais - which is basically an electronically operated switch. I ended up with a 4-relais board by AZDelivery, and a set of jumper wires like these.

Now connecting the remote is rather trivial, given the schematics for how the switch works are drawn onto the connectors: One of the wires goes into the middle. Then I chose to connect the other one to the "open" side, i.e. the one that is by default (without powering the board) not closed/connected. This assures that the switch won't open the door unless explicitly controlled to do so.

Now the tricky part for someone who gets into GPIO stuff the first time is to figure out which of the pins to use and connect to which pins on the board. Gladly, the Raspberry Pi docs help - a bit: At least, they explain what types of pins are available. Additionally, the gpio command on Raspbian prints out which pins are available on what they do.

The easy part is finding appropriate pins for power supply: The relais board operates on 5V, so the board's "VCC" pin must be connected to a raspberry 5V GPIO pin - e.g. pin 2 on a Pi 1 Model B+. Next, the relais board's "GND" is to be wired to one of the GPIO's Ground pins - e.g. pin 6 on the same Pi model.

Now the relais are operated by setting their respective connector (IN1 to IN4, one per relais on the board) to a HIGH or LOW (0 V) signal level. The raspberry uses 3,3 V for HIGH, which gladly is enough for the board to consider it HIGH as well (even though it operates on 5V). HIGH tells the board to close the switch - or more specifically: Shorten the middle connector with the one that is not connected by default. It will stay in that position until setting the signal to LOW again.

Next, I needed to find an appropriate GPIO pin. There's a caveat that I discovered the hard way: pins 0 to 8 as well as pins 14 & 15 are briefly set to HIGH whenever the Pi boots. So these pins make bad choices for a door opener 😉.  I went with pin 17. Now, opening the door can be done with the following commands:

gpio -g mode 17 out
gpio -g write 17 1
gpio -g write 17 0

The last thing to accomplish was wrapping these commands into a simple (albeit secure) web interface. I decided to for a simple Python solution, since there's an easy to use library for it. The core routine switches the given GPIO port on for a second:

def open(gpioPin):
 GPIO.setmode(GPIO.BCM)
 GPIO.setup(gpioPin, GPIO.OUT)
 GPIO.output(gpioPin, False)
 sleep(1)
 GPIO.output(gpioPin, True)
 GPIO.cleanup()

 

And that's it: Door opens using the browser!