miio: Device can not be discovered

This issue is here to track issues with device discovery, both via miio.devices() and the miio CLI-tool. The network discovery was rewritten in 0.15.0 making it a bit more resilient.

Feel free to add a comment to this issue if you have a device that does not show up when using miio discover.

This issue covers the following old issues: #31, #40 (partially), #84

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Comments: 42 (2 by maintainers)

Most upvoted comments

Indeed, in lib/network.js, I hardcoded the address of the right interface into the _socket.bind call:

this._socket.bind(43434, '192.168.1.225');

instead of

this._socket.bind();

which led to a successful discovery of my Xiaomi device (Mijia bedside lamp).

I also tried to hardcode 0.0.0.0 there, didn’t work. This value is default as per the docs, and apparently it’s indeed used, but doesn’t work for some reason. I don’t know much about UDP, so can’t tell what’s going on here. As a workaround, we can probably use something like the internal-ip module.

My environment:

  • Where miio discover didn’t work: Win 7 x64, node 9.5.0
  • Where it worked: Win 8.1 x32, node 9.4.0, same network

Sorry, I didn’t look hard enough. Now I found the screen. On my phone it was all in English. For anyone else trying to find this, go to the main app screen then:

  1. tap “Mi Home” (bottom left)
  2. tap “All” (top left)
  3. tap on name or icon of light -> light power/color settings page
  4. tap on menu ellipsis (…) at top right -> opens menu
  5. select “General settings” -> General settings page
  6. select “Network info” -> Network info page
  7. read Token value

Does anyone have an update on this? I am unable to discover the xiaomi roborock too, even after a wifi reset. It can be discovered after I set it up in the Mi Home app though.

Hi to everyone, my goal is to control mi air purifier 2 (zhimi-airpurifier-m1) in local network while blocking it by firewall for any outbound connections to xiaomi servers. I have issue with that. When in the WiFi network, there is no internet connection (or outbound tcp:80 is blocked), I am loosing the ability to control and read sensors by mi home app or miio cli commands. I suppose that it is intended bug or feature in the mi airpurifier firmware (1.2.4_60), known as “,” in JSON payload.

When comparing JSON payloads: Internet-on: “mmfree”:25992,“ot”:“ott”,“otu_stat”:[0,0,0,0,0,171] Internet-off: “mmfree”:26576,“otu_stat”:[0,0,0,0,0,171] Device-after-reset: “mmfree”:24896,“otu_stat”:[0,0,0,0,0,0]

, We can see that, in the last two examples ,“ot”:“ott”, filed is empty: “,”.

In the python-miio library this issue was solved: https://github.com/rytilahti/python-miio/blob/master/miio/protocol.py#L167

Is there, a chance, that similar solution could be implemented in the miio library?

I use miio v0.15.6 installed on RPi 3B+ with Raspbian Stretch Lite, node v6.14.2, node-red v0.18.6.

Edit: I found that adding this, in file: safeishJSON.js, have fixed all my issues with miio discover / inspect / configure:

// Case 2 Load for devices fail as they return empty JSON payload fields
// e.g. ,"ot":"ott", = ,, when device is not connected with Xiaomi servers
//str = str.replace(',,"otu_stat"', ',"otu_stat"');
str = str.replace(',,', ',');

Internet-on:

