【IoT】获取米家设备 token

Posted by 西维蜀黍 on 2021-05-16, Last Modified on 2024-02-26

获取access token

[Better] XIAOMI CLOUD TOKENS EXTRACTOR

Manual run in python

$ git clone https://github.com/PiotrMachowski/Xiaomi-cloud-tokens-extractor.git

$ virtualenv Xiaomi-cloud-tokens-extractor -p python2.7

$ cd Xiaomi-cloud-tokens-extractor

$ source bin/activate

Install dependencies:

$ pip3 install pycryptodome pybase64 requests

Download and run script:

$ python3 token_extractor.py

Execute following command:

bash <(curl -L https://github.com/PiotrMachowski/Xiaomi-cloud-tokens-extractor/raw/master/run.sh)

Approach 2

重置设备,重置成功后,小米设备会暴露一个可连接的WIFI,用macOS连接这个WIFI,然后执行下面的操作来获取 access token:

python3 -m venv mymiio
$  cd mymiio
$  source bin/activate
$  pip3 install python-miio

$ mirobo discover --handshake 1
INFO:miio.miioprotocol:Sending discovery to <broadcast> with timeout of 5s..
INFO:miio.miioprotocol:  IP 192.168.4.1 (ID: 0733a28e) - token: b'f8202c54d81691015eafb661c26ecc00'
INFO:miio.miioprotocol:Discovery done

For some devices (e.g. the vacuum cleaner) the automatic discovery works only before the device has been connected over the app to your local wifi. This does not work starting from firmware version 3.3.9_003077 onwards, in which case the procedure shown in Tokens from backups has to be used to obtain the token.

This method can also be useful for devices not yet connected to any network. In those cases the device trying to do the discovery has to connect to the network advertised by the corresponding device (e.g. rockrobo-XXXX for vacuum)

Tokens full of 0s or fs (as above) are either already paired with the mobile app or will not yield a token through this method. In those cases the procedure shown in Tokens from Mi Home logs has to be used.

在获取到token后,在手机App连接到该小米设备提供的WIFI,然后在MI Home App中设置(将该小米设备连接到家里的已连接Internet的WIFI),已使该小米设备能在MI Home App中管理。

拿到该小米设备的IP(当然你也可以在你的WIFI路由器里拿到)。

$  miiocli device --ip 192.168.4.1 --token 0126e4b90fa18cbbd4763e36163de78a info
Model: chunmi.cooker.normal2
Hardware version: ESP8266
Firmware version: 1.3.6
Network: {'localIp': '0.0.0.0', 'mask': '0.0.0.0', 'gw': '0.0.0.0'}
AP: {'rssi': 31, 'ssid': 'OpenWrt-2.4G', 'bssid': '8C:53:C3:E3:37:C7'}

通过 miiocli 与设备通讯

refer to https://swsmile.info/post/iot-mi-device-integrate-with-home-assistant/

Reference