Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support LYWSD03MMC new mijia temp/humidity sensor #552

Closed
aidbish opened this issue Jan 5, 2020 · 367 comments
Closed

Support LYWSD03MMC new mijia temp/humidity sensor #552

aidbish opened this issue Jan 5, 2020 · 367 comments
Milestone

Comments

@aidbish
Copy link

aidbish commented Jan 5, 2020

Describe the problem you have/What new integration you would like
Add support for the mijia Temperature/Humidity Sensor LYWSD03MMC

Please describe your use case for this integration and alternatives you've tried:
These are an updated version of the sensors from xiaomi mijia, and are small compact devices that can be used in discreet places

Additional context
Work has been done in a number of places to support them, that maybe of assistance
custom-components/ble_monitor#7

https://github.com/JsBergbau/MiTemperature2/

thankyou

@Trillon008
Copy link

The LYWSD03MMC is incredible value for the price and could fit gloriously in any smart home. But, following the links you provided it seems it doesn't work like any other Xioami device and doesn't broadcast his data but instead requires a direct connection. This will probably cause issues when multiples devices are concerned and ultimately consume more battery power.

@vevsvevs
Copy link

vevsvevs commented Jan 11, 2020

@Trillon008 no, it sends ADV-packets as well, but they're encrypted :)

@Joshfindit
Copy link

(Just got mine a couple hours ago, and super interested in pulling the data in)

There is a fundamental shift in how it operates (seems to require pairing where the previous versions broadcast to the open air), but the linked python script is able to read the data without issue.
It seems likely to me that a new xiaomi_lywsd03mmc sensor would have to be created that supports pairing (and hopefully supports simultaneous pairing of, say, 3-6 devices) and then listens for data from the paired devices.

@Joshfindit
Copy link

Likely standard BLE encryption, and since esphome is the master device, there would be no need to break the encryption

@vevsvevs
Copy link

But esp32_ble_tracker has no master/slave approach, it passively listening.

@Joshfindit
Copy link

@vevsvevs Yes, that’s what I meant: there would have to be a separate esp32_ble_tracker that supports pairing in order to provide functionality for this sensor

@Joshfindit
Copy link

Apologies if this looks like advertising, I’m really just trying to help dev results.
They went on sale on banggood https://banggood.app.link/3oEGAJptH3

@Monacoslo
Copy link

Anything new regarding the use in HA?

@vevsvevs
Copy link

vevsvevs commented Mar 4, 2020

Meanwhile, the ADV-payload decryption method for this sensor is already known. I hope some high-skilled contributor will be so kind to implement support of this sensor soon :)

@aidbish
Copy link
Author

aidbish commented Mar 4, 2020

@vevsvevs Yes it was found, but only a few hours ago. Give people a chance to look into it

@gbarneda
Copy link

Anything new? Meanwhile i've found this but I don't know how to use it

@uKL
Copy link

uKL commented Mar 11, 2020

Anything new? Meanwhile i've found this but I don't know how to use it

This one is based on GATT connection and notifications while ESPHome way is to listen to advertised packets.

@rufik
Copy link

rufik commented Mar 11, 2020

I think HA custom component mitemp_bt has found some way to fetch data passively, see custom-components/ble_monitor#7 (comment)
It looks like some paring with MiHome app is required first, then encrypt key extraction, but done once can be used in esphome config I think :)

@ahpohl
Copy link

ahpohl commented Mar 14, 2020

I bought 8 of the new LYWSD03MMC mijia temp/humidity sensors and 3 ESP32 dev boards to be used with ESPHome. I would really like to get the sensors directly supported in ESPHome. I have collected a full Android HCI dump while pairing the devices in the Xiaomi Home App and looked at it in Wireshark , but don't know how to proceed from here. I would really appreciate if somebody more skilled than me could step in.

It looks like some paring with MiHome app is required first, then encrypt key extraction, but done once can be used in esphome config I think

It would be really nice if that would be possible and hopefully the encryption key does not change over time.

@Joshfindit
Copy link

It looks like some paring with MiHome app is required first, then encrypt key extraction, but done once can be used in esphome config I think

It would be really nice if that would be possible and hopefully the encryption key does not change over time.

Hopefully with the right people involved, ESPHome could be the one to do the initial pairing, making extracting the key much simpler.

@ahpohl
Copy link

ahpohl commented Mar 15, 2020

What are the next steps? I can read the temperature and humidity with this python script, but only when connecting to the device. I collected a clean dump of the conversation and can see the raw sensor values in the Notification, Handle 0x0036 as described in the README.

But that is not what I need. I need to passively collect the data simultaneously from eight sensors with just two ESP32 (flashed with ESPHome connected via MQTT to HA and Grafana). As I understand, the device is constantly sending the data, but the data is encrypted. How do I extract the encryption key from the raw dump?

