This shows how to make smart home devices, which can be controlled/monitored via Siri or Home app.
Letâs make the smart home devices and control/monitor it via Apple Siri or Apple Home App.
After reading this tutorial, you are able to make Siri and Home app:
- monitor any kind of sensors, which is listed by Apple.
- control any kind of devices, which is listed by Apple.
Siri supports multiple languages. It just needs to change language setting on Siri without changing any line of code.
Step 1: Installing homebridge on Raspberry Pi
Follow this instruction
Step 2: Installing PHPoC Plugin on Raspberry Pi
PHPoC plugin does two main tasks:
- Periodically making HTTP request to PHPoC board to get states of sensors/devices and updating these states to Home App.
- On-demand making HTTP request to PHPoC board to control/monitor sensors/devices when there is request from Siri or Home app.
To install PHPoC plugin, type the following command:
sudo npm install -g homebridge-phpoc
Step 3: Writing Config File on Raspberry Pi
Config file mainly defines which devices and their characteristics is shown on Home app.
Refer to the sample config.json file below
{
âbridgeâ:{
ânameâ:âHomebridgeâ,
âusernameâ:âCC:22:3D:E3:CE:31âł,
âportâ:51826,
âpinâ:â031-45-154âł
},
âaccessoriesâ:[
{
âaccessoryâ: âPHPoCâ,
ânameâ: âLiving Room Lampâ,
âserviceâ: âLightbulbâ,
âcharacteristicsâ: [
âOnâ
],
âurlâ: âhttp://192.168.0.189/lightbulb_livingroom.phpâ,
âhttp_methodâ: âGETâ,
âupdate_intervalâ: 1000
},
{
âaccessoryâ: âPHPoCâ,
ânameâ: âBedrom Room Lampâ,
âserviceâ: âLightbulbâ,
âcharacteristicsâ: [
âOnâ,
âBrightnessâ
],
âurlâ: âhttp://192.168.0.189/lightbulb_bedroom.phpâ,
âhttp_methodâ: âGETâ,
âupdate_intervalâ: 1000
},
{
âaccessoryâ: âPHPoCâ,
ânameâ: âCarbon Monoxideâ,
âserviceâ: âCarbon Monoxide Sensorâ,
âcharacteristicsâ: [
âCarbon Monoxide Levelâ,
âCarbon Monoxide Detectedâ,
âCarbon Monoxide Peak Levelâ
],
âurlâ: âhttp://192.168.0.189/carbon.phpâ,
âhttp_methodâ: âGETâ,
âupdate_intervalâ: 1000
},
{
âaccessoryâ: âPHPoCâ,
ânameâ: âtemperatureâ,
âserviceâ: âTemperature Sensorâ,
âcharacteristicsâ: âCurrent Temperatureâ,
âurlâ: âhttp://192.168.0.189/temperature.phpâ,
âhttp_methodâ: âGETâ,
âupdate_intervalâ: 1000
}
]
}
We just need to take a look at accessories part. Here I defines four accessories (also called devices):
- Living Room Lamp:Â Allow to control and monitor on/off state.
- Bedroom Lamp:Â Allow to control and monitor on/off state and brightness.
- Carbon Monoxide Sensor:Â Allow to detect the Carbon Monoxide and monitor Carbon Monoxide level and Carbon Monoxide peek level.
- Temperature Sensor:Â Allow to monitor temperature value from temperature sensor.
Structure of each accessories is the same. They has the following fields:
- âaccessoryâ:Â Value is âPHPoCâ because it uses homebridge-phpoc plugin installed above.
- ânameâ:Â This will be displayed in Home App and is Keyword to ask Siri. No rule for this field but I recommend to use the name of sensors/devices
- âserviceâ: device type. It MUST be the one of Apple-defined services, which specified in HomeKit Accessory Protocol Specification
- âcharacteristicsâ: characteristics of sensors/devices. It MUST be some of Apple-defined characteristics, which specified in HomeKit Accessory Protocol Specification
- âurlâ:Â the link to PHPoC devices, which reads value from sensors or take control of devices.
- âupdate_intervalâ:Â Interval of updating characteristics of accessories to Home App. The unit is millisecond.
Step 4: Connecting PHPoC Board to Sensors/Devices and Writing PHPoC Code
After connecting PHPoC board to sensors/devices, We need to write code for PHPoC board to handle HTTP request from homebridge-phpoc plugin.
In case of controlling, PHPoC code does:
- Get the controlling information from HTTP request and then control devices
- Read states of sensors/devices and then send back to homebridge-phpoc plugin.
In case of monitoring, PHPoC code does:
- Read states of sensors/devices and then send back to homebridge-phpoc plugin.
Note:Â when sending back states of sensors/devices, data MUST be in json format. Keys MUST be characteristics name without blank spaces. For example, if characteristic is âCarbon Monoxide Levelâ, json key MUST be âCarbonMonoxideLevelâ.
Example codes (see code sections):
- Carbon Monoxide Sensor:Â only monitoring
- Temperature Sensor:Â only monitoring
- Lightbulb:Â monitoring and controlling
Step 5: Setting Home app your iPhone, iPad
Refer to https://support.apple.com/en-us/HT204893
Step 6: Controlling and Monitoring via Siri or Home app
To buy electronic components, you can order them from utsource.net
Code
{
âbridgeâ:{
ânameâ:âHomebridgeâ,
âusernameâ:âCC:22:3D:E3:CE:31âł,
âportâ:51826,
âpinâ:â031-45-154âł
},
âaccessoriesâ:[
{
âaccessoryâ: âPHPoCâ,
ânameâ: âLiving Room Lampâ,
âserviceâ: âLightbulbâ,
âcharacteristicsâ: [
âOnâ
],
âurlâ: âhttp://192.168.0.189/lightbulb_livingroom.phpâ,
âupdate_intervalâ: 1000100
},
{
âaccessoryâ: âPHPoCâ,
ânameâ: âBedrom Room Lampâ,
âserviceâ: âLightbulbâ,
âcharacteristicsâ: [
âOnâ,
âBrightnessâ
],
âurlâ: âhttp://192.168.0.189/lightbulb_bedroom.phpâ,
âupdate_intervalâ: 1000200
},
{
âaccessoryâ: âPHPoCâ,
ânameâ: âCarbon Monoxideâ,
âserviceâ: âCarbon Monoxide Sensorâ,
âcharacteristicsâ: [
âCarbon Monoxide Levelâ,
âCarbon Monoxide Detectedâ,
âCarbon Monoxide Peak Levelâ
],
âurlâ: âhttp://192.168.0.189/carbon.phpâ,
âupdate_intervalâ: 1000
},
{
âaccessoryâ: âPHPoCâ,
ânameâ: âtemperatureâ,
âserviceâ: âTemperature Sensorâ,
âcharacteristicsâ: âCurrent Temperatureâ,
âurlâ: âhttp://192.168.0.189/temperature.phpâ,
âupdate_intervalâ: 1050
}
]
}
Source: DIY HomeKit-Enabled Devices