grahl/ch

Wemos D1 Mini DHT HomeKit setup

The tutorial below is based primarily on work done by Aditya Tannu (see here and here) and tries to make things easier for beginners by reducing the setup to the bare essentials and providing clean and simple examples.

So, you might have gotten the iOS 10 beta and seen the Home app but don’t wan’t to spend an inordinate amount on commercial solutions and nowadays ESP8266 solutions are ubiquitous and cheap and those can be made to work with HomeKit.

You’ll need three things running to actually make that work: a bridge for your sensors, an MQTT broker and the sensor itself. While you can certainly use a Raspberry Pi for the first two I chose a Mac mini since I have that already running anyway. Sadly, that solution is Node-based but I haven’t seen any other solution that works out there.

The server

  • HAP (bridge)
    • Clone the HAP-NodeJS repository
    • Install with npm install (just Google if you don’t have node yet)
    • Set a custom pincode in BridgedCore.js
    • Run with DEBUG=\* node BridgedCore.js
  • Mosca (broker)
    • Install with sudo npm install -g mosca bunyan (bunyan is only needed for output)
    • Run with mosca -v | bunyan

You should now be able to add the_Node bridge_ in your HomeKit management application and see the fake sensors which are present in accessories. Once everything is running smoothly, you can run these as system services with an unprivileged user. See the project root for plist examples, just add your user name where necessary. I had some issues with calling node directly for HAP so I just made a one-liner bash script which then runs ’node BridgedCore.js’ in the relevant directory.

Problems

  • The pairing fails after a longish timeout. Your firewall settings on the server are the likely cause turn if off, if that helps, adjust as necessary.

The sensor

We can program the sensor with the NodeMCU stack but in my experience the Arduino one is far less volatile (for now).

You can now flash something onto the Wemos which will periodically send your data to the server. You can use my ino templates as a starting point. You still need set your Wifi SSID, password and the IP of the server you are sending data to.

Now that you are sending your results to the broker you still need the bridge to tell your HomeKit application about the accessory you wish to use and update its data when an update comes along. You can start with my accessory templates for humidity and temperature. You will only need to update those with the server IP, if you did not change anything else in the Arduino templates.

Problems

  • Uploading fails. Check the baud rate, the definition might not have set it to the recommended 115200.
  • I see the data in my serial monitor, the broker gets a connection but the values are still zero. Make sure that that the channel you are pushing to is the same as in the accessory file.
  • My accessory doesn’t show when I start the bridge. Make sure all files end in _accessory.js.

Caveats

Hardcoding the channels for each accessory in the js template and the Arduino sketch is inefficient at best. If someone has a good tutorial for HAP-Nodejs that does that right, let me know.

I’ve noticed that the default of 10 minutes until the next update is a bit much and the accessory can be shown as timed out or zero out. Also let me know if you have a good default here or recommendations on power optimisation.