50ButtonsEach / fliclib-linux-hci

Flic SDK for Linux
306 stars 54 forks source link

Connection to flic buttons is getting lost #90

Closed soldag closed 4 years ago

soldag commented 5 years ago

Recently, I encounter some issues with the flicd server, which I use in my home-assistant setup. After some time, the connection to the buttons is always getting lost. In this state, when clicking any of the paired buttons, they blink red two times. After restarting flicd, everything works well again for some hours. The log always looks like this:

Sep 15 17:15:24 raspberry-pi systemd[1]: Started Flic.
Sep 15 17:15:24 raspberry-pi flicd[5767]: Available HCI devices found:
Sep 15 17:15:24 raspberry-pi flicd[5767]: hci0
Sep 15 17:15:24 raspberry-pi flicd[5767]: Trying hci0
Sep 15 17:15:24 raspberry-pi flicd[5767]: Flic server is now up and running!
Sep 15 17:15:24 raspberry-pi flicd[5767]: Successfully bound HCI socket
Sep 15 17:15:25 raspberry-pi flicd[5767]: Initialization of Bluetooth controller
Sep 15 17:15:36 raspberry-pi flicd[5767]: Accepted new client
Sep 15 17:28:54 raspberry-pi flicd[5767]: Got unknown hci packet type: 3
Sep 15 17:30:03 raspberry-pi flicd[5767]: Got unknown hci packet type: 3
Sep 15 17:52:03 raspberry-pi flicd[5767]: Got unknown hci packet type: 3
Sep 15 18:54:28 raspberry-pi flicd[5767]: Got unknown hci packet type: 3

Sometimes, the server even crashes with the error message Invalid ACL packet size.

Emill commented 5 years ago

Can you run "sudo btmon" in a separate terminal or "sudo btmon > logfile.txt" to capture all hci messages? When the issue occurs, restart flicd and stop the logging and then post the log file.

soldag commented 5 years ago

Thanks for the quick response! Log file is kind of huge as it's hard to determine the exact time the issue occurs (without pressing the button constantly :grimacing:) - sorry for that. It's even too big for github, so I uploaded it here: https://transfernow.net/ddl/flic

Emill commented 5 years ago

Are you running some other program that uses Bluetooth? I see it sends stuff over HCI that isn't related to flicd.

soldag commented 5 years ago

No, flicd is the only process that uses bluetooth. But I noticed that the discovery option of the flic component in home-assistant was enabled. I disabled it and now it seems to be stable again.

I will observe the behavior and reopen the issue, if I should face issues again.

hanwin commented 4 years ago

Can we reopen this issue. I have had this problem for months now. To day I installed a fresh new Raspbian Buster on a Raspberry Pi 3 but the problem still exists.

Emill commented 4 years ago

Hi. I just learned some days ago that Raspberry Pi 3 has quite flaky UART communication to the Bluetooth chip. It doesn't use any flow control and neither any CRC mechanisms to verify that the packet content didn't get corrupt. See https://github.com/raspberrypi/linux/issues/2264.

A solution that works for me is to reduce the baud rate from 921600 kbit/s to 230400 kbit/s. What you should do is to modify the file /usr/bin/btuart and change the following line:

                $HCIATTACH /dev/serial1 bcm43xx 921600 noflow - $BDADDR

to

                $HCIATTACH /dev/serial1 bcm43xx 230400 noflow - $BDADDR

Then reboot. Now check that the correct baud rate (230400) is set by executing ps aux | grep hciattach.

hanwin commented 4 years ago

Thank you. I have modified the file now. I will come back with the result.

hanwin commented 4 years ago

It is stable now and is working perfekt. Here is a one line command you can use: sudo sed -i 's/921600/230400/g' /usr/bin/btuart Lets spread the word!