ThomDietrich/miflora-mqtt-daemon

bluetooth fails to connect after some time

michaelbeljaars opened this issue · 19 comments

I am using your script on a Raspberry Pi 3B+. I have adapted it to include an option to directly pass the data to InfluxDB. All of that works great. I log the sensors ever 15 minutes (900s). However, after certain amount of time (a number of hours typically), the scripts fails to connect to the devices. I have had a similar problem I believe when I used Node-red to connect to the sensors. Everything would work fine, until a few hours later it kept failing to get data. I have the idea that it has something to do with the bluetooth stack, because resetting the adapter generally fixed the problem. Any ideas what could be the cause and how to fix it? Anyone else dealing with a similar issue?

Just got my miflora sensor and I think I have the same issue. After some time it dies. Running sudo hciconfig hci0 down and sudo hciconfig hci0 up everything works again for a while...

Had the same issues with Debian 9 (but not Raspberry) half a year ago. Like @chairman2s said,
hciconfig hci0 down and up was the only solution.

The problem was bluez, had to use the recent version.

http://www.bluez.org/download/ and https://learn.adafruit.com/install-bluez-on-the-raspberry-pi/installation

Maybe this helps.

I am currently compiling and installing the latest version of bluez (5.54). I was running 5.50, not sure if this will make a difference, we'll find out. As a temporary solution, I have added

process = subprocess.Popen(['sudo','hciconfig','hci0','reset'], stdout=subprocess.PIPE)

before the sleep command, so it resets the bluetooth adapter each cycle. That seems to keep the bluetooth connection available. I am using a 15 min interval.

Ok, I tried to install bluez 5.54 but to no avail. Compiling and installing returns no errors, but I am left without a working bluez installation (no bluetoothhd deamon, no hcitool etc). For now I will stick with version 5.50 and use the workaround of reseting the adapter each cycle. I am not even sure that upgrading to 5.54 will fix the problem.

I am currently compiling and installing the latest version of bluez (5.54). I was running 5.50, not sure if this will make a difference, we'll find out. As a temporary solution, I have added

process = subprocess.Popen(['sudo','hciconfig','hci0','reset'], stdout=subprocess.PIPE)

before the sleep command, so it resets the bluetooth adapter each cycle. That seems to keep the bluetooth connection available. I am using a 15 min interval.

Sorry, Im not so into coding. On what line did you but it? Is it at the end on line 402?

    if daemon_enabled:
        print_line('Sleeping ({} seconds) ...'.format(sleep_period))
        process = subprocess.Popen(['sudo','hciconfig','hci0','reset'], stdout=subprocess.PIPE)
        output, error = process.communicate()
        print(error)
        sleep(sleep_period)
        print()
    else:
        print_line('Execution finished in non-daemon-mode', sd_notify=True)
        if reporting_mode == 'mqtt-json':
            mqtt_client.disconnect()
        break

I have exactly the same issue as discussed here. After a while, the connection fails.

My system looks as follows:

  • Mi Flora firmware 3.2.2
  • Raspberry Pi 3 Model B Rev 1.2
  • Raspbian GNU/Linux 9 (stretch)
  • Linux RaspPi 4.19.66-v7+ #1253 SMP Thu Aug 15 11:49:46 BST 2019 armv7l GNU/Linux

I suspect that the problem only occurs since the kernel was updated from 4.14 to 4.19.

All my packages (including the firmware) are up to date.

The workaround with sudo hciconfig hci0 reset is working so far. But it should not be the solution.

Had the same issues with Debian 9 (but not Raspberry) half a year ago. Like @chairman2s said,
hciconfig hci0 down and up was the only solution.

I have noticed that this often works, but unfortunately not always. At some point, there will be a error here too.

The problem was bluez, had to use the recent version.

http://www.bluez.org/download/ and https://learn.adafruit.com/install-bluez-on-the-raspberry-pi/installation

Maybe this helps.

The installation via the link given here does not work with Stretch and my Raspberry Pi 3.

Before the installation I have the following version:
$ bluetoothctl -v
5.43

After the installation it looks the same:
$ bluetoothctl -v
5.43

With this installation guide it worked for me as well.

https://scribles.net/updating-bluez-on-raspberry-pi-from-5-43-to-5-50/

I'm testing the version right now:
bluetoothctl: 5.54

I hope this makes things more stable...

Unfortunately the update to Bluez 5.54 did not help much. The solution for me was a firmware update to the last 4.19 version. See here:
#83 (comment)

I am experiencing this issue since last year. I reduced the Bluetooth speed as I have a Raspberry pi v3 standard I.e. non plus.
I implemented an openhab rule to restart bluetooth and miflora services but once the connection begins to fail, the time between restarts gets shorter and shorter. I noticed that it happens when memory is almost full, due to some openhab memory leak.
By restarting openhab some memory is released and Bluetooth connection to miflora continues.

Hey all,
just a short comment from me: I never experienced issues like these. I'd be happy to include a solution but it sounds like this might be more related to the environment.

Was any of you able to implement a detection for the error? A detection in the daemon would be a good start to get closer to a solution. Feel free to open a PR for it.

I have recently updated to Bluez v5.54 (https://scribles.net/updating-bluez-on-raspberry-pi-from-5-43-to-5-50/) and upgraded RPi firmware to 4.19.118-v7+ (#83 (comment)) and removed the temporary fix of resetting the bluetooth adapter (sudo hciconfig hci0 reset).

I have had no issues for almost a week now, so it seems to have solved the problem.

I have recently updated to Bluez v5.54 (https://scribles.net/updating-bluez-on-raspberry-pi-from-5-43-to-5-50/) and upgraded RPi firmware to 4.19.118-v7+ (#83 (comment)) and removed the temporary fix of resetting the bluetooth adapter (sudo hciconfig hci0 reset).

I did it exactly the same way. And now it' s going better for me than ever before.

I'm glad that worked for you.. unfortunately for me, running the new firmware and bluez 5.5 I still have the issue on a Pi 0. After 7-8 hours I have to reboot the Pi. The hcireset trick didn't work for me either. Not sure what's going on.

Upgrading from stretch to buster appears to have fixed this for my pi 3b. The dist upgrade included new bluez and updated firmware.

I do have the same problem as @Buckeyes1995 on my Rpi 4, after a firmware update I just get a nameless exception:
[2020-10-19 18:59:41] Initial connection to Mi Flora sensor "S1" (C4:7C:8D:...) failed due to exception:
Did anyone else experience this and found a solution? My blueZ Version is 5.50

Experiencing the same problem on raspberry 4, Linux version 5.4.72-v7l+ with bluez 5.54. Until a few days all worked seamlessly.

Lyr3x commented

Experiencing the same issue on my pi 3b. Running a docker container on another linux maschine which is working without any problems, while i need to reboot the pi every day.
Setup:

$ uname -a
Linux hyperion-ng 5.10.9-v7+ #1396 SMP Thu Jan 21 15:54:00 GMT 2021 armv7l GNU/Linux

I installed a Bluetooth dongle, and since then it never happened anymore