chvolkmann / govee_btled

A Python wrapper for controlling a cheap Bluetooth RGB light bulb.
91 stars 14 forks source link

random connection timeouts using the library #4

Open jr3us opened 3 years ago

jr3us commented 3 years ago

Hi, First, thank you for creating this library for accessing the GOVEE BT LED bulbs! I am in process of writing a wrapper on a Raspberry pi 4 for this bulb that sends mqtt messages for use in Home assistant (home-assistant.io).

I do have all of the functionality working from within Home Assistant with regards to controlling the GOVEE bulb.

However, randomly, after 10 or 20 commands sent to the bulb, I get errors connecting. as follows:

raise ConnectionTimeout(self.mac, err) govee_btled.errors.ConnectionTimeout: Failed connecting to XX:XX:XX:XX:XX:XX

Any ideas come to mind to fix this? For each call from Home assistant, the wrapper initializes the bulb, and then deletes the connection.

I have tried both the gattinfo as well as the BGAPI version in the pygatt library.

I also tried the following timeout change, with no change.

echo 1000 > /sys/kernel/debug/bluetooth/hci0/supervision_timeout

Thanks in advance!

chvolkmann commented 3 years ago

Hmm, I'm really not too sure, looks like it might come from the underlying bluetooth library. I'm thinking of rebuilding this library in Node JS, maybe it works in that cosmos.

In any case, if you find a solution, I'll be happy to merge a pull request :)

benobytes commented 3 years ago

Hi @chvolkmann, I found your library very useful as I wanted to recreate this library in NodeJS. In fact I did using the Noble library, at https://www.npmjs.com/package/govee-led-client I managed to get all the features to work, however I am also experiences issues with random connection timeouts. Feel free to check it out as I am still a very much beginner at creating libraries.

jr3us commented 3 years ago

Hi @chvolkmann , I have stumbled across some information that I think might cure the issue of timeouts. On the following github page https://github.com/egold555/Govee-H6113-Reverse-Engineering is a mention of the AA data packets as being used for keep alive every 2 seconds. I'm thinking maybe these need to be sent to to keep the connection alive, and not let it time out. How would I mod your _send function? I see the self._dev.char_write function. can that just be called with the frame as defined in the github page above? I'm thinking if so, I might add a function in the object that would do the transmit, tho as structured, it would have to be sent by the python program calling your library manually I think. I'm not sure how it could be automated.

jr3us commented 3 years ago

I have modified the mqtt interface to the govee bulb using the 2 second keep alive( I made it 1.5 seconds) and I have had no failures with connectivity in the past few hours. with regards to failing to connect. Looks like the ping is working to keep the connection alive!

edit: I have created a repository ( https://github.com/mqttblebulb/mqttblebulb ) that incorporates the ping as I have added it in the pr, and it is working well with no timeouts so far. the code now sends a keep alive ever 1.5 seconds to the bulb.

mqttblebulb commented 3 years ago

I asked a question on stackexchange and I received a response that basically goes direct to the bluez interface, and works quite well. The python objects are basically a drop in replacement for the pygatt library you were using. The only change required in bluetooth_led.py is to modify the following line:

def init(self, mac, bt_backend_cls=PyDbusBackend):

and to import the new PyDbusBackend as follows:

everything else in bluetooth_led.py stays the same.

from .bluezdbus import PyDbusBackend, PyDbusDevice

Do you want a pull request for the changes? This change also lets the module work on multiple bulbs simultaneously.

The keep alive modification that i added previously is still working fine.