Code Monkey home page Code Monkey logo

windowwatcher's Introduction

windowwatcher

Raspberry Pi module that calls a web hook when a window opens or closes

How to use a Raspberry Pi with Apple Homekit, a Synology NAS, Homebridge, Nest, and a few plugins to solve a real-world problem.

Hardware:

  • Raspberry Pi Zero
  • Magnetic contact switch
  • 200 ohm resistor
  • Synology NAS
  • Nest on-wall thermostat
  • AppleTV

Software:

  • Apple Home (built in to iOS)
  • Synology Docker (built in to Synology DSM software)
  • Homebridge SKP (“custom” package for Synology)

Node packages:

The code:

  • ww.js (runs on Raspberry Pi)
  • ww.service (manages ww.js service)

Instructions:

  1. Create a new SD card for your pi, boot to it, connect via ssh
  2. Make sure that node is up-to-date
  3. Install onoff: % npm install onoff
  4. Install node-fetch % npm install node-fetch
  5. Put ww.js on the device
  6. Test ww.js: % node ww.js
  7. Create service config

How it works:

Whenever the window sensor detects that the window has changed, either from closed to open, or open to closed, the Pi makes an outbound HTTP GET request to homebridge-http-webhooks, running on my Synology NAS in a Docker container, and exposing port 51828. The GET call passes the name of the window and the new state. In this case, the window’s ID is sensor1, and the sensor state is false, indicating that the sensor is open.

http://192.168.85.100:51828?accessoryId=sensor1&state=false

Then, the HTTP server for homebridge-http-webhooks updates homebridge to tell it that the accessory called sensor1 is now open. Through some magic that I still don’t quite understand, Apple’s HomeKit detects that the bridged device sensor1 has changed state. This happens very quickly, and the UI in the iOS Home app updates nearly instantly.

Automation in the iOS Home app watches for sensor1 (aka Master Bedroom Window) and runs the script “When the Master Bedroom Window Opens”. This causes the Nest Thermostat Eco Mode to be set to on. This command goes back to the homebridge application, which tells the homebridge-nest application to tell the Nest service to put my thermostat in eco mode.

When we close the window, the exact same happens, but in reverse. The window sensor changes state, sends an HTTP get to the homebridge servers, which magically alerts Apple HomeKit, which runs the automation, which sends a message back to homebridge, which updates the Nest service in the cloud.

windowwatcher's People

Contributors

mshulman avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.