Meanwhile, the ADV-payload decryption method for this sensor is already known. I hope some high-skilled contributor will be so kind to implement support of this sensor soon :)

It sounds as it is actually possible!

@ahpohl
Copy link

ahpohl commented Mar 19, 2020

Obtaining the bind-key for ADV-payload decryption is quite simple following the guides described in the above mentioned project custom-components/sensor.mitemp_bt. I have extracted the bind-keys for all my eight LYWSD03MMC sensors and they are working fine in HA. However, I want to distribute them around the house and therefore need the ESP32s with ESPHome for the bluetooth connectivity.

I had a quick look at the python code in sensor.py, which emulates hcitool, constantly scans for the broadcasted ADV packets and automatically decrypts the payload with the temperature and humidity values. This is basically what we need without the HA code, so an experienced programmer needs to translate the existing python code into Arduino C++ code to be included into ESPHome. Unfortunately I am just starting with bluetooth programming, so somebody more experienced is welcomed to step in.

I hope the sensor will get eventually supported in ESPHome.

@clau-bucur
Copy link

I also thought I'd need ESP32 or some more bluetooth adapters to be able to cover the entire house. But it seems I don't, after all. One USB dongle works perfectly.
The farthest sensor from the dongle is about 5m apart on a different level (about 1.5m height difference), separated by concrete and brick walls. I was really surprised it was read.
However your situation might be entirely different, but you can give it a try.
You can also use multiple Bluetooth dongles with mitemp_bt so you can have USB-to-Ethernet extenders (I use these, they work nicely) and place some around the house.

@ahpohl
Copy link

ahpohl commented Mar 20, 2020

you can have USB-to-Ethernet extenders (I use these, they work nicely) and place some around the house.

Interesting, I didn't know that it is possible to extend the USB via Ethernet cables. It would indeed be a possibility for me as I have unused RJ45 outlets in every room. But I prefer to use the ESP32s as they are more versatile and also more fun (and I already have bought all hardware).

I started to look into the ESPHome pyhton and C++ code and how to implement the new sensor component. It seems that the esp32_ble_tracker component needs to modified to include the payload decryption and also a new sensor configuration needs to be created.

I am just starting to learn ESPHome and bluetooth programming. So it will take me a while to figure it out if I have to do it all by myself. But I'll give it a try.

@Magalex2x14
Copy link

@ahpohl Feel free to ask any questions about the operation of the custom component mentioned above and the technical details about LYWSD03MMC - I will help you all I can.

@ahpohl
Copy link

ahpohl commented Mar 23, 2020

@Magalex2x14 Thanks for offering your help. In the meantime I have successfully created a standalone python script mitemp_standalone.py from your HA custom component sensor.py to get familiar with bluetooth low energy programming.

The esp32_ble_tracker component is another story though. As far as I can tell, the function ESPBTDevice::parse_adv_ does the parsing of the packets. It needs a lot more code to detect the encrypted payload, decrypt and parse the output, and probably more classes or functions. I found an AES library for the ESP32, not sure if it is compatible with FreeRTOS and will be of any use.

Which IDE do you recommend for coding? How do you test the new code? I find it difficult to develop for ESP32 when I cannot use the Arduino IDE directly.

@ahpohl
Copy link

ahpohl commented Mar 23, 2020

The Crypto++ library includes AES-CCM with CBC-MAC and is more likely what we need. Another option would be Botan, which looks more modern and easier to start with.

@Magalex2x14
Copy link

@ahpohl I use Visual Studio Code with addons like python linters, formatters, etc.

For VSCode there are also C++ and Platformio addons, and I used them when I made a custom component for working with oled display on my ESP32 board. Unfortunately, this is where my knowledge of ESPHome architecture ends... At the moment, I'm a bad assistant in this...

Just in case, here is a link to the esphome development environment setting up.

@Monacoslo
Copy link

https://community.home-assistant.io/t/xiaomi-passive-ble-monitor-sensor-platform/177352/144

this is great component, but I need working solution for ESP32...

@ahpohl
Copy link

ahpohl commented Apr 6, 2020

Development of a xiaomi_lywsd03mmc custom component for ESPHome has been initiated. The C++ decryptor works and the code can be used to extend xiaomi_ble. I already created the new sensor type xiaomi_lywsd03mmc, but unfortunately the new platform is not recognised during build.

@Monacoslo
Copy link

even in 1.5 dev not?

@ahpohl
Copy link

ahpohl commented Apr 7, 2020

even in 1.5 dev not?

@Monacoslo: Could you please explain what you mean with this comment?

I created a new platform xiaomi_lywsd03mmc (in a new brach with the same name) but get this when compiling the firmware

