I reviewed the Xiaomi Mi Home Hub as a standalone gadget, it is time to see how I can integrate the device with my NodeRED based home automation setup. The hub is more than just a Zigbee compatible bridge between the app and sensors so I’m hoping to have all the functionality available in NodeRED too. Let’s see what is Xiaomi Gateway in NodeRED capable of.
Xiaomi Gateway in NodeRED
Before I dive into the details, let’s talk about the possible functions. The Xiaomi Gateway doesn’t have to be hacked to work with NodeRED. I appraised the Yeelight Smart Bulbs for having the API support before, so I’m super pleased to see the same options available with Xiaomi Gateway in NodeRED.
Before I can interact with Xiaomi Gateway in NodeRED, I have to download a new node (node-red-contrib-xiaomi-smart-devices) which enables connectivity with the hub.:
sudo npm install node-red-contrib-xiaomi-smart-devices
The node comes with the configuration node and a couple of filters which are useful, but not necessary.
How Xiaomi Gateway in NodeRED works
There are 3 forms of communication that NodeRED will be dealing with. Before I can receive any communication, I have to configure the Xiaomi Node.
name: "Xiaomi Mi Hub" adress: 198.168.xxx.xxx port: 9898 key: "xxxxxxxxxxxxx"
You can obtain all the information from the Mi Home app – providing you have enabled the LAN controls.
I have marked the fields of the importance – the key (password) is listed in “Wireless communication protocol” section.
Once the Xiaomi hub has been configured, you can add the sensor IDs. These are listed as DID (Device ID) in the JSON files. You will also see these each time sensor has posted an update.
If you configured everything correctly, you should see the first message type:
“heartbeat”
Every couple of seconds a message is received containing the most basic information about your Xiaomi hub.
{ "cmd": "heartbeat", "model": "gateway", "sid": "04cfxxxxxx", "short_id": "0", "token": "9jFMt02Nxxxxx", "data": { "ip": "192.168.1.117" } }
You will find there the type of the message (cmd), which in this case is the heartbeat – information about the online status of the device. The DID (sid) of the Xiaomi hub, the IP of the hub and an authentication token.
“report”
This update is received when a sensor sends a message back to the hub. The message will contain information about the type of the sensor, DID of the sensor and the type of the update.
{ "cmd": "report", "model": "sensor_cube.aqgl01", "sid": "158d0xxxxxx", "short_id": 49508, "data": { "status": "alert" } }
“write”
If you want to send a message to the hub (or another device) you have to send an object with the command “write”. The command has to contain the following information:
{ "cmd": "write", "sid": "04cfxxxxx", "model": "gateway", "data": {"mid": 10001, "vol": 20} }
You will need the model and the sid, which refer to the target device. You can send a command to the hub directly, or to a device that the hub controls. The “data” contains another object with the settings you wish to change.
Controlling LED
The Xiaomi gateway comes with an RGB LED which can be used for mood lighting, alarms and notifications. It took me some time to figure out how the colours are set. To set the light you have to pass an object with the following data:
{
"cmd": "write",
"model": "gateway",
"sid": "04cf8xxxxx",
"short_id": 0,
"key": "7",
"data": {
"rgb": 4294921874
}
}
The msg.data.rgb is an integer created from a HEX8 colour value. The colour is composed of 3 RGB channels with hexadecimal values plus alpha: AARRGGBB. The problem is that Xiami Hub requires alpha to be passed at the end: RRGGBBAA.
The function node will do this all for you but should you wish to do this manually here are the steps to follow:
- Obtain HEX8 colour value (use Google Colour Picker)
- Add an alpha channel to the end of the string from the list below
- Convert the value to an integer (use this page)
To turn the LED off simply pass “rgb“: 0 as data – and the light will turn off.
Controlling Sounds
The gateway comes with a set of sounds that you can play. On top of that, you can upload your sounds or even voice recordings. These sounds can be played via NodeRED too. To play the sounds you have to issue an object like this:
{ "cmd": "write", "sid": "04cfxxxxx", "model": "gateway", "data": {"mid": 10001, "vol": 20} }
Apart from the sid and model, you need the mid (tune) and vol (volume) values. These are hardcoded into the hub so you will need a list.
To stop the playback at any point, pass the 10000 value. The custom sounds get the number assigned based on the order in the Mi Home app. The volume setting is self-explanatory and ranges from 0-100%.
I have created a neat NodeRED demo so you can play the sounds. These can be picked from the drop-down menu, played and stopped on request.
Playing custom radios
The Mi Home app allows you to play radios. I found on the internet that you could modify DNS or hosts file on your phone to change the default lists. If you use DNS changer app this will work, but I had no luck with editing hosts files.
[appbox googleplay com.frostnerd.dnschanger]Controlling other devices
I’m leaving this chapter open to modifications, as for now I don’t own (yet) any devices I can control. In the future, I will grab a couple of Xiaomi compatible devices (and IKEA ones since they work with Mi Home too) and will add more detailed control protocols.
Amazon Dash issues
I have heard about the compatibility issues between Amazon Dash node, pcap library and Xiaomi nodes, but I manage to resolve this with:
sudo setcap 'cap_net_raw,cap_net_admin+eip' $(readlink -f $(which node))
Conclusion
I’m glad I can use Xiaomi Gateway in NodeRED without any hacks. It makes sense to add the API support to your device, as there are a lot of folks out there who enjoy integrating their gadgets into incredibly creative and personal home automation systems. I’m looking forward to playing with more devices, and I may even take a shot of programming one Zigbee compatible module myself!