miflora-mqtt-daemon icon indicating copy to clipboard operation
miflora-mqtt-daemon copied to clipboard

miflora 0.6 library not working on jessie after moving to bluepy

Open itayraviv opened this issue 4 years ago β€’ 26 comments


Downloading/unpacking miflora==0.6 (from -r requirements.txt (line 2))
  Downloading miflora-0.6.tar.gz
  Running setup.py (path:/tmp/pip-build-fnzks7mm/miflora/setup.py) egg_info for package miflora
    Traceback (most recent call last):
      File "<string>", line 17, in <module>
      File "/tmp/pip-build-fnzks7mm/miflora/setup.py", line 15, in <module>
        long_description=readme(),
      File "/tmp/pip-build-fnzks7mm/miflora/setup.py", line 8, in readme
        return readme_file.read()
      File "/usr/lib/python3.4/encodings/ascii.py", line 26, in decode
        return codecs.ascii_decode(input, self.errors)[0]
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 4694: ordinal not in range(128)
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):

  File "<string>", line 17, in <module>

  File "/tmp/pip-build-fnzks7mm/miflora/setup.py", line 15, in <module>

    long_description=readme(),

  File "/tmp/pip-build-fnzks7mm/miflora/setup.py", line 8, in readme

    return readme_file.read()

  File "/usr/lib/python3.4/encodings/ascii.py", line 26, in decode

    return codecs.ascii_decode(input, self.errors)[0]

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 4694: ordinal not in range(128)

----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /tmp/pip-build-fnzks7mm/miflora
Storing debug log for failure in /root/.pip/pip.log

prior moving to bluepy, I also had issues with miflora=0.6, so i changed it to 0.4 and all worked great until this change.. now 0.4 and 0.6 not working. I can manually go over the files changes but i would prefer sticking with the master build. p.s - I'm working with Homeassistant MQTT if it matters.

itayraviv avatar Apr 01 '20 08:04 itayraviv

Hello @itayraviv,

UnicodeDecodeError: 'ascii' codec can't decode

this is almost always a sign for an older python or pip version. Please ensure that you are on an up to date version state. This is on my testing Raspberry Pi:

[17:23:58] openhabian@openHABianPi:~$ python3 --version
Python 3.5.3
[17:24:10] openhabian@openHABianPi:~$ pip3 --version
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.5)

Note to self: Document minimum version requirements.

ThomDietrich avatar Apr 01 '20 17:04 ThomDietrich

i just run: sudo apt-get update sudo apt-get upgrade -y sudo apt remove git python3 python3-pip bluetooth bluez than reinstall with: sudo apt install git python3 python3-pip bluetooth bluez and I get this: python3 --version --> Python 3.4.2 pip3 --version --> pip 1.5.6 from /usr/lib/python3/dist-packages (python 3.4)

any idea why I don't get the latest versions or how i can force it to download newer builds?

itayraviv avatar Apr 01 '20 21:04 itayraviv

looks like python 3.5 is required:

when running: sudo pip3 install -r requirements.txt I get: Traceback (most recent call last): File "/usr/local/bin/pip3", line 7, in from pip._internal.cli.main import main File "/usr/local/lib/python3.4/dist-packages/pip/_internal/cli/main.py", line 10, in from pip._internal.cli.autocompletion import autocomplete File "/usr/local/lib/python3.4/dist-packages/pip/_internal/cli/autocompletion.py", line 9, in from pip._internal.cli.main_parser import create_main_parser File "/usr/local/lib/python3.4/dist-packages/pip/_internal/cli/main_parser.py", line 7, in from pip._internal.cli import cmdoptions File "/usr/local/lib/python3.4/dist-packages/pip/_internal/cli/cmdoptions.py", line 28, in from pip._internal.models.target_python import TargetPython File "/usr/local/lib/python3.4/dist-packages/pip/_internal/models/target_python.py", line 4, in from pip._internal.utils.misc import normalize_version_info File "/usr/local/lib/python3.4/dist-packages/pip/_internal/utils/misc.py", line 20, in from pip._vendor import pkg_resources File "/usr/local/lib/python3.4/dist-packages/pip/_vendor/pkg_resources/init.py", line 92, in raise RuntimeError("Python 3.5 or later is required") RuntimeError: Python 3.5 or later is required

itayraviv avatar Apr 01 '20 21:04 itayraviv

manual download and compile?? found these instructions: https://medium.com/@isma3il/install-python-3-6-or-3-7-and-pip-on-raspberry-pi-85e657aadb1e

itayraviv avatar Apr 01 '20 21:04 itayraviv

May I ask why you are on Jessie? It might be the easiest to just switch over to the latest openHABian / Raspbian build :)

Otherwise, yes that would be the way to go πŸ‘

ThomDietrich avatar Apr 01 '20 21:04 ThomDietrich

https://packages.debian.org/search?keywords=python3

ThomDietrich avatar Apr 01 '20 21:04 ThomDietrich

running jessie because this Rpi SD flashed with Volumio image which is jessie. volumio@volumio:/opt/miflora-mqtt-daemon$ cat /etc/issue Raspbian GNU/Linux 8 \n \l Everything worked fine with miflora=0.4, when you moved to 0.6 it gave an error so i just changed back to 0.4, but now with the bluepy its not working at all..

itayraviv avatar Apr 01 '20 22:04 itayraviv

Welcome to the future :) What should I say. Software is constantly improved and that often means you need to follow along. If you don't want that you can of course just not use the latest version of miflora-mqtt-daemon - which would be bad as we just recently improved Home Assistant discovery.

My recommendation is to either upgrade your distribution (dist-upgrade see https://www.raspberrypi.org/documentation/raspbian/updating.md) or your python

ThomDietrich avatar Apr 02 '20 07:04 ThomDietrich

I installed python 3.6.5 and pip3, now those are the versions: python: volumio@volumio:/opt/miflora-mqtt-daemon$ python3 --version Python 3.6.5

pip3: volumio@volumio:/opt/miflora-mqtt-daemon$ sudo pip3 -V pip 20.0.2 from /usr/local/lib/python3.6/site-packages/pip (python 3.6)

and this is the result:

volumio@volumio:/opt/miflora-mqtt-daemon$ cd /opt/miflora-mqtt-daemon volumio@volumio:/opt/miflora-mqtt-daemon$ sudo pip3 install -r requirements.txt Collecting miflora==0.6 Using cached miflora-0.6.tar.gz (14 kB) ERROR: Command errored out with exit status 1: command: /usr/local/bin/python3.6 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-bej93xxo/miflora/setup.py'"'"'; file='"'"'/tmp/pip-install-bej93xxo/miflora/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-install-bej93xxo/miflora/pip-egg-info cwd: /tmp/pip-install-bej93xxo/miflora/ Complete output (9 lines): Traceback (most recent call last): File "", line 1, in File "/tmp/pip-install-bej93xxo/miflora/setup.py", line 15, in long_description=readme(), File "/tmp/pip-install-bej93xxo/miflora/setup.py", line 8, in readme return readme_file.read() File "/usr/local/lib/python3.6/encodings/ascii.py", line 26, in decode return codecs.ascii_decode(input, self.errors)[0] UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 4694: ordinal not in range(128) ---------------------------------------- ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

how do I install an earlier version of miflora-mqtt-deamon?

itayraviv avatar Apr 02 '20 10:04 itayraviv

Did you try this?

https://github.com/mroosmalen/nanosv/issues/45#issuecomment-400640884

https://stackoverflow.com/a/50495200/6654920

https://stackoverflow.com/a/45555936/6654920

ThomDietrich avatar Apr 02 '20 10:04 ThomDietrich

OK, getting some progress i think.. i run those:

echo 'export LANG=en_US.UTF-8' >> ~/.bashrc echo 'export LANGUAGE=en_US:en' >> ~/.bashrc echo 'export LC_ALL=en_US.UTF-8' >> ~/.bashrc sudo apt-get install language-pack-en

and installed the requirements.txt again, gained some progress until at the end i got this:

Successfully built miflora bluepy btlewrap paho-mqtt sdnotify ERROR: miflora 0.6 has requirement btlewrap==0.0.8, but you'll have btlewrap 0.0.9 which is incompatible. Installing collected packages: typing, btlewrap, miflora, bluepy, paho-mqtt, wheel, sdnotify, colorama, Unidecode Attempting uninstall: wheel Found existing installation: wheel 0.34.2 Uninstalling wheel-0.34.2: Successfully uninstalled wheel-0.34.2 Successfully installed Unidecode-0.4.21 bluepy-1.3.0 btlewrap-0.0.9 colorama-0.3.9 miflora-0.6 paho-mqtt-1.4.0 sdnotify-0.3.1 typing-3.7.4.1 wheel-0.29.0 volumio@volumio:/opt/miflora-mqtt-daemon$

any ideas for this?

itayraviv avatar Apr 02 '20 10:04 itayraviv

That might be a small bug on my side. Could you just remove btlewrap from requirements.txt? Should work after.

ThomDietrich avatar Apr 02 '20 11:04 ThomDietrich

ok, finished with no errors... trying to run i get these (no info what the exception is):

volumio@volumio:/opt/miflora-mqtt-daemon$ python3 /opt/miflora-mqtt-daemon/miflora-mqtt-daemon.py

Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon Source: https://github.com/ThomDietrich/miflora-mqtt-daemon

[2020-04-02 11:12:01] Connecting to MQTT broker ... [2020-04-02 11:12:01] MQTT connection established

Adding sensor to device list and testing connection ... Name: "Lemon" [2020-04-02 11:12:02] Initial connection to Mi Flora sensor "Lemon" (C4:7C:8D:64:98:F2) failed due to exception:

Adding sensor to device list and testing connection ... Name: "Pomelo" [2020-04-02 11:12:03] Initial connection to Mi Flora sensor "Pomelo" (C4:7C:8D:64:9B:CF) failed due to exception:

itayraviv avatar Apr 02 '20 11:04 itayraviv

maybe a different approach will work, what if I'll run miflora-mqtt-deamon as a docker? is it possible? wouldn't be isolating all the OS dependencies?

itayraviv avatar Apr 02 '20 11:04 itayraviv

It's weird that no exception is printed. Anyhow, the most likely cause is that bluetooth is not set up properly. Did the manual scanning work? https://github.com/ThomDietrich/miflora-mqtt-daemon#configuration

Docker: Now that you are almost there I wouldn't switch. It's probably linked to bluetooth, which would also be an issue in docker. Let's figure this out and hopefully we can lean something ;)

ThomDietrich avatar Apr 02 '20 12:04 ThomDietrich

To see the actual exception, could you please change miflora-mqtt-daemon.py in line 207 (remove the try.. except ...):

    flora_poller.fill_cache()
    flora_poller.parameter_value(MI_LIGHT)
    flora['firmware'] = flora_poller.firmware_version()

    print('Internal name: "{}"'.format(name_clean))
    print('Device name:   "{}"'.format(flora_poller.name()))
    print('MAC address:   {}'.format(flora_poller._mac))
    print('Firmware:      {}'.format(flora_poller.firmware_version()))
    print_line('Initial connection to Mi Flora sensor "{}" ({}) successful'.format(name_pretty, mac), sd_notify=True)
    if int(flora_poller.firmware_version().replace(".", "")) < 319:
        print_line('Mi Flora sensor with a firmware version before 3.1.9 is not supported. Please update now.'.format(name_pretty, mac), error=True, sd_notify=True)

ThomDietrich avatar Apr 02 '20 12:04 ThomDietrich

yes, the scan shows devices, I do have hci0 (on board) and hci1 (usb dongle), tried both in the config.ini with same result.. As i said, all the changes broke the volumio, so I went back to factory defaults, trying to do it again step by step while after each step i check its still working.. hoping to reach that point again, then I will do what you suggested here and update..

itayraviv avatar Apr 02 '20 12:04 itayraviv

Similar issue here, I'm available to help debug.

Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon
Source: https://github.com/ThomDietrich/miflora-mqtt-daemon

[2020-04-02 15:08:21] Connecting to MQTT broker ...
[2020-04-02 15:08:21] MQTT connection established

Adding sensor to device list and testing connection ...
Name:          "FiddleLeafFigKitchen"
[2020-04-02 15:08:36] Initial connection to Mi Flora sensor "FiddleLeafFigKitchen" (C4:7C:8D:64:08:04) failed due to exception:

Adding sensor to device list and testing connection ...
Name:          "FiddleLeafFigOfficeRight"
[2020-04-02 15:08:53] Initial connection to Mi Flora sensor "FiddleLeafFigOfficeRight" (C4:7C:8D:64:11:AE) failed due to exception:

Adding sensor to device list and testing connection ...
Name:          "PeaceLillyEntry"
[2020-04-02 15:09:15] Initial connection to Mi Flora sensor "PeaceLillyEntry" (C4:7C:8D:64:10:85) failed due to exception:

Adding sensor to device list and testing connection ...
Name:          "FiddleLeafFigOfficeLeft"
[2020-04-02 15:09:25] Initial connection to Mi Flora sensor "FiddleLeafFigOfficeLeft" (C4:7C:8D:64:10:C7) failed due to exception:

Adding sensor to device list and testing connection ...
Name:          "HopePhilodendronEntry"
[2020-04-02 15:09:36] Initial connection to Mi Flora sensor "HopePhilodendronEntry" (C4:7C:8D:64:10:82) failed due to exception:

[2020-04-02 15:09:36] Announcing Mi Flora devices to MQTT broker for auto-discovery ...
Traceback (most recent call last):
  File "/home/pi/miflora-mqtt-daemon/miflora-mqtt-daemon.py", line 330, in <module>
    'sw_version': flora['firmware']
KeyError: 'firmware'

bpartridge83 avatar Apr 02 '20 15:04 bpartridge83

Removing that try/catch that you recommended provided the following output:

[2020-04-02 15:24:44] Connecting to MQTT broker ...
[2020-04-02 15:24:44] MQTT connection established

Adding sensor to device list and testing connection ...
Name:          "FiddleLeafFigKitchen"
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/btlewrap/bluepy.py", line 27, in _func_wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/btlewrap/bluepy.py", line 56, in connect
    self._peripheral = Peripheral(mac, iface=iface, addrType=self.address_type)
  File "/usr/local/lib/python3.5/dist-packages/bluepy/btle.py", line 391, in __init__
    self._connect(deviceAddr, addrType, iface)
  File "/usr/local/lib/python3.5/dist-packages/bluepy/btle.py", line 439, in _connect
    raise BTLEDisconnectError("Failed to connect to peripheral %s, addr type: %s" % (addr, addrType), rsp)
bluepy.btle.BTLEDisconnectError: Failed to connect to peripheral C4:7C:8D:64:08:04, addr type: public

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

Traceback (most recent call last):
  File "/home/pi/miflora-mqtt-daemon/miflora-mqtt-daemon.py", line 240, in <module>
    flora_poller.fill_cache()
  File "/usr/local/lib/python3.5/dist-packages/miflora/miflora_poller.py", line 85, in fill_cache
    firmware_version = self.firmware_version()
  File "/usr/local/lib/python3.5/dist-packages/miflora/miflora_poller.py", line 127, in firmware_version
    with self._bt_interface.connect(self._mac) as connection:
  File "/usr/local/lib/python3.5/dist-packages/btlewrap/base.py", line 47, in __enter__
    self._backend.connect(self._mac)
  File "/usr/local/lib/python3.5/dist-packages/btlewrap/bluepy.py", line 33, in _func_wrapper
    raise BluetoothBackendException() from last_error
btlewrap.base.BluetoothBackendException

I also updated the config.ini to attempt other BLE devices, and they all caused the same error.

bpartridge83 avatar Apr 02 '20 15:04 bpartridge83

Running sudo hcitool lescan, I am able to see the devices configured for this RPi.

For some context, I have three other RPis running miflora-mqtt-daemon, and they aren't throwing the exception. Those other three have not yet been updated to the latest version of this project since last year. (from git log, last change "Fri Feb 15 11:17:30 2019")

I haven't updated my Mi Flora sensors to 3.2.1; they're all running 3.1.9 except for one newly installed device. (I saw "Tested with Mi Flora firmware 3.2.1" in the README.)

bpartridge83 avatar Apr 02 '20 15:04 bpartridge83

Checking out a0b85a64334c5a5bcbfe419210f31728364794a0 and restarting the RPi seems to be working.

Checking out 766227d845e5860fdb54a744fdab5002ad08459f (upgrade to btlewrap-0.0.9), still works.

5ceae319c408bd7b8e162c53b09b7b58d8e238a6, all good.

master, and now it's working.

Β―\_(ツ)_/Β―

bpartridge83 avatar Apr 02 '20 16:04 bpartridge83

and now it's working.

Yay non-deterministic behavior -.-

The only thing meaningful I read is that you restarted. Any other changes to the system or user executing the daemon?

ThomDietrich avatar Apr 02 '20 17:04 ThomDietrich

OK, got no idea what I was doing differently but its working now... with the error on btlewrap version. for the ASCI error, what helped me is to add the following lines to ~/.bashrc: export LC_CTYPE=en_US.UTF-8 export LANG=en_US.UTF-8

image

I did manually installed python 3.5.3 and some other pip packages but i can't tell if it was indeed necessary... i'm thinking of restoring again (clean install) and just add the lines above to ~/.bashrc to see if that was the only issue (got the feeling that the answer is no for that). but anyway i'll do it as i by mistake installed the Python 3.5.3 inside miflora-mqtt-deamon dir

itayraviv avatar Apr 02 '20 18:04 itayraviv

export LC_CTYPE=en_US.UTF-8 export LANG=en_US.UTF-8

Honestly you should consider to raise this issue in the volumio issue tracker. I don't know which environment settings they use (probably your local locale) but UTF8 is the better default nowadays.

Happy that you got it working. Is there anything we can take for the project from this issue? What I remember:

  • Document the supported python version
  • Sort out the btlewrap version mismatch (delete from requirements)

ThomDietrich avatar Apr 02 '20 18:04 ThomDietrich

OK, thats it.. I restored again.. added the following to ~/.bashrc:

export LANGUAGE=en_US:en export LC_ALL=en_US.UTF-8 export LC_CTYPE=en_US.UTF-8 export LANG=en_US.UTF-8

next step - running all commands according to the git instructions, and got this error: image

so i run: sudo pip3 install typing

after that the "sudo pip3 install -r requirements.txt" passed successfully. thanks for the help.

itayraviv avatar Apr 02 '20 19:04 itayraviv

python 3.4?

ThomDietrich avatar Apr 03 '20 08:04 ThomDietrich