INFO Reading configuration livingroom.yaml...
Failed config

sensor.xiaomi_lywsd03mmc: [source livingroom.yaml:26]
  
  Platform not found: 'sensor.xiaomi_lywsd03mmc'.
  platform: xiaomi_lywsd03mmc
  mac_address: A4:C1:38:8C:34:B7
  temperature: 
    name: Temperature
  humidity: 
    name: Humidity
  battery_level: 
    name: Battery Level

EDIT: problem solved. I was not installing the new xiaomi_lywsd03mmc platform into my virtualenv after creating it. Now it recognizes the new platform!

@ppanagiotis
Copy link

even in 1.5 dev not?

@Monacoslo: Could you please explain what you mean with this comment?

I created a new platform xiaomi_lywsd03mmc (in a new brach with the same name) but get this when compiling the firmware

INFO Reading configuration livingroom.yaml...
Failed config

sensor.xiaomi_lywsd03mmc: [source livingroom.yaml:26]
  
  Platform not found: 'sensor.xiaomi_lywsd03mmc'.
  platform: xiaomi_lywsd03mmc
  mac_address: A4:C1:38:8C:34:B7
  temperature: 
    name: Temperature
  humidity: 
    name: Humidity
  battery_level: 
    name: Battery Level

EDIT: problem solved. I was not installing the new xiaomi_lywsd03mmc platform into my virtualenv after creating it. Now it recognizes the new platform!

I compile the firmware using your branch but the response is empty:
xiaomi_lywsd03mmc:013]: Temperature 'Temperature'
Are there any other steps or settings I have to to/add?
Thank you very much!

@ahpohl
Copy link

ahpohl commented Apr 7, 2020

@ppanagiotis its not quite finished yet. The new platform is currently just a template. A lot more work needs to be done, e.g. creating a new config item encryption_key, adding the decryptor code etc.

@kosyaky
Copy link

kosyaky commented Oct 24, 2020

EDIT: problem solved. I was not installing the new xiaomi_lywsd03mmc platform into my virtualenv after creating it. Now it recognizes the new platform!

how to install a new platform?

"Failed config

sensor.xiaomi_lywsd03mmc: [source config/ble_tracker.yaml:38]

Platform not found: 'sensor.xiaomi_lywsd03mmc'.
platform: xiaomi_lywsd03mmc"

@rvanderven
Copy link

EDIT: problem solved. I was not installing the new xiaomi_lywsd03mmc platform into my virtualenv after creating it. Now it recognizes the new platform!

how to install a new platform?

"Failed config

sensor.xiaomi_lywsd03mmc: [source config/ble_tracker.yaml:38]

Platform not found: 'sensor.xiaomi_lywsd03mmc'.
platform: xiaomi_lywsd03mmc"

Hi,
I had the same issue.
I installed esphome via 'pip install esphome' some time ago. I have a recent 2nd HA installation where I installed esphome as well. I found out the both esphome versions were not the same.
I thought I used the proper update method for esphome, but apparently not.
Today I searched for it and I needed to use 'pip3 install -U esphome' to update esphome and this did the trick.
I had to kill and start the esphome process which I normally start with: 'nohup esphome /home/pi/homeassistant/config/ dashboard > /home/pi/homeassistant/esphome.log &'.
So now I am able to compile the new esphome entry with the new sensor xiaomi.lywsd03mmc.
Hope this helps.
PS: Don't forget to connect to the sensors and set the Advertising Type to "Mi Like", otherwise you will not see any data in esphome. For one sensor I had to do this mulitple times before receiving data.

@kosyaky
Copy link

kosyaky commented Nov 1, 2020 via email

@havrancek
Copy link

i am a noob
i flashed custom firmware, set mi like advertising but when i am trying to reconnect in mi home i always get this error:
1, device connected
2, couldnt verify (-28)
what should i do to be able to connect it back again?

@nikito7
Copy link

nikito7 commented Dec 1, 2020

what should i do to be able to connect it back again?

Flash stock firmware

https://github.com/atc1441/ATC_MiThermometer#stock-firmware

@havrancek
Copy link

thanks, i have one more noob question: why is it not showing in google home app when i linked both accounts? is there a way to do it?

@rockas69
Copy link

Hi Folks, already have 2 of these wonders working on home assistant through ESP, however having issues an getting data from 3 sensor.
Have forced a few times "mi like" advertising however sill not getting data out of sensor into esp32.

Any ideas?

@ahpohl
Copy link

ahpohl commented Dec 10, 2020

Have you tried restoring the stock firmware? Then just generate the encryption key with the TelinkFlasher and try without actually flashing the custom firmware. It should work if the sensor itself is ok.

@rockas69
Copy link

sorted, problem is that sometimes when you press "mi like" doesn't assume config and you don't see any error, press some dozen times and it's done :)

