Xiaomi Gateway in NodeRED

Making Xiaomi Hub yours!

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.

How to enable Xiaomi's Mijia hub dev menu
  1. Navigate to Hi Home app – select the Mi Control Hub
  2. Tap 3 dot menu in the top right corner – select “About
  3. At the bottom start tapping “Plug-in version” until the additional menu appears
  4. Enable “Wireless Communication Protocol

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.

Sample JSON config
{
	"life": 11796,
	"cfg_time": 0,
	"token": "xxxxxxxxxx",
	"mac": "04:CF:8C:xx:xx:xx",
	"fw_ver": "1.4.1_161",
	"hw_ver": "MW300",
	"model": "lumi.gateway.v3",
	"mcu_fw_ver": "0143",
	"wifi_fw_ver": "SD878x-14.76.36.p84-702.1.0-WM",
	"ap": {
		"rssi": -60,
		"ssid": "SlowFrog",
		"bssid": "60:38:E0:xx:xx:xx"
	},
	"netif": {
		"localIp": "192.168.1.117",
		"mask": "255.255.255.0",
		"gw": "192.168.1.1",
		"gw_mac": "60:38:xx:xx:xx:xx"
	},
	"mmfree": 179072,
	"ot": "otu",
	"otu_stat": [
		207,
		206,
		142,
		6,
		136,
		291
	],
	"ott_stat": [
		0,
		0,
		0,
		0
	]
}


[
	{
		"model": "lumi.sensor_cube.aqgl01",
		"did": "lumi.158d0xxxx",
		"name": "Aqara Cube2"
	},
	{
		"model": "lumi.sensor_ht.v1",
		"did": "lumi.158d00xxxx",
		"name": "Mi Temperature and Humidity Sensor"
	}
]

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.

At first, I had issues receiving information from my temperature sensor. If you are not able to see any updates from your sensors, simply remove it from the Mi Home app and add the sensor again – this should fix the issue.

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:

  1. Obtain HEX8 colour value (use Google Colour Picker)
  2. Add an alpha channel to the end of the string from the list below
  3. Convert the value to an integer (use this page)
Alpha Channel - 0-100% in HEX
Alpha Level HEX Code
100% FF
95% F2
90% E6
85% D9
80% CC
75% BF
70% B3
65% A6
60% 99
55% 8C
50% 80
45% 73
40% 66
35% 59
30% 4D
25% 40
20% 33
15% 26
10% 1A
5% 0D
0% 00

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.

List of sounds
stop 10000
custom1 10001
custom2 10002
police car 1 0
police car 2 1
accident 2
countdown 3
ghost 4
sniper rifle 5
battle 6
air raid 7
bark 8
doorbell 10
knock at a door 11
amuse 12
alarm clock 13
mimix 20
enthusiastic 21
guitar classic 22
ice world piano 23
leisure time 24
childhood 25
morning stream list 26
music box 27
orange 28
thinker 29

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.

Instructions
  1. Download DNS Changer app
  2. Set DNS IP to 82.146.38.202
  3. Open MiHome and navigate to Radios
  4. Select “National Radios”
  5. Add radios as favourites

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!

Support NotEnoughTech
A lot of time and effort goes into keeping NotEnoughTech alive! If my work helped you out, consider buying me a coffee or check out exclusive rewards available to Patreon supporters.
SHARE