erdose / xiaomi-mi-lywsd03mmc

Xiaomi Mi BLE Temperature & Humidity Sensor
MIT License
26 stars 7 forks source link

Script broken with new firmware #20

Closed LacmanHUN closed 8 months ago

LacmanHUN commented 9 months ago

Hi erdose!

I let the Xiaomi app to upgrad the FW on the little lywsd03mmc gadget to 2.1.1_0159 (as you can see below), since then the script fails at the point where it tries to subscribe for notification. This was previously possible to query with the earlier fw level. Since I'm not quite familiar with the data what the script is looking for, I have no clue which handle to change the script and the subscription info.

What I see in the loginfo.log: 2024-01-28 20:30:59 INFO *** 2024-01-28 20:30:59 INFO Start script... 2024-01-28 20:30:59 INFO Input parameters: Domoticz Server IP: 127.0.0.1 Domoticz Server Port: 8080 Domoticz Server User: * Domoticz Server Password: ***** 2024-01-28 20:31:09 INFO TH_IDX:5 2024-01-28 20:31:13 ERROR Bluetooth command failed (code: 3, error: Attribute can't be written)

What the btle.py can fetch of the gadget:

python btle.py A4:C1:38:60:0B:C1

Connecting to: A4:C1:38:60:0B:C1, address type: public
Service <uuid=Generic Access handleStart=1 handleEnd=7> :
    Characteristic <Device Name>, hnd=0x2, supports READ NOTIFY
    -> b'LYWSD03MMC\x00'
    Characteristic <Appearance>, hnd=0x4, supports READ
    -> b'\x00\x00'
    Characteristic <Peripheral Preferred Connection Parameters>, hnd=0x6, supports READ
    -> b'\x14\x00(\x00\x00\x00\xe8\x03'
Service <uuid=Generic Attribute handleStart=8 handleEnd=11> :
    Characteristic <Service Changed>, hnd=0x9, supports INDICATE
Service <uuid=Device Information handleStart=12 handleEnd=24> :
    Characteristic <Model Number String>, hnd=0xd, supports READ
    -> b'LYWSD03MMC\x00'
    Characteristic <Serial Number String>, hnd=0xf, supports READ
    -> b'GL-F2.0-CFMK-LB-JHDZ'
    Characteristic <Firmware Revision String>, hnd=0x11, supports READ
    -> b'2.1.1_0159\x00'
    Characteristic <Hardware Revision String>, hnd=0x13, supports READ
    -> b'B1.4'
    Characteristic <Software Revision String>, hnd=0x15, supports READ
    -> b'0159\x00'
    Characteristic <Manufacturer Name String>, hnd=0x17, supports READ
    -> b'miaomiaoce.com\x00'
Service <uuid=00010203-0405-0607-0809-0a0b0c0d1912 handleStart=25 handleEnd=29> :
    Characteristic <00010203-0405-0607-0809-0a0b0c0d2b12>, hnd=0x1a, supports READ WRITE NO RESPONSE NOTIFY
    -> b'\x00'
Service <uuid=ebe0ccb0-7a0a-4b0c-8a1a-6ff2997da3a6 handleStart=30 handleEnd=75> :
    Characteristic <ebe0ccb7-7a0a-4b0c-8a1a-6ff2997da3a6>, hnd=0x1f, supports READ WRITE
    -> b'\x01J\xe4\x00'
    Characteristic <ebe0ccb9-7a0a-4b0c-8a1a-6ff2997da3a6>, hnd=0x22, supports READ
    -> b':\x10\x00\x00:\x0e\x00\x00'
    Characteristic <ebe0ccba-7a0a-4b0c-8a1a-6ff2997da3a6>, hnd=0x25, supports READ WRITE
    -> b'\x00\x00\x00\x00'
    Characteristic <ebe0ccbb-7a0a-4b0c-8a1a-6ff2997da3a6>, hnd=0x28, supports READ
    -> b':\x10\x00\x00\xb0=\xe4\x00\xd2\x00<\xd0\x009'
    Characteristic <ebe0ccbc-7a0a-4b0c-8a1a-6ff2997da3a6>, hnd=0x2b, supports NOTIFY
    Characteristic <ebe0ccbe-7a0a-4b0c-8a1a-6ff2997da3a6>, hnd=0x2f, supports READ WRITE
    -> b'\x00'
    Characteristic <ebe0ccc1-7a0a-4b0c-8a1a-6ff2997da3a6>, hnd=0x32, supports READ NOTIFY
    -> b'~\n6\xa2\n'
    Characteristic <ebe0ccc4-7a0a-4b0c-8a1a-6ff2997da3a6>, hnd=0x36, supports READ
    -> b'd'
    Characteristic <ebe0ccc8-7a0a-4b0c-8a1a-6ff2997da3a6>, hnd=0x39, supports WRITE
    Characteristic <ebe0ccd1-7a0a-4b0c-8a1a-6ff2997da3a6>, hnd=0x3c, supports WRITE
    Characteristic <ebe0ccd7-7a0a-4b0c-8a1a-6ff2997da3a6>, hnd=0x3f, supports READ WRITE
    -> b'\x8c\nl\x07U\x14'
    Characteristic <ebe0ccd8-7a0a-4b0c-8a1a-6ff2997da3a6>, hnd=0x42, supports WRITE
    Characteristic <ebe0ccd9-7a0a-4b0c-8a1a-6ff2997da3a6>, hnd=0x45, supports WRITE NOTIFY
    Characteristic <ebe0cff1-7a0a-4b0c-8a1a-6ff2997da3a6>, hnd=0x49, supports READ WRITE
    -> b'\x00'
Service <uuid=fafafa00-fafa-fafa-fafa-fafafafafafa handleStart=76 handleEnd=79> :
    Characteristic <fafafa01-fafa-fafa-fafa-fafafafafafa>, hnd=0x4d, supports READ WRITE
    -> b'\x00\x00\x00\x00'
Service <uuid=8edffff0-3d1b-9c37-4623-ad7265f14076 handleStart=80 handleEnd=94> :
    Characteristic <8edffff1-3d1b-9c37-4623-ad7265f14076>, hnd=0x51, supports READ
    -> b':\x10\x00\x00:\x0e\x00\x00'
    Characteristic <8edffff3-3d1b-9c37-4623-ad7265f14076>, hnd=0x54, supports NOTIFY
    Characteristic <8edffff4-3d1b-9c37-4623-ad7265f14076>, hnd=0x58, supports READ WRITE
    -> b'\x00\x00\x00\x00'
    Characteristic <8edfffef-3d1b-9c37-4623-ad7265f14076>, hnd=0x5b, supports READ NOTIFY
    -> b'~\n6\xa2\n'
Service <uuid=fe95 handleStart=95 handleEnd=118> :
    Characteristic <0004>, hnd=0x60, supports READ
    -> b'2.1.1_0159\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
    Characteristic <0010>, hnd=0x62, supports WRITE NO RESPONSE NOTIFY
    Characteristic <0017>, hnd=0x65, supports WRITE NOTIFY
    Characteristic <0018>, hnd=0x68, supports WRITE NO RESPONSE NOTIFY
    Characteristic <0019>, hnd=0x6b, supports WRITE NO RESPONSE NOTIFY
    Characteristic <001a>, hnd=0x6e, supports WRITE NO RESPONSE NOTIFY
    Characteristic <001b>, hnd=0x71, supports WRITE NO RESPONSE NOTIFY
    Characteristic <001c>, hnd=0x74, supports WRITE NO RESPONSE NOTIFY

Looking for some help solving this.

Thanks in advance! LacmanHUN

LacmanHUN commented 9 months ago

After digging the wide web I found this site: https://false.ekta.is/2021/06/xiaomi-miijia-lywsd03mmc-with-pure-bluetoothctl/ and based on that I figured out where the Temp/Hum/Batt values can be fetched:

[LYWSD03MMC:/service001e/char002f/desc0031]# select-attribute /org/bluez/hci0/dev_A4_C1_38_60_0B_C1/service001e/char0032/desc0034 [LYWSD03MMC:/service001e/char0032/desc0034]# read Attempting to read /org/bluez/hci0/dev_A4_C1_38_60_0B_C1/service001e/char0032/desc0034 [CHG] Attribute /org/bluez/hci0/dev_A4_C1_38_60_0B_C1/service001e/char0032/desc0034 Value: 54 65 6d 70 65 72 61 74 75 72 65 20 61 6e 64 20 Temperature and 48 75 6d 69 64 69 74 79 00 Humidity. 54 65 6d 70 65 72 61 74 75 72 65 20 61 6e 64 20 Temperature and 48 75 6d 69 64 69 74 79 00 Humidity.

And if I set notification to char0032 I get the real values:

[LYWSD03MMC:/service0050/char005b/desc005d]# select-attribute /org/bluez/hci0/dev_A4_C1_38_60_0B_C1/service001e/char0032 [LYWSD03MMC:/service001e/char0032]# notify on [CHG] Attribute /org/bluez/hci0/dev_A4_C1_38_60_0B_C1/service001e/char0032 Notifying: yes Notify started [CHG] Attribute /org/bluez/hci0/dev_A4_C1_38_60_0B_C1/service001e/char0032 Value: 88 06 40 e2 09 ..@..

06-88: 16.72 Celsius, 09-E2: 2.53 V, 40: 64% Humidity.

But still I don't know what to send (handle, value) in your code to get actually these values for processing :

p.writeCharacteristic(0x0038, b'\x01\x00', True) p.writeCharacteristic(0x0046, b'\xf4\x01\x00', True)

Thanks, LacmanHUN

LacmanHUN commented 8 months ago

Hi erdose,

Eventually this change worked for me in the long run p.writeCharacteristic(0x0035, b'\x01\x00', True) #0x0038

p.writeCharacteristic(0x0046, b'\xf4\x01\x00', True) #0x0046

And the script ran perfectly.

BR, Laszlo