Github Boards Devices Community Docs Blog

Xiaomi Mijia LYWSD03MMC ESP32 support

@floyddotnet is working on it but encountered several issues, once implemented it generates instability.
It’s currently a work in progress

2 Likes

Maybe it’s worth to look at:


and:
https://tasmota.github.io/docs/Bluetooth/

Does someone could test this implementation and see if it is working:

The one you refer doesn’t seem to work for me. However the one below did work, but very unreliable as of my 3 temp sensors only 1 was read and very sporadically the other 2. https://github.com/polclota/esp32lywsd03mmc As I have only 1 esp32 around, it could be I have a bad unit and others might have better results.

Thanks for the link and your test! If somebody else can provide feedback on both links it would be helpfull.

The link from polclota works

Attempting MQTT connection as: esp32BleHub_84F574AE11_6717, connected!
i: 2 s: 3, done.
Status topic: esp32blehub_84f574ae11/status/on
{"Timestamp":"2020-07-21T17:47:29.000Z","MAC":"4C:11:AE:74:F5:84","Bootcount":1,"Option":0,"On_secs":20,"Updatetime":180,"SensorName":"Eetkamer","SensorAddress":"A4:C1:38:C9:BC:21","SSID":"dd-wrt","BSSID":"00:14:C1:38:CD:CC","IP":"10.0.0.57","RSSI":-61}, done.
Subscribed to: esp32BleHub_84F574AE11/cmd
Updating BLE device: Eetkamer (1), A4:C1:38:C9:BC:21
Connected!
-Found our service: ebe0ccb0-7a0a-4b0c-8a1a-6ff2997da3a6
-Found our characteristic: ebe0ccc1-7a0a-4b0c-8a1a-6ff2997da3a6
Waiting for notifications!
Notify callback from: Eetkamer (1), characteristic ebe0ccc1-7a0a-4b0c-8a1a-6ff2997da3a6
esp32blehub_84f574ae11/sensor/eetkamer_temperature/state: 24.46, published!
esp32blehub_84f574ae11/sensor/eetkamer_humidity/state: 56.00, published!
esp32blehub_84f574ae11/sensor/eetkamer_battery/state: 69.40, published!
esp32blehub_84f574ae11/sensor/eetkamer_voltage/state: 2.79, published!
Status topic: esp32blehub_84f574ae11/status/off
{"Timestamp":"2020-07-21T17:47:46.000Z","MAC":"4C:11:AE:74:F5:84","Bootcount":1,"Option":0,"On_secs":37,"Result":"published!"}, done.
...
Attempting MQTT connection as: esp32BleHub_84F574AE11_118e, connected!
Status topic: esp32blehub_84f574ae11/status/on
{"Timestamp":"2020-07-21T17:50:58.000Z","MAC":"4C:11:AE:74:F5:84","Bootcount":2,"Option":1,"On_secs":50,"Updatetime":180,"SensorName":"Keuken","SensorAddress":"A4:C1:38:50:68:61","SSID":"dd-wrt","BSSID":"00:14:C1:38:CD:CC","IP":"10.0.0.57","RSSI":-60}, done.
Subscribed to: esp32BleHub_84F574AE11/cmd
Updating BLE device: Keuken (2), A4:C1:38:50:68:61
Connected!
-Found our service: ebe0ccb0-7a0a-4b0c-8a1a-6ff2997da3a6
-Found our characteristic: ebe0ccc1-7a0a-4b0c-8a1a-6ff2997da3a6
Waiting for notifications!
Notify callback from: Keuken (2), characteristic ebe0ccc1-7a0a-4b0c-8a1a-6ff2997da3a6
esp32blehub_84f574ae11/sensor/keuken_temperature/state: 24.43, published!
esp32blehub_84f574ae11/sensor/keuken_humidity/state: 56.00, published!
esp32blehub_84f574ae11/sensor/keuken_battery/state: 86.70, published!
esp32blehub_84f574ae11/sensor/keuken_voltage/state: 2.97, published!
Status topic: esp32blehub_84f574ae11/status/off
{"Timestamp":"2020-07-21T17:51:06.000Z","MAC":"4C:11:AE:74:F5:84","Bootcount":2,"Option":1,"On_secs":58,"Result":"published!"}, done.

