basnijholt / miflora

☘️🌑🌼πŸ₯€πŸ‘ Mi Flora Plant sensor Python package
MIT License
366 stars 98 forks source link

Mi Flora Max Support #171

Open DarkByteZero opened 2 years ago

DarkByteZero commented 2 years ago

I tried to get data from the new Mi Flora Max with firmware version 3.6.6. Unfortunately, it did not work. Is this new version supported or have I done something wrong?

LOG: Sat, 06 Aug 2022 15:17:42 ERROR could not read data from C4:7C:8D:6D:59:3F (flora_1) with reason: Traceback (most recent call last): File "/usr/local/lib/python3.9/dist-packages/btlewrap/bluepy.py", line 27, in _func_wrapper return func(*args, **kwargs) File "/usr/local/lib/python3.9/dist-packages/btlewrap/bluepy.py", line 75, in read_handle return self._peripheral.readCharacteristic(handle) File "/usr/local/lib/python3.9/dist-packages/bluepy/btle.py", line 529, in readCharacteristic self._writeCmd("rd %X\n" % handle) File "/usr/local/lib/python3.9/dist-packages/bluepy/btle.py", line 302, in _writeCmd raise BTLEInternalError("Helper not started (did you call connect()?)") bluepy.btle.BTLEInternalError: Helper not started (did you call connect()?)

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/usr/local/lib/python3.9/dist-packages/plantgw/plantgw.py", line 263, in process_all self.process_mac(sensor) File "/usr/local/lib/python3.9/dist-packages/plantgw/plantgw.py", line 241, in process_mac self._publish(sensor_config, poller) File "/usr/local/lib/python3.9/dist-packages/plantgw/plantgw.py", line 213, in _publish MQTTAttributes.BATTERY.value: poller.parameter_value(MI_BATTERY), File "/usr/local/lib/python3.9/dist-packages/miflora/miflora_poller.py", line 149, in parameter_value return self.battery_level() File "/usr/local/lib/python3.9/dist-packages/miflora/miflora_poller.py", line 119, in battery_level self.firmware_version() File "/usr/local/lib/python3.9/dist-packages/miflora/miflora_poller.py", line 128, in firmware_version res = connection.read_handle(_HANDLE_READ_VERSION_BATTERY) # pylint: disable=no-member File "/usr/local/lib/python3.9/dist-packages/btlewrap/bluepy.py", line 33, in _func_wrapper raise BluetoothBackendException() from last_error btlewrap.base.BluetoothBackendException

DarkByteZero commented 2 years ago

Example with demo.py:

pi@plantgateway:~ $ sudo python demo.py --backend bluepy poll XX:XX:XX:XX:XX:XX Getting data from Mi Flora DEBUG:miflora.miflora_poller:Received result for handle 56: 64 53 33 2E 36 2E 36 FW: 3.6.6 Name: Grow care garden DEBUG:miflora.miflora_poller:Filling cache with new sensor data. DEBUG:miflora.miflora_poller:Received result for handle 53: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 Traceback (most recent call last): File "/home/pi/demo.py", line 142, in main() File "/home/pi/demo.py", line 138, in main args.func(args) File "/home/pi/demo.py", line 40, in poll print("Temperature: {}".format(poller.parameter_value(MI_TEMPERATURE))) File "/usr/local/lib/python3.9/dist-packages/miflora/miflora_poller.py", line 168, in parameter_value raise BluetoothBackendException("Could not read data from Mi Flora sensor %s" % self._mac) btlewrap.base.BluetoothBackendException: Could not read data from Mi Flora sensor XX:XX:XX:XX:XX:XX

ThomDietrich commented 2 years ago

Could you clarify which device you are talking about? https://www.google.com/search?q=Mi+Flora+Max

DarkByteZero commented 2 years ago

Xiaomi HHCC Mi Flower Care Plant Sensor Max https://www.techpunt.nl/en/xiaomi-hhcc-mi-flower-care-plant-sensor-max.html?gclid=Cj0KCQjworiXBhDJARIsAMuzAux9hI_KRIh2B7IK5UOW0pzWWzLLExTK75GUVELdgqPfJwyu7irxemIaAmhdEALw_wcB

jvoosten commented 4 months ago

Yup, got the same problem. The larger version uses a different MAC address range, even though it seems to be the same chip (Dialog DA14580). It announces itself as the "Grow care garden" with firmware version 3.6.6 (not 3.3.6!) but fails to respond. The trace below first polls a small sensor, then the big one:

INFO:__main__:Start webserver
INFO:web_server:Received command /status/5C:85:7E:xx:xx:xx
DEBUG:web_server:Fetching status
DEBUG:miflora.miflora_poller:Filling cache with new sensor data.
DEBUG:miflora.miflora_poller:Received result for handle 56: 64 39 33 2E 33 2E 36
DEBUG:miflora.miflora_poller:Received result for handle 53: D2 00 03 FE 00 00 00 1F 01 06 02 3C 00 FB 34 9B
192.168.243.64 - - [22/May/2024 21:09:46] "GET /status/5C:85:7E:xx:xx:xx HTTP/1.1" 200 -
INFO:web_server:firmware version = 3.3.6
DEBUG:miflora.miflora_poller:Using cache (0:00:00.002857 < 0:10:00)
DEBUG:miflora.miflora_poller:Using cache (0:00:00.003224 < 0:10:00)
DEBUG:miflora.miflora_poller:Using cache (0:00:00.003420 < 0:10:00)
DEBUG:miflora.miflora_poller:Using cache (0:00:00.003584 < 0:10:00)

INFO:web_server:Received command /status/C4:7C:8D:yy:yy:yy
DEBUG:web_server:Fetching status
DEBUG:miflora.miflora_poller:Filling cache with new sensor data.
DEBUG:miflora.miflora_poller:Received result for handle 56: 62 53 33 2E 36 2E 36
DEBUG:miflora.miflora_poller:Received result for handle 53: 1A 01 5D 80 8F 04 00 00 00 00 00 00 00 00 00 00
192.168.243.64 - - [22/May/2024 21:11:43] "GET /status/C4:7C:8D:yy:yy:yy HTTP/1.1" 200 -
INFO:web_server:firmware version = 3.6.6
DEBUG:miflora.miflora_poller:Using cache (0:05:00.002456 < 0:10:00)
ERROR:web_server:BT exception raised: ('Could not read data from Mi Flora sensor C4:7C:8D:yy:yy:yy',)
192.168.243.64 - - [22/May/2024 21:11:43] code 500, message Communication error: ('Could not read data from Mi Flora sensor C4:7C:8D:yy:yy:yy',)
192.168.243.64 - - [22/May/2024 21:11:43] "GET /status/C4:7C:8D:yy:yy:yy HTTP/1.1" 500 -

The Android app works of course, so I guess it reacts to different commands. Suggestions on how to debug the Bluetooth traffic?

DarkByteZero commented 4 months ago

@jvoosten You can use my fix, it will work with it. If you want to debug, you could run the program with a debugger and observe the variables. I did this.

jvoosten commented 4 months ago

@jvoosten You can use my fix, it will work with it. If you want to debug, you could run the program with a debugger and observe the variables. I did this.

Yes, the delay works. I made a slightly improved version that checks for the firmware version and adds the delay when necessary.

And what I meant by debugging is sniffing the traffic between the official app and the sensor, not this Python code.