After viewing this great example by Nikita Leonov, I decided to work on HomeKit and Particle Photon. My plan was simulate Temperature, Humidity and Luminosity sensor and one Light. Here is the sample I completed.
Here in this sample, I am using Homebridge and HAP-NodeJS to emulate HomeKit API. Homebridge is a shim around HAP-NodeJS. HAP-NodeJS is a Node.js implementation of HomeKit Accessory Server.
Homebridge supports 2 types of shims, accessory and platforms. Accessory is a single or multiple accessory device. Platform a bridge to another system. In HAP-Node-JS each accessory has require Characteristics which must be implemented by the Accessory and some optional Characteristics which is optional. For this sample I created an accessory which emulates 4 devices, Temperature Sensor, Humidity Sensor, Luminosity Sensor and a Light. The Light supports brightness and hue characteristics. My Homebridge app is running on my Macbook Pro (you can Raspberry Pi or any Linux system also, I tried to compile Homebridge under Cygwin but failed).
DHT22 sensor is used to read Temperature and Humidity, TSL2561 sensor is
used for luminosity. To simulate light I am using a simple RGB Led. The Photon firmware reads Temperature, Humidity and Luminosity sensor values and publish an event at every 5 seconds.
The Particle Accessory listens for this events and whenever it is received, update the Characteristics. To control the light, the accessory calls Photon exposed functions to turn on/off, adjust brightness and set hue.
Wiring
DHT22 Vcc to Photon 3.3v
DHT22 GND to Photon GND
DHT22 Data to Photon D4
TSL2561 3.3vto Photon 3.3v
TSL2561 GND to Photon GND
TSL2561 SDA to Photon D0
TSL2561 SCL to Photon D1
RGB Led R to Photon A4
RGB Led G to Photon A5
RGB Led B to Photon A7
RGB Led GND to Photon GND
How to run
Setup Particle Photon:- Wire the sensors as described above and compile the firmware source code and flash it to the Photon.
Configure Homebridge:- Copy the Particle.js file to the Homebridge Accessory folder. Edit the config.json and add following under accessory section and replace the device id and access token.
{
“accessory”: “Particle”,
“name”: “Particle Sensors”,
“cloud_url”: “https://api.spark.io/v1/devices/”,
“deviceid”: “replace with your deviceid”,
“accesstoken”: “replace with your access token”
}
Open a terminal and run node app.js. If everything goes well you can see the Homebridge is up and running. Take a note of the PIN that is shown on the console. This is necessary to add the accessory to the iOS application. In every 5 seconds you can also see the sensor values received.
Control/Monitor using iOS Device:- Install any HomeKit application. There are different HomeKit application on iOS device. Some of them are free and some are paid. I have Home, Elgato Eve and Insteon+ installed on my iPhone. Open the app and add the accessory, when the device scan for accessory you can see Homebridge, under this every accessories will be listed. While adding you have to enter the PIN. Enter the pin shown on the console while running the Homebridge.
@Jeff, it should work on Particle Core also, it does not have any Photon dependency. The Photon read sensor values from TSL2561 and DHT22, also connected to RGB LED.
@krvarma Thanks! The code seems to work on the Core, but I’m having some trouble getting HomeBridge working. I’ve tried on Raspberry Pi (both 1 and 2) and Ubuntu in Parallels, but I’m getting stuck with different errors.
So far I’ve had the most luck in Ubuntu, but I’m just stuck on the Particle accessory… I’ll get back to it tomorrow, it’s time to call it a night.
@Jeff, I too had some issues installing on Ubuntu, but I managed it after manually installing some dependencies. I don’t exactly remember which are the dependencies. Even though I managed to install it on Ubuntu, I am not using. I am using my Macbook Pro and Raspberry Pi for development.
You can check this video explaining installation on RPi, may be that will be useful.
Thanks @krvarma – I’ve almost got it working! Now I am just stuck on this error:
Loading 1 accessories...
module.js:338
throw err;
^
Error: Cannot find module 'eventsource'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:278:25)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (/home/pi/homebridge/accessories/Particle.js:4:19)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
I have looked at the configuration files but I just can’t seem to figure out what these error message mean (I’m pretty new to node, this is my first foray into it…)
I had some trouble with my Core, and it seems like it might also be having some issues with the lower amount of memory available to it.
I don’t need the Lux or RGB for my relay, so I’m going to try to remove those bits and work on adding a IR output instead to turn on my air conditioner.
The other thing I can’t quite work out is why Siri will happily turn the light on or off but can’t tell me the temperature or humidity using the particle readings.
/Users/ritchyromero/Documents/homebridge/node_modules/hap-nodejs/lib/Service.js:38
if (!UUID) throw new Error(“Services must be created with a valid UUID.”);
^
Error: Services must be created with a valid UUID.
at new Service (/Users/ritchyromero/Documents/homebridge/node_modules/hap-nodejs/lib/Service.js:38:20)
at parseServiceJSON (/Users/ritchyromero/Documents/homebridge/node_modules/hap-nodejs/lib/AccessoryLoader.js:114:17)
at /Users/ritchyromero/Documents/homebridge/node_modules/hap-nodejs/lib/AccessoryLoader.js:63:19
at Array.forEach (native)
at Object.parseAccessoryJSON (/Users/ritchyromero/Documents/homebridge/node_modules/hap-nodejs/lib/AccessoryLoader.js:62:17)
at createAccessory (/Users/ritchyromero/Documents/homebridge/app.js:153:28)
at loadAccessories (/Users/ritchyromero/Documents/homebridge/app.js:89:25)
at startup (/Users/ritchyromero/Documents/homebridge/app.js:60:29)
at Object. (/Users/ritchyromero/Documents/homebridge/app.js:223:1)
at Module._compile (module.js:456:26)
@krvarma tnx for the fast reply… And sorry for the lack of information given in my previous post.
I’m using a mac with OSX…
Because i’m not familiar with js i can only follow the steps given by the tutorial
I did move (and edit with my tokens) the config file to the homebridge directory and i did move the particle file to the homebridge/accessories directory…
The error talks about the service not having a correct uuid?
Lukas Jezny developed an application for a Photon, in which he implements a HAP (Homekit Access Point) on the device itself. So, you don’t need a Homebridge or an OpenHAB server to control a Photon with Siri…
It works beautifully: You compile a sketch and flash it to any Particle device and then it can be directly paired with Homekit on any iPad, iPhone… Currently, you can only control the Particle’s RGB LED ON/OFF state.
I was hoping that Lukas would also show us how we could access Particle.function s and Particle.variable s from Homekit. But, fair enough, he apologised because he can’t make the time for it. Pity…
He shared also the Apple document explaining how to expand the functionality, but it goes over my head!
If some of you, professional programmers, would be interested to try and expand his work, I believe many Particle users would be very happy! I would…