@miastekpl
Copy link

I have a question for you, will I be able to connect 4 sensors under ESP32 and Xiaomi Home simultaneously by pulling out Bindkey https://atc1441.github.io/TelinkFlasher.html?
Another question, what configuration to use after changing the sensor software in ESP32?
Thanks for the help.

@KoalaBear84
Copy link

KoalaBear84 commented Dec 11, 2020

@miastekpl It is no problem, you can add as many as you want.

You don't need to change anything. You can leave the current binkkey as it is, as it only needs to have the correct format.

@miastekpl
Copy link

@miastekpl Nie ma problemu, możesz dodać tyle, ile chcesz.

Nie musisz niczego zmieniać. Możesz pozostawić bieżący binkkey bez zmian, ponieważ musi mieć tylko prawidłowy format.

So if I read Bindkey TelinkFlasher and put it in a file in ESP32, I will have sensors in Home Assistant and in the Xoaimi Home application?
Got it right?
Thanks for the help.

@KoalaBear84
Copy link

I think when you flash it with custom firmware, you cannot access it anymore in Xoaimi Home app.

@miastekpl
Copy link

I have a plan, add sensors to the Xiaomi Home app, read Bindkey with TelinkFlasher and then put this bindkey into ESP32, can it work? Any of you did it?

@KoalaBear84
Copy link

Ahh. Sorry, I did it with the old method (the custom Android app) with the official stock firmware.

Moved to custom firmware a couple of days ago.

Looks like custom firmware cannot be used with Mi Home app.

After flashed custom firmware, I cannot detect them with Mi Home App · Issue #124 · atc1441/ATC_MiThermometer (github.com)

@miastekpl
Copy link

Ahh. Przepraszam, zrobiłem to starą metodą (niestandardowa aplikacja na Androida) z oficjalnym oprogramowaniem sprzętowym.

Kilka dni temu przeniesiono do niestandardowego oprogramowania układowego.

Wygląda na to, że niestandardowego oprogramowania układowego nie można używać z aplikacją Mi Home.

Po flashowaniu niestandardowego oprogramowania układowego nie mogę ich wykryć za pomocą aplikacji Mi Home · Problem # 124 · atc1441 / ATC_MiThermometer (github.com)

That's right, after changing the firmware, you will not connect the Xiaomi Home app with the sensors.
Hence my question, is it possible to do with the factory software as I wrote in the previous message.
Does Bindkey change every time I try to connect even TelinkFlasher, because if TelinkFlasher only reads the current Bindkey, then the connection to the phone in the Xiaomi Home application and to ESP32 should work - this is my way of thinking. How does it look in practice ? Now I cannot access these sensors and cannot check.

@KoalaBear84
Copy link

Ahh, only use the "Do Activation" button.. Maybe. I don't see a similar issue/question on the original repository.

Please, create an issue with this question here so we can find out: Issues · atc1441/ATC_MiThermometer (github.com)

@miastekpl
Copy link

Ach, użyj tylko przycisku „Do Activation”… Może. Nie widzę podobnego problemu / pytania w oryginalnym repozytorium.

Utwórz tutaj problem z tym pytaniem, abyśmy mogli się dowiedzieć: Problemy · atc1441 / ATC_MiThermometer (github.com)

Thank you very much for your answer.
So if I understand correctly after pressing "Do Activation" it will not change Bindkey, it will only read it and I will be able to connect to the ESP32 sensor?

@KoalaBear84
Copy link

I'm not sure, have no experience with it. Please ask this through an issue here: Issues · atc1441/ATC_MiThermometer (github.com)

@miastekpl
Copy link

I just wrote a report there, if I get an answer, I will share it with you.

@KoalaBear84
Copy link

Great. I'll watch the ticket, for the rest, here is the ticket: Connecting sensors to ESP32 · Issue #126 · atc1441/ATC_MiThermometer (github.com)

@atc1441
Copy link

atc1441 commented Dec 11, 2020

Hey. The BindKey does change the moment you click on do activation.

This is also mentioned in the Telink flasher

@miastekpl
Copy link

Got it, is there any way to read an already existing Bindkey without changing it? Because that would solve a lot of problems.

@ahpohl
Copy link

ahpohl commented Dec 14, 2020

Yes, there is. But only the "traditional way" by sniffing the bind key when the device is connected to the Xiaomi cloud.

@pvvx
Copy link

pvvx commented Jan 28, 2021

Reading bindkey by reprogramming to custom firmware and back to original.
https://github.com/pvvx/ATC_MiThermometer/tree/master/img

@github-actions github-actions bot locked and limited conversation to collaborators Oct 23, 2021
@nagyrobi nagyrobi removed the good first issue Good for newcomers label Nov 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests