Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bluetooth fails to connect after some time #120

Open
michaelbeljaars opened this issue May 22, 2020 · 19 comments
Open

bluetooth fails to connect after some time #120

michaelbeljaars opened this issue May 22, 2020 · 19 comments

Comments

@michaelbeljaars
Copy link

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?

@chairman2s
Copy link

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...

@onebavarian
Copy link

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.

@michaelbeljaars
Copy link
Author

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.

@michaelbeljaars
Copy link
Author

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.

@chairman2s
Copy link

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?

@michaelbeljaars
Copy link
Author

    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

@Markkuuss
Copy link
Contributor

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.

@Markkuuss
Copy link
Contributor

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...

@Markkuuss
Copy link
Contributor

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)

@lionhe1966
Copy link

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.

@ThomDietrich
Copy link
Owner

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.

@michaelbeljaars
Copy link
Author

michaelbeljaars commented Jun 24, 2020

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.

@Markkuuss
Copy link
Contributor

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.

@Buckeyes1995
Copy link

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.

@DukeyToo
Copy link

DukeyToo commented Sep 18, 2020

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

@moTo31
Copy link

moTo31 commented Oct 19, 2020

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

@Riseryn
Copy link

Riseryn commented Dec 11, 2020

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
Copy link

Lyr3x commented Jan 23, 2021

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

@lionhe1966
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests