Send data from your Arduino to HomeKit!

November 23, 2016

In my last post I described how to configure Homebridge to control Belkin WeMo switches through HomeKit. This is great, but it’s only part of my home automation story. My previous custom app included temperature and humidity sensors from Arduino Yún devices throughout my apartment. Fortunately it’s (almost) as easy to add these sensors to HomeKit as it is to add WeMo switches.

Arduino Yún

The Arduino Yún was discontinued earlier this year, so I can’t exactly recommend it if you don’t already have your own hardware. But any microcontroller that can serve up JSON will be fine. If you’re using an Arduino, I put one of my sketches here as an example. If you’re a beginner to this stuff and haven’t bought anything yet, remember that you can’t beat Adafruit for selection of parts and great tutorials.

The more powerful devices let you assign a hostname like livingroom.local, but if you’re accessing it through an IP address you should assign a static IP on your router so its address doesn’t change in future. You want your device to output a small chunk of JSON like this:

{
    "temperature": 21.61,
    "humidity": 48.85
}

Invalid output will just crash Homebridge, so use your browser to make sure it’s sending a valid result. When you’re satisfied, install the plugin on the computer running Homebridge:

sudo npm install -g homebridge-httptemperaturehumidity

Now open up ~/.homebridge/config.json and add each of your devices under the accessories section.

"accessories": [
    {
        "accessory": "HttpTemphum",
        "name": "Arduino Yún (Living Room)",
        "url": "http://livingroom.local/arduino/temp",
        "http_method": "GET",
        "humidity": true
    },
    {
        "accessory": "HttpTemphum",
        "name": "Arduino Yún (Bedroom)",
        "url": "http://bedroom.local/arduino/temp",
        "http_method": "GET",
        "humidity": true
    }
]

Restart Homebridge and you should see your sensors in HomeKit!

Unfortunately, there are a few caveats. The iOS Home app is limited in what it can do. There’s no history, and you can’t create an automation or get an alert based on a temperature reading. And the Homebridge plugin is less than perfect. It can take a few seconds to update values, and occasionally it will time out altogether. But I’m hoping both HomeKit and Homebridge continue to get better over time. And it is neat to ask Siri, “what’s the temperature in bedroom?” and get a response.

Marc Charbonneau is a mobile software engineer in Portland, OR. Want to reply to this article? Get in touch on Twitter @mbcharbonneau.