Xiaomi Mijia LYWSD03MMC ESP32 support

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

Oh yes :confused:well seen. Thanks !

This tag is set when we publish a release when you take the code from the dev branch is is always “version_tag”

I disconnected the sensor now from the MiHome app, but I still only get only these messages:

{ "id" : "A4:C1:38:9B:F1:8E", "rssi" : -70, "distance" : 3.472448, "servicedata" : "30585b05828ef19b38c1a408" }

Do I need to reset the sensor somehow?

Did you wait at least for 10 minutes and looked at your message history?

One more problem : the HA discovery of voltage does not work, it throws errors :
Reason : there is no sensor device class ‘voltage’, so removing ‘device_class’: ‘voltage’ from the message should fix this problem.

Exception in async_discover_sensor when dispatching 'mqtt_discovery_new_sensor_mqtt': ({'state_topic': 'home/OpenMQTTGateway_ESP32_BLE/BTtoMQTT/A4C138C9BC21', 'name': 'LYWSD03MMC-volt', 'unique_id': 'A4C138C9BC21-LYWSD03MMC-volt', 'device_class': 'voltage', 'value_template': '{{ value_json.volt | is_defined }}', 'unit_of_measurement': 'V', 'platform': 'mqtt'},) Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/mqtt/sensor.py", line 79, in async_discover_sensor config = PLATFORM_SCHEMA(discovery_payload) File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 272, in __call__ return self._compiled([], data) File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict return base_validate(path, iteritems(data), out) File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 432, in validate_mapping raise er.MultipleInvalid(errors) voluptuous.error.MultipleInvalid: value is not allowed for dictionary value @ data['device_class']
Exception in async_discover_sensor when dispatching 'mqtt_discovery_new_sensor_mqtt': ({'state_topic': 'home/OpenMQTTGateway_ESP32_BLE/BTtoMQTT/A4C138A009B5', 'name': 'LYWSD03MMC-volt', 'unique_id': 'A4C138A009B5-LYWSD03MMC-volt', 'device_class': 'voltage', 'value_template': '{{ value_json.volt | is_defined }}', 'unit_of_measurement': 'V', 'platform': 'mqtt'},) Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/mqtt/sensor.py", line 79, in async_discover_sensor config = PLATFORM_SCHEMA(discovery_payload) File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 272, in __call__ return self._compiled([], data) File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict return base_validate(path, iteritems(data), out) File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 432, in validate_mapping raise er.MultipleInvalid(errors) voluptuous.error.MultipleInvalid: value is not allowed for dictionary value @ data['device_class']
Exception in async_discover_sensor when dispatching 'mqtt_discovery_new_sensor_mqtt': ({'state_topic': 'home/OpenMQTTGateway_ESP32_BLE/BTtoMQTT/A4C138506861', 'name': 'LYWSD03MMC-volt', 'unique_id': 'A4C138506861-LYWSD03MMC-volt', 'device_class': 'voltage', 'value_template': '{{ value_json.volt | is_defined }}', 'unit_of_measurement': 'V', 'platform': 'mqtt'},) Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/mqtt/sensor.py", line 79, in async_discover_sensor config = PLATFORM_SCHEMA(discovery_payload) File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 272, in __call__ return self._compiled([], data) File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict return base_validate(path, iteritems(data), out) File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 432, in validate_mapping raise er.MultipleInvalid(errors) voluptuous.error.MultipleInvalid: value is not allowed for dictionary value @ data['device_class']

Yes, in the meantime I switched to a Micropython script on my ESP32 and that’s working directly “out of the box”. Doesn’t matter if my sensors are paired with my phone or not. Everything is inmediately read.

What version of OMG are you using?

Thanks,

Corrected here

OpenMQTTGateway v0.9.4

I followed the documentation step by step.

  1. I’m using an ESP32-WROOM-32 board.
  2. I uploaded all the binaries (boot-app, bootloader-80, BLE_firmware & BLE_partitions)
  3. I Configured everything (WIFI & MQTT)

But somehow my sensors won’t retrieve any useful information, like the Micropython Script is doing.

I would love to get it working with OpenMQTTGateway, because I want to retrieve information from not only my LYWSD03MMC temp sensors, but also MiFlora.

I think we have the answer :slight_smile:
V0.9.4 is not compatible with LYWSD03MMC, only the development branch is.
So there is no binaries available yet, I’m planning to release v0.9.5 in September for info. v0.9.5 will be compatible.

In the meantime you may build the binaries yourself from the development branch.

1 Like

Thanks for clarifying! I have no idea how to compile the binaries myself, so I’ll patiently wait until v0.9.5 will be released. It won’t take that long until september arrives. :wink:

1 Like