The first one is not a full program, and my knowledge of platformio is not so great that I could create one :frowning:

Thanks for the feedback @francisp

For info this integration is making big progress, I’m expecting to publish a PR this week.

Here is the implementation for tests purposes:

Now during the scan OMG store the different models of devices, after the scan if we have found a LYWSD03MMC we connect to it and retrieve the data. The time between each connect is BLEinterval

This implementation helped me, thanks to @jschaeke and @francisp for the identification and the tests.

It is now integrated into development:

it works, but not very reliable. Sometimes I get temperature, sometimes distance.

N: BLE Connect begin
N: Subject: /BTtoMQTT/A4C138C9BC21
N: Received json : {"model":"LYWSD03MMC","id":"A4:C1:38:C9:BC:21","tempc":24.63,"tempf":76.334,"hum":59,"volt":2.585,"batt":48.5}
N: Subject: /BTtoMQTT/A4C138A009B5
N: Received json : {"model":"LYWSD03MMC","id":"A4:C1:38:A0:09:B5","tempc":24.73,"tempf":76.514,"hum":59,"volt":2.322,"batt":22.2}
N: BLE Connect end
N: Subject: /BTtoMQTT/A4C138506861
N: Received json : {"model":"LYWSD03MMC","id":"A4:C1:38:50:68:61","tempc":24.57,"tempf":76.226,"hum":60,"volt":2.823,"batt":72.3}
N: Scan begin
N: Found 0 devices, scan number 31 end deinit controller
N: Subject: /SYStoMQTT
N: Received json : {"uptime":2042,"version":"version_tag","freemem":182112,"rssi":-36,"SSID":"SNOW","ip":"10.0.0.113","mac":"4C:11:AE:74:F5:84","wifiprt":0,"lowpowermode":0,"interval":55555,"scanbcnct":10,"modules":"BTHADiscovery"}
N: Scan begin
N: Device detected: A4:C1:38:A0:09:B5
N: Subject: /BTtoMQTT/A4C138A009B5
N: Received json : {"id":"A4:C1:38:A0:09:B5","name":"LYWSD03MMC","rssi":-60,"distance":1.135236,"servicedata":"30585b051fb509a038c1a408"}
N: Device detected: A4:C1:38:C9:BC:21
N: Subject: /BTtoMQTT/A4C138C9BC21
N: Received json : {"id":"A4:C1:38:C9:BC:21","name":"LYWSD03MMC","rssi":-62,"distance":1.429824,"servicedata":"30585b054021bcc938c1a408"}
N: Device detected: A4:C1:38:50:68:61
N: Subject: /BTtoMQTT/A4C138506861
N: Received json : {"id":"A4:C1:38:50:68:61","name":"LYWSD03MMC","rssi":-62,"distance":1.429824,"servicedata":"30585b054a61685038c1a408"}
N: Found 3 devices, scan number 32 end deinit controller
N: Scan begin
N: Device detected: A4:C1:38:C9:BC:21
N: Subject: /BTtoMQTT/A4C138C9BC21
N: Received json : {"id":"A4:C1:38:C9:BC:21","name":"LYWSD03MMC","rssi":-64,"distance":1.795564,"servicedata":"30585b054021bcc938c1a408"}
N: Device detected: A4:C1:38:50:68:61
N: Subject: /BTtoMQTT/A4C138506861
N: Received json : {"id":"A4:C1:38:50:68:61","name":"LYWSD03MMC","rssi":-62,"distance":1.429824,"servicedata":"30585b054a61685038c1a408"}
N: Device detected: A4:C1:38:A0:09:B5
N: Subject: /BTtoMQTT/A4C138A009B5
N: Received json : {"id":"A4:C1:38:A0:09:B5","name":"LYWSD03MMC","rssi":-58,"distance":0.842869,"servicedata":"30585b051fb509a038c1a408"}
N: Subject: /SYStoMQTT
N: Received json : {"uptime":2162,"version":"version_tag","freemem":146712,"rssi":-35,"SSID":"SNOW","ip":"10.0.0.113","mac":"4C:11:AE:74:F5:84","wifiprt":0,"lowpowermode":0,"interval":55555,"scanbcnct":10,"modules":"BTHADiscovery"}
N: Found 3 devices, scan number 33 end deinit controller

it works, but not very reliable. Sometimes I get temperature, sometimes distance.

Hi,

As this sensor requires a connection to retrieve data, and a BLE connection draw the battery. I do the connect every ScanBeforeConnect, per default 10.
So 9 times on 10 you will see the regular scan results with all the BLE devices nearby the gateway and their distances, lywsd03mmc included.
Maybe we should remove the distance when a temperature sensor is identified?

If you want to retrieve temperature every scan just set ScanBeforeConnect to 1 in config_BT.h (risk of battery draining), you can also change it by MQTT.

Is there a way to remove the other scans ? I only need it to retrieve temperature, I don’t use BLE for presence detection.

Should be possible. I will see how to integrate that.

1 Like

Hi :slight_smile:
Big thanks for your effort!
I’ve compiled “development” with enabled BT, but it seems that “my” OMG doesn’t recognise sensor…

N: Received json : {"uptime":1448,"version":"version_tag","freemem":43480,"rssi":-56,"SSID":"******","ip":"192.168.*.*6","mac":"5C:CF:7F:AA:86:AF","wifiprt":0,"interval":55555,"scanbcnct":10,"modules":"BT"}
N: Subject: /BTtoMQTT/B452A912A68A
N: Received json : {"id":"B4:52:A9:12:A6:8A","rssi":-44,"distance":0.053212,"servicedata":"b452a912a68a071600b0000000000302e0ff020a00"}
N: Subject: /BTtoMQTT/B452A912A68A
N: Received json : {"id":"B4:52:A9:12:A6:8A","rssi":-44,"distance":0.053212,"servicedata":"74"}
N: Subject: /BTtoMQTT/7440BEB4187C
N: Received json : {"id":"74:40:BE:B4:18:7C","rssi":-63,"distance":1.602966,"servicedata":"31702006"}
N: Subject: /BTtoMQTT/7440BEB4187C
N: Received json : {"id":"74:40:BE:B4:18:7C","rssi":-63,"distance":1.602966,"servicedata":"345228374329020100"}
N: Subject: /BTtoMQTT/A4C138917F11
N: Received json : {"id":"A4:C1:38:91:7F:11","rssi":-94,"distance":32.73764,"servicedata":"30585b0501117f9138c1a4280100"}
N: Subject: /BTtoMQTT/A4C138E52359
N: Received json : {"id":"A4:C1:38:E5:23:59","rssi":-42,"distance":0.033417,"servicedata":"30585b05015923e538c1a4280100"}
N: Subject: /BTtoMQTT/A4C138E52359
N: Received json : {"id":"A4:C1:38:E5:23:59","rssi":-43,"distance":0.042283,"servicedata":"30334d4d43"}

done, In test below:

These 2 ones are LYWSD03MMC, the values are retrieved after the scan, so you will see the values after it every 10 scans.
The best way is to check your broker history for these sensors with an MQTT explorer.

I don’t see any updates after this message:
{"id":"A4:C1:38:9B:F1:8E","name":"LYWSD03MMC","rssi":-77,"distance":7.119795}

Even when I check specific in ESP32-1/OpenMQTTGateway_ESP32_BLE/BTtoMQTT/A4C1389BF18E

Seems like the ESP32 isn’t receiving the broadcast messages from my LYWSD03. My temp sensor is paired with my phone, through the Mi app.

What could be causing this issue?

You should disconnect it from your phone.
So as to retrieve the data from the lywsd03mmc the gateway trigger a BLE connection. If your phone is connected, the gateway will not be able to connect and retrieve data.

Downloaded latest development yesterday, set define PublishOnlySensors true, uploaded again, OMG has been running fine.

But I think there is a ‘bug’ in the comment, true and false should be swapped in the comment

#ifndef PublishOnlySensors
#  define PublishOnlySensors true //true if we publish all BLE devices discovered or false only the identified sensors (like temperature sensors)
#endif

And something else I saw, version_tag is not filled in :

image