Xiaomi sensors/appliances #1067
Replies: 13 comments
-
Posted at 2019-04-26 by @gfwilliams This is great - thanks for posting it up separately to your other stuff! I've been meaning to try one of those Xiaomi Plant moisture sensors for ages, and this gives me a good excuse :) If you're interested, you should be able to change the last part of
which will automatically filter out only devices that advertise that service data - it's a bit faster and more efficient with memory if there are a bunch of BLE devices. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-03-17 by Andrea I have a Xiaomi LYWSD03MMC Thermometer / Hygrometer and with this CircuitPython library I can fetch its values: Now I'd like to make everything work on Espruino thanks to your code. I changed filters: [{serviceData: {"fe95": {}}}] the device is found (although its serviceData should be "ebe0ccb0") but all values remain null. How do you think it could be solved? THANK YOU |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-03-17 by @gfwilliams Which device are you wanting to run this on? There's actually a Bangle.js app for this already at: https://github.com/espruino/BangleApps/blob/master/apps/miplant/app.js So to run on another device you could just copy The code posted above (using |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-03-17 by Andrea Hi @gordon, thanks for the reply. As soon as I have time, I try the code on my Pixl.js and tell you! See you soon :-) |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-03-18 by @fanoush
Guess you already know but there is alternative firmware for this thermometer, it is pretty hackable device (Telink 8251 - 32K ram,512K flash). Was started by atc1441 and there is now nice fork here https://github.com/pvvx/ATC_MiThermometer with many additional features like logging data to flash. So if you would have issues with original Xiaomi firmware (like encrypted advertisement data) this one may be easier to work with. Also flashing that Telink chip is easy (both OTA and SWS over serial). |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-03-18 by Andrea Hi @gordon, I uploaded this code to Pixl.js (2v08)
It always prints FRAME, and when the temperature or humidity changes it prints the event:
I have no idea how to extract the temperature and humidity :-) Can I get some useful info from this Python code (0x80, 0x7F)? THANK YOU |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-03-18 by Andrea Hi @fanoush, thanks for the link, I initially saw something about: https://github.com/atc1441/ATC_MiThermometer Then I saw that through Python, without any hacks, you can get the values easily. Maybe later, I sacrifice one in the name of science :-) |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-03-18 by @fanoush
Yes, definitely optional thing for later. As for sacrifices - it should be possible to restore it either over BLE or at worst it should be always recoverable via SWS interface - pin on board. This is similar debugging/programming interface to ARM SWD but thanks to clever hack you even don't need special HW for recovery, the right data for SWS can be generated in software and sent via serial uart. Anyway, they are pretty cheap after all (below $4/piece when getting 4 of them https://www.aliexpress.com/item/4001095735569.html ) so even if completely broken it is not that bad :-) |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-03-18 by @gfwilliams The adafruit code appears to actually connect to the thermometer rather than just looking at advertisements, so I don't know how useful that is. It looks to me like the advertising is using a different format to the one other Xiaomi thermometers use. It's probably better to do:
And then show us everything it prints over the course of a few minutes, with a rough idea of the temperature and humidity the device is showing - and we might be able to work out how to decode it then |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-03-18 by @fanoush
That's because data in advertisement is encrypted, see https://github.com/Magalex2x14/LYWSD03MMC-info#encryption When connected there is no such encryption! And any device can connect without pairing so it all makes very little sense. That's why I suggested alternative firmware - there is no such encryption nonsense there. And the firmware was developed from scratch (with Telink SDK available on github) and is opensource. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-03-19 by Andrea Hi @fanoush, you are right, in the next few days I will hack one :-) See you soon! |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-03-20 by Andrea Hi, with the custom firmware everything works perfectly! After flashing, following these directions: https://github.com/pvvx/ATC_MiThermometer @vlad's code fetches values without problems:
THANK YOU ALL! |
Beta Was this translation helpful? Give feedback.
-
Posted at 2021-03-22 by @gfwilliams Thanks for the update, and glad it's all working ok! |
Beta Was this translation helpful? Give feedback.
-
Posted at 2019-04-26 by Vlad
Hi all, just want to share a code snippet and some thoughts/ideas on how to interact with Xiaomi devices.
There are a few Xiaomi devices that have internal bluetooth module, e.g.:
Xiaomi uses its own proprietary authentication protocol in their devices. The protocol logic is encapsulated into a native shared library (Android JNI library) that is used to generate/encode/decode authentication tokens which must be exchanged with the central device. Xiaomi devices only allow a very short lived connection while they are expecting to receive an authentication token. Once authentication fails, device drops its connection. More info on the Xiaomi authentication protocol can be found [here] (sputnikdev/eclipse-smarthome-bluetooth-binding#19 (comment)). Fortunately, Xiaomi devices actively advertise their data which can be easily received and decoded.
It turns out that all Xiaomi devices (that I'm aware of) use a common protocol/data structure for their advertisement messages that can be easily decoded, hence no need in establishing any direct connection with the device. I've decoded them and came up with some custom GATT definitions that describes the protocol and data structure here and here.
Here is a code snippet that shows how to decode Temperature and Humidity sensor data:
As I mentioned, all devices share the same notification data structure, hence it is easy to come up with very similar code that would decode advertisements from other Xiaomi devices by following the gatt specs that I provided.
PS. The provided gatt spec files is a part of Gatt Parser, Java Bluetooth Manager and Eclipse SmartHome binding that I'm working on, you may find it useful too. The official discussion thread here.
Let me know if you have any questions.
Beta Was this translation helpful? Give feedback.
All reactions