rhyst / linak-controller

A Python script to control Linak standing desks.
MIT License
350 stars 51 forks source link

Desk not found by idasen-controller (but paired with bluetooth) on Raspberry Pi #15

Closed tomsturges closed 3 years ago

tomsturges commented 3 years ago

Hi again!

So my Raspberry Pi is paired to my desk but when I type idasen-controller I get the error Could not find desk e0:83:79:xx:xx:xx (I added it to the config.yaml). I confirmed that I am paired by typing bluetoothctl at the command line to open the Bluetooth Controller and then typed info e0:83:79:xx:xx:xx which tells me:

Device E0:83:79:xx:xx:xx (random)
        Name: Standing Desk
        Alias: Standing Desk
        Paired: yes
        Trusted: yes
        Blocked: no
        Connected: yes
        LegacyPairing: no
        UUID: Generic Access Profile    (xxxxxxxxxxxxxx)
        UUID: Generic Attribute Profile (xxxxxxxxxxxxxx)
        UUID: Vendor specific           (xxxxxxxxxxxxxx)
        UUID: Vendor specific           (xxxxxxxxxxxxxx)
        UUID: Vendor specific           (xxxxxxxxxxxxxx)
        UUID: Vendor specific           (xxxxxxxxxxxxxx)

(I scrambled some info above with 'xxx'; I'm not sure what is safe to include online)

rhyst commented 3 years ago

Raspberry Pis are weird, mine won't let me do bluetooth things as a non-root user so maybe that has something to do with it. If you install idasen controller as root:

sudo pip3 install idasen-controller

And then run the script as root:

sudo idasen-controller

Does that change anything?

tomsturges commented 3 years ago

Thanks, but no. I did as you suggested (and re-connected in bluetoothctl as it had disconnected overnight) but it still cannot find the desk.

rhyst commented 3 years ago

Ah so it seems that the mac_address must be uppercase. I have made the script uppercase it when reading from the config. Please update and let me know if that starts working (I think there is no need for sudo as your bluetoothctl works without it):

 pip3 install --upgrade idasen-controller
tomsturges commented 3 years ago

I notice that update capitalising the MAC address now, but unfortunately I still have the same error (I checked with sudo too).

rhyst commented 3 years ago

Is the mac address capitalised in the error?

(This now works on my RPi 3B running Raspbian)

tomsturges commented 3 years ago

Yes it is. I am on a RPi 4, I think also Raspbian (It is "Raspberry Pi OS with desktop and recommended software"). I will double check I really have the correct MAC address, maybe I made a mistake, or it has changed, I dunno.

rhyst commented 3 years ago

Does running idasen-controller --scan return anything?

rhyst commented 3 years ago

Also what python version are you running on the pi?

tomsturges commented 3 years ago
pi@raspberrypi:~ $ python --version
Python 2.7.16
pi@raspberrypi:~ $ python3 --version
Python 3.7.3
tomsturges commented 3 years ago

idasen-controller --scan returns a list of 10 devices, but none of them are the MAC address that bluetoothctl tells me I am connected to (and has the Alias "Standing Desk")

rhyst commented 3 years ago

Very confusing! Whats the output of hcitool dev? Wondering if you somehow have two bluetooth adapters or something.

tomsturges commented 3 years ago
pi@raspberrypi:~ $ hcitool dev
Devices:
        hci0    DC:A6:32:D3:FE:2D
tomsturges commented 3 years ago

Btw I could not connect to my desk with the Linak android app, so I disconnected my RPi using remove inside bluetoothctl and now I can connect again. So I'm pretty sure that's the correct MAC address.

tomsturges commented 3 years ago

Ahah! Now that I disconnected via bluetoothctl, the desk is now found with idasen-controller --scan. However I'm still having trouble connecting. I'll play a little bit and report back.

rhyst commented 3 years ago

I've got everything basically the same as you:

➜  ~ python3 --version
Python 3.7.3
[bluetooth]# info E8:5B:5B:24:22:E4
Device E8:5B:5B:24:22:E4 (random)
    Name: Desk 8932
    Alias: Desk 8932
    Paired: yes
    Trusted: no
    Blocked: no
    Connected: no
    LegacyPairing: no
    UUID: Generic Access Profile    (00001800-0000-1000-8000-00805f9b34fb)
    UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
    UUID: Vendor specific           (99fa0001-338a-1024-8a49-009c0215f78a)
    UUID: Vendor specific           (99fa0010-338a-1024-8a49-009c0215f78a)
    UUID: Vendor specific           (99fa0020-338a-1024-8a49-009c0215f78a)
    UUID: Vendor specific           (99fa0030-338a-1024-8a49-009c0215f78a)
➜  ~ hcitool dev
Devices:
    hci0    B8:27:EB:77:8C:03
➜  ~ idasen-controller --scan
Found 5 devices using hci0
E8:5B:5B:24:22:E4: Desk 8932
...

Except mine connects:

➜  ~ idasen-controller       
Connected E8:5B:5B:24:22:E4
Height:  688mm
Disconnected   

You could try adjusting the scan/connect timeouts and see if that helps? (I have noticed another bug that setting scan timeout from command line will not work so do that one in the config file).

tomsturges commented 3 years ago

Ok thanks I'll report back in a couple hours, need to head out.

tomsturges commented 3 years ago

Ok it works now! The (or one of the) problems was this...

I paired in bluetoothctl using pair <MAC ADDRESS>. It seems this would pair me, but also connect me, to the desk. So then idasen-controller was unable to connect. So to fix I used disconnect <MAC ADDRESS> in bluetoothctl so that I am paired but not connected.

Perhaps you could update the README as it says "the desk should be connected and paired to the computer", whereas (at least with the RPi) it looks like it needs to be paired but NOT connected. Just a suggestion.

Thanks a lot for you help!

rhyst commented 3 years ago

Glad its working. Interestingly on my pi it does not seem to matter if its connected in bluetoothctl or not but I will add it as a troubleshooting suggestion as you suggest.

magicmatt007 commented 2 years ago

Thanks for this Python code. It's the first one, which allowed me to control my desk.

I'd like to integrate this now into Homeassistant. However, before I start with this adventure, I'd need to resolve the following:

I only was able to use your code after I paired the desk in my Raspian on a PI3B. WIhtout pairing, I don't get it working at all. However, I don't now, how I could automate pairing in Python......

Any idea, how I could get your code work without prior pairing? Or alternatively, do you have a proposal, how pairing could be done with Python code?

Many thanks!

rhyst commented 2 years ago

A couple of ways you could: