Monday, 11 September, 2017 UTC


Summary

This post is the fourth in a series exploring home automation using a Raspberry Pi, each covering a different aspect of the build of a garage door controller:
  1. Basic hardware acquisition and installation into a project box
  2. Software installation and code for controlling the door
  3. Software camera configuration and code streaming video to HomeKit
  4. Installing and configuring door open/closed sensors
Hardware Acquisition
I purchased an additional item (listed below) to detect whether or not the garage door is closed. I’ve included a link to Amazon for your convenience, but you may be able to find this item at a lower cost elsewhere.
Item Purpose Price
Magnetic Switch Magnetic/reed switch for detecting if the garage door is closed. $8.99
Door Sensor Code
Back in Part 2, we stubbed out the door sensor code in src/sensor.js. We can implement that now.
import gpio from 'rpi-gpio';
import config from '../config.json';
import * as _ from 'lodash';
const debug = require('debug')('controller:sensor');
const closedPin = config.door.pins.closed;
gpio.setMode(gpio.MODE_RPI);
const readValue = () => {
  return new Promise((fulfill) => {
    gpio.setup(config.door.pins.closed, gpio.DIR_IN, function () {
      gpio.read(config.door.pins.closed, function(err, value) {
        debug('readValue', value);
        fulfill(value);
      });
    });
  });
}
const isDoorClosed = async () => {
  const status = await readValue();
  debug('isDoorClosed', status);
  return status;
}
export default { isDoorClosed };
Configuration
We’ll need to supplement the configuration to include the GPIO pin we used for our door closed sensor. We can do that in our config.json:
{
  "door": {
    "accessory": {
      "name": "Garage Door",
      "username": "01:23:45:67:89:AB",
      "pincode": "876-54-321",
      "port": 51826
    },
    "pins": {
      "toggle": 11,
      "closed": 7
    }
  },
  "camera": {
    "accessory": {
      "name": "Garage Camera",
      "username": "BA:98:76:54:32:10",
      "pincode": "123-45-678",
      "port": 51062
    },
    "snapshot": {
      "rotation": 180
    },
    "stream": {
      "width": 640,
      "height": 400,
      "fps": 15,
      "bitrate": 100,
      "rotation": 180
    }
  }
}
I used GPIO 4 (pin 7). You can use any GPIO as long as you’re not using it for anything else.
Wiring
I placed the magnetic sensor on the floor next to the garage door and the magnetic part (the part without the wire connections) on the door itself. Then I ran the 22-gauge wire from the switch back to the terminal block on the outside of the project box we set up in Step 1.
Next, I connected “COM” on the magnetic switch to GPIO 4 and “NO” on the switch to a ground pin. “NO” means “normally open,” so you will only get a 3.3V reading (high) when the magnets are adjacent to each other. By setting it up this way, we will get a signal when the door is closed, but no signal when the door is open.
Wrap-Up
You should now see the status of the door in the Home app on iOS. And that’s it!
Door Status in iOS Home app
I’ve placed all of the source code for the project on a GitHub repo. You can download the whole set of code and work from there. Clone it, fork it, improve it. Feel free to submit pull requests if you have some great ideas/improvements.
The post Building a Siri/iOS HomeKit-Enabled Garage Door Control with Raspberry Pi – Part 4: Door Sensors appeared first on Atomic Spin.

Related Stories

  • Building a Siri/iOS HomeKit-Enabled Garage Door Control with Raspberry Pi – Part 3: Camera Config & Video Streaming
  • Building a Siri/iOS HomeKit-Enabled Garage Door Control with Raspberry Pi – Part 2: Software
  • Building a Siri/iOS HomeKit-Enabled Garage Door Control with Raspberry Pi – Part 1: Hardware