user@xxxx:~ $ DEBUG=thing* miio inspect 192.168.0.9 INFO Attempting to inspect 192.168.0.9 thing:miio:pending <- Handshake reply: <Buffer 17 43 dd 09 91 0f 62 a6 28 8d d3 49 8f ae ac 1a> +0ms thing:miio:5xxxxxx4 Identifier of device updated +0ms thing:miio:5xxxxxx4 Using automatic token: 1743dd09910f62a6288dd3498faeac1a +6ms thing:miio:5xxxxxx4 -> (5) {“method”:“miIO.info”,“params”:[],“id”:1} +7ms thing:miio:5xxxxxx4 <- Message: {"result":{"life":1343,"cfg_time":0,"token":"1743dd09910f62a6288dd3498faeac1a","mac":"xx:xx:xx:xx:xx:xx","fw_ver":"1.2.4_60","hw_ver":"MC200","uid":1xxxxxxxx7,"model":"zhimi.airpurifier.m1","wifi_fw_ver":"SD878x-14.76.36.p79-702.1.0-WM","ap":{"rssi":-71,"ssid":"xxxx","bssid":"xx:xx:xx:xx:xx:xx"},"netif":{"localIp":"192.168.0.9","mask":"255.255.255.0","gw":"192.168.0.1"},"mmfree":27080,"ot":"ott","otu_stat":[0,0,0,0,0,171],"ott_stat":[4, 5, 419, 1883]},"id":1} +78ms thing:miio:5xxxxxx4 -> (5) {“method”:“get_prop”,“params”:[“power”,“mode”,“temp_dec”,“humidity”,“aqi”,“favorite_level”,“filter1_life”,“f1_hour_used”,“use_time”,“led”,“led_b”,“buzzer”],“id”:2} +33ms thing:miio:5xxxxxx4 <- Message: {"result":["on","auto",258,51,9,10,84,558,2010792,"on",0,"on"],"id":2} +90ms thing:miio:5xxxxxx4 Property power changed from undefined to true +0ms thing:miio:5xxxxxx4 Property mode changed from undefined to auto +3ms thing:miio:5xxxxxx4 Property temperature changed from undefined to 25.8 +1ms thing:miio:5xxxxxx4 Property humidity changed from undefined to 51 +1ms thing:miio:5xxxxxx4 Property aqi changed from undefined to 9 +1ms thing:miio:5xxxxxx4 Property favoriteLevel changed from undefined to 10 +1ms thing:miio:5xxxxxx4 Property filterLifeRemaining changed from undefined to 84 +1ms thing:miio:5xxxxxx4 Property filterHoursUsed changed from undefined to 558 +0ms thing:miio:5xxxxxx4 Property useTime changed from undefined to 2010792 +1ms thing:miio:5xxxxxx4 Property led changed from undefined to true +0ms thing:miio:5xxxxxx4 Property ledBrightness changed from undefined to bright +0ms thing:miio:5xxxxxx4 Property buzzer changed from undefined to true +0ms thing:miio:5xxxxxx4 -> (5) {“method”:“miIO.info”,“params”:[],“id”:3} +18ms thing:miio:5xxxxxx4 <- Message: {"result":{"life":1344,"cfg_time":0,"token":"1743dd09910f62a6288dd3498faeac1a","mac":"xx:xx:xx:xx:xx:xx","fw_ver":"1.2.4_60","hw_ver":"MC200","uid":1xxxxxxxx7,"model":"zhimi.airpurifier.m1","wifi_fw_ver":"SD878x-14.76.36.p79-702.1.0-WM","ap":{"rssi":-72,"ssid":"xxxx","bssid":"xx:xx:xx:xx:xx:xx"},"netif":{"localIp":"192.168.0.9","mask":"255.255.255.0","gw":"192.168.0.1"},"mmfree":25992,"ot":"ott","otu_stat":[0,0,0,0,0,171],"ott_stat":[4, 5, 419, 1883]},"id":3} +25ms

Device ID: 5xxxxxx4 Model info: zhimi.airpurifier.m1 Address: 192.168.0.9 Token: 1743dd09910f62a6288dd3498faeac1a via auto-token Support: At least basic

Type info: miio:air-purifier, sensor, miio, air-purifier Capabilities: miio:buzzer, miio:led-brightness, miio:switchable-led, pm2.5, relative-humidity, temperature, switchable-mode, mode, switchable-power, restorable-state, power, state

Firmware version: 1.2.4_60 Hardware version: MC200

WiFi: xxxx (xx:xx:xx:xx:xx:xx) RSSI: -72 WiFi firmware version: SD878x-14.76.36.p79-702.1.0-WM

Remote access (Mi Home App): TCP

Properties:

  • power: true
  • mode: auto
  • temperature: 25.8
  • humidity: 51
  • aqi: 9
  • favoriteLevel: 10
  • filterLifeRemaining: 84
  • filterHoursUsed: 558
  • useTime: 2010792
  • led: true
  • ledBrightness: bright
  • buzzer: true

Internet-off:

user@xxxx:~ $ DEBUG=thing* miio inspect 192.168.0.9 INFO Attempting to inspect 192.168.0.9 thing:miio:pending <- Handshake reply: <Buffer 17 43 dd 09 91 0f 62 a6 28 8d d3 49 8f ae ac 1a> +0ms thing:miio:5xxxxxx4 Identifier of device updated +0ms thing:miio:5xxxxxx4 Using automatic token: 1743dd09910f62a6288dd3498faeac1a +5ms thing:miio:5xxxxxx4 -> (5) {“method”:“miIO.info”,“params”:[],“id”:1} +7ms thing:miio:5xxxxxx4 <- Message: {"result":{"life":1680,"cfg_time":0,"token":"1743dd09910f62a6288dd3498faeac1a","mac":"xx:xx:xx:xx:xx:xx","fw_ver":"1.2.4_60","hw_ver":"MC200","uid":1xxxxxxxx7,"model":"zhimi.airpurifier.m1","wifi_fw_ver":"SD878x-14.76.36.p79-702.1.0-WM","ap":{"rssi":-72,"ssid":"xxxx","bssid":"xx:xx:xx:xx:xx:xx"},"netif":{"localIp":"192.168.0.9","mask":"255.255.255.0","gw":"192.168.0.1"},"mmfree":27240,,"otu_stat":[0,0,0,0,0,171],"ott_stat":[4, 6, 419, 1883]},"id":1} +71ms thing:miio:5xxxxxx4 <- Invalid JSON SyntaxError: Unexpected token , in JSON at position 389 at Object.parse (native) at module.exports (/usr/lib/node_modules/miio/lib/safeishJSON.js:10:15) at DeviceInfo.onMessage (/usr/lib/node_modules/miio/lib/network.js:367:18) at Socket._socket.on.ex (/usr/lib/node_modules/miio/lib/network.js:160:11) at emitTwo (events.js:106:13) at Socket.emit (events.js:191:7) at UDP.onMessage (dgram.js:561:8) +3ms thing:miio:5xxxxxx4 -> (4) {“method”:“miIO.info”,“params”:[],“id”:101} +2s thing:miio:5xxxxxx4 <- Message: {"result":{"life":1682,"cfg_time":0,"token":"1743dd09910f62a6288dd3498faeac1a","mac":"xx:xx:xx:xx:xx:xx","fw_ver":"1.2.4_60","hw_ver":"MC200","uid":1xxxxxxxx7,"model":"zhimi.airpurifier.m1","wifi_fw_ver":"SD878x-14.76.36.p79-702.1.0-WM","ap":{"rssi":-71,"ssid":"xxxx","bssid":"xx:xx:xx:xx:xx:xx"},"netif":{"localIp":"192.168.0.9","mask":"255.255.255.0","gw":"192.168.0.1"},"mmfree":27240,,"otu_stat":[0,0,0,0,0,171],"ott_stat":[4, 6, 419, 1883]},"id":101} +550ms thing:miio:5xxxxxx4 <- Invalid JSON SyntaxError: Unexpected token , in JSON at position 389 at Object.parse (native) at module.exports (/usr/lib/node_modules/miio/lib/safeishJSON.js:10:15) at DeviceInfo.onMessage (/usr/lib/node_modules/miio/lib/network.js:367:18) at Socket._socket.on.ex (/usr/lib/node_modules/miio/lib/network.js:160:11) at emitTwo (events.js:106:13) at Socket.emit (events.js:191:7) at UDP.onMessage (dgram.js:561:8) +2ms thing:miio:5xxxxxx4 -> (3) {“method”:“miIO.info”,“params”:[],“id”:201} +1s thing:miio:5xxxxxx4 <- Message: {"result":{"life":1684,"cfg_time":0,"token":"1743dd09910f62a6288dd3498faeac1a","mac":"xx:xx:xx:xx:xx:xx","fw_ver":"1.2.4_60","hw_ver":"MC200","uid":1xxxxxxxx7,"model":"zhimi.airpurifier.m1","wifi_fw_ver":"SD878x-14.76.36.p79-702.1.0-WM","ap":{"rssi":-73,"ssid":"xxxx","bssid":"xx:xx:xx:xx:xx:xx"},"netif":{"localIp":"192.168.0.9","mask":"255.255.255.0","gw":"192.168.0.1"},"mmfree":26576,,"otu_stat":[0,0,0,0,0,171],"ott_stat":[4, 6, 419, 1883]},"id":201} +384ms thing:miio:5xxxxxx4 <- Invalid JSON SyntaxError: Unexpected token , in JSON at position 389 at Object.parse (native) at module.exports (/usr/lib/node_modules/miio/lib/safeishJSON.js:10:15) at DeviceInfo.onMessage (/usr/lib/node_modules/miio/lib/network.js:367:18) at Socket._socket.on.ex (/usr/lib/node_modules/miio/lib/network.js:160:11) at emitTwo (events.js:106:13) at Socket.emit (events.js:191:7) at UDP.onMessage (dgram.js:561:8) +2ms WARNING Could not find device

Device-after-reset:

user@xxxx:~ $ DEBUG=thing* miio inspect 192.168.13.1 INFO Attempting to inspect 192.168.13.1 thing:miio:pending <- Handshake reply: <Buffer 17 43 dd 09 91 0f 62 a6 28 8d d3 49 8f ae ac 1a> +0ms thing:miio:5xxxxxx4 Identifier of device updated +0ms thing:miio:5xxxxxx4 Using automatic token: 1743dd09910f62a6288dd3498faeac1a +6ms thing:miio:5xxxxxx4 -> (5) {“method”:“miIO.info”,“params”:[],“id”:1} +6ms thing:miio:5xxxxxx4 <- Message: {"result":{"life":103,"cfg_time":0,"token":"1743dd09910f62a6288dd3498faeac1a","mac":"xx:xx:xx:xx:xx:xx","fw_ver":"1.2.4_60","hw_ver":"MC200","model":"zhimi.airpurifier.m1","wifi_fw_ver":"SD878x-14.76.36.p79-702.1.0-WM","ap":{"rssi":-98,"ssid":"�","bssid":"65:15:00:F9:6C:14"},"netif":{"localIp":"36.102.21.0","mask":"129.74.16.0","gw":"249.108.20.0"},"mmfree":25512,,"otu_stat":[0,0,0,0,0,0],"ott_stat":[0, 0, 0, 0]},"id":1} +20ms thing:miio:5xxxxxx4 <- Invalid JSON SyntaxError: Unexpected token , in JSON at position 366 at Object.parse (native) at module.exports (/usr/lib/node_modules/miio/lib/safeishJSON.js:10:15) at DeviceInfo.onMessage (/usr/lib/node_modules/miio/lib/network.js:367:18) at Socket._socket.on.ex (/usr/lib/node_modules/miio/lib/network.js:160:11) at emitTwo (events.js:106:13) at Socket.emit (events.js:191:7) at UDP.onMessage (dgram.js:561:8) +3ms thing:miio:5xxxxxx4 -> (4) {“method”:“miIO.info”,“params”:[],“id”:101} +2s thing:miio:5xxxxxx4 <- Message: {"result":{"life":105,"cfg_time":0,"token":"1743dd09910f62a6288dd3498faeac1a","mac":"xx:xx:xx:xx:xx:xx","fw_ver":"1.2.4_60","hw_ver":"MC200","model":"zhimi.airpurifier.m1","wifi_fw_ver":"SD878x-14.76.36.p79-702.1.0-WM","ap":{"rssi":-98,"ssid":"�","bssid":"65:15:00:F9:6C:14"},"netif":{"localIp":"36.102.21.0","mask":"129.74.16.0","gw":"249.108.20.0"},"mmfree":25512,,"otu_stat":[0,0,0,0,0,0],"ott_stat":[0, 0, 0, 0]},"id":101} +30ms thing:miio:5xxxxxx4 <- Invalid JSON SyntaxError: Unexpected token , in JSON at position 366 at Object.parse (native) at module.exports (/usr/lib/node_modules/miio/lib/safeishJSON.js:10:15) at DeviceInfo.onMessage (/usr/lib/node_modules/miio/lib/network.js:367:18) at Socket._socket.on.ex (/usr/lib/node_modules/miio/lib/network.js:160:11) at emitTwo (events.js:106:13) at Socket.emit (events.js:191:7) at UDP.onMessage (dgram.js:561:8) +1ms thing:miio:5xxxxxx4 -> (3) {“method”:“miIO.info”,“params”:[],“id”:201} +2s thing:miio:5xxxxxx4 <- Message: {"result":{"life":107,"cfg_time":0,"token":"1743dd09910f62a6288dd3498faeac1a","mac":"xx:xx:xx:xx:xx:xx","fw_ver":"1.2.4_60","hw_ver":"MC200","model":"zhimi.airpurifier.m1","wifi_fw_ver":"SD878x-14.76.36.p79-702.1.0-WM","ap":{"rssi":-98,"ssid":"��","bssid":"65:15:00:F9:6C:14"},"netif":{"localIp":"36.102.21.0","mask":"129.74.16.0","gw":"249.108.20.0"},"mmfree":24896,,"otu_stat":[0,0,0,0,0,0],"ott_stat":[0, 0, 0, 0]},"id":201} +12ms thing:miio:5xxxxxx4 <- Invalid JSON SyntaxError: Unexpected token , in JSON at position 367 at Object.parse (native) at module.exports (/usr/lib/node_modules/miio/lib/safeishJSON.js:10:15) at DeviceInfo.onMessage (/usr/lib/node_modules/miio/lib/network.js:367:18) at Socket._socket.on.ex (/usr/lib/node_modules/miio/lib/network.js:160:11) at emitTwo (events.js:106:13) at Socket.emit (events.js:191:7) at UDP.onMessage (dgram.js:561:8) +2ms WARNING Could not find device

@pkol How many active network interfaces does your machine have? For me, miio discover works on a pc with only one interface. However, on another laptop, where I have virtual networks for VMs, it doesn’t work. Apparently, it sends requests or listens to the wrong interface.

When using the debug command (for reference, on Windows it’s set DEBUG=* & miio discover) I’m getting different results but was eventually able to track it down.

I deleted the miio directory under my profile folder which holds the tokens.xml. It was added when manually updating the token on 0.14.1 for a device that is not able to auto-discover the token. After deleting miio discover does find my robot. Not able to control it yet but that’s a different issue. Discovery for the vacuum cleaner is working here again.

@studentIvan Does the app you describe work for iPhone as well or is there an alternative? I can’t seem to download it and AFAIK iPhones do not support .apk files. I have no Android phone nearby to use. Also the token does not show in the regular Mi Home app, is that correct?

Edit: I have managed to get the tokens of my devices now, but when I try to update them for the devices I get the following error: miio ERROR Could not update token, double-check the given token. Does anyone have suggestions as to how I can progress from here?

Edit 2: After some more looking around, I have found a solution that works for me. Perhaps it’ll be useful to anyone who stumbles upon this in the future. The idea is: we need to get the token of the device, but it is hidden in a database file. Once we get this token, we can manually set it using the miio protocol. Hope it helps! In short:

  • (using iPhone) make backup using iTunes, unencrypted. (AFAIK Android devices can see the token in the Mi Home app)
  • Read backup using the program iBackupViewer, navigate to AppDomain > com.xiaomi.mihome > search for _mihome. Select the 1766735698_mihome.sqlite file (numbers may be different) and export it.
  • Open this file using a different program, DB Browser for SQLite. Now you can see the deviceID, IP, name and token. This token is encrypted, however. Should be 96 characters.
  • Decrypt token using http://aes.online-domain-tools.com/, instructions image, you should get a 32-character hex value.
  • Use command in terminal: miio tokens update deviceID --token decryptedTokenAsHex
  • miio discover now shows Token: 82a…baa via stored token

you saved me man. thanks. I searched for this token 4 hours.