zewelor / bt-mqtt-gateway

A simple Python script which provides a Bluetooth to MQTT gateway, easily extensible via custom workers. See https://github.com/zewelor/bt-mqtt-gateway/wiki for more information.
MIT License
549 stars 117 forks source link

Linak Desk: BT Error "Attribute can't be written" #144

Closed mountainsandcode closed 4 years ago

mountainsandcode commented 4 years ago

Describe the bug Appreciate this tool @zewelor - trying to get it working on the IKEA Desk running on a Raspberry Pi. Only deviation from the manual is that I'm not running it in a python venv, but it appears the bug below is unrelated.

If I remember correctly, it works correctly for you with an IKEA Desk? Any pointers would be greatly appreciated!

Debug gateway logs

2020-01-26 17:46:56,666 INFO bt-mqtt-gw gateway.py:61: - Starting 2020-01-26 17:46:56,669 DEBUG bt-mqtt-gw.mqtt mqtt.py:30:init - Setting LWT to: lwt_topic WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip. Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue. To avoid this problem you can invoke Python with '-m pip' instead of running pip directly. 2020-01-26 17:47:00,069 DEBUG bt-mqtt-gw.workers_manager workers_manager.py:106:register_workers - Added linakdesk worker with 1800 seconds interval and a 35 seconds timeout 2020-01-26 17:47:00,348 DEBUG bt-mqtt-gw.mqtt mqtt.py:116:callbacks_subscription - Subscribing to: linak_desk/update_interval 2020-01-26 17:47:00,349 DEBUG bt-mqtt-gw.mqtt mqtt.py:116:callbacks_subscription - Subscribing to: homeassistant/status 2020-01-26 17:47:00,355 DEBUG bt-mqtt-gw.workers_manager workers_manager.py:174:update_all - Updating all workers 2020-01-26 17:47:01,915 ERROR linak_dpg_bt.connection connection.py:91:make_request - Got exception from bluepy while making a request: Bluetooth command failed (code: 3, error: Attribute can't be written) 2020-01-26 17:47:01,919 ERROR bt-mqtt-gw.workers.linakdesk logger.py:50:log_exception - Error during update of linak desk 'linakdesk' (VALID_MAC): BTLEGattError Traceback (most recent call last): File "/home/pi/bt-mqtt-gateway/workers/linakdesk.py", line 43, in _get_height self.desk.read_dpg_data() File "/usr/local/lib/python3.7/dist-packages/linak_dpg_bt/linak_device.py", line 83, in read_dpg_data conn.dpg_command(PROP_DESK_OFFSET) File "/usr/local/lib/python3.7/dist-packages/linak_dpg_bt/connection.py", line 109, in dpg_command self.make_request(DPG_COMMAND_HANDLE, value) File "/usr/local/lib/python3.7/dist-packages/linak_dpg_bt/connection.py", line 92, in make_request raise ex File "/usr/local/lib/python3.7/dist-packages/linak_dpg_bt/connection.py", line 86, in make_request self._conn.writeCharacteristic(handle, value, withResponse=with_response) File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 543, in writeCharacteristic return self._getResp('wr') File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 407, in _getResp resp = self._waitResp(wantType + ['ntfy', 'ind'], timeout) File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 368, in _waitResp raise BTLEGattError("Bluetooth command failed", resp) bluepy.btle.BTLEGattError: Bluetooth command failed (code: 3, error: Attribute can't be written) 2020-01-26 17:47:01,924 ERROR bt-mqtt-gw logger.py:50:log_exception - Timeout while executing worker command Traceback (most recent call last): File "/home/pi/bt-mqtt-gateway/workers/linakdesk.py", line 43, in _get_height self.desk.read_dpg_data() File "/usr/local/lib/python3.7/dist-packages/linak_dpg_bt/linak_device.py", line 83, in read_dpg_data conn.dpg_command(PROP_DESK_OFFSET) File "/usr/local/lib/python3.7/dist-packages/linak_dpg_bt/connection.py", line 109, in dpg_command self.make_request(DPG_COMMAND_HANDLE, value) File "/usr/local/lib/python3.7/dist-packages/linak_dpg_bt/connection.py", line 92, in make_request raise ex File "/usr/local/lib/python3.7/dist-packages/linak_dpg_bt/connection.py", line 86, in make_request self._conn.writeCharacteristic(handle, value, withResponse=with_response) File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 543, in writeCharacteristic return self._getResp('wr') File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 407, in _getResp resp = self._waitResp(wantType + ['ntfy', 'ind'], timeout) File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 368, in _waitResp raise BTLEGattError("Bluetooth command failed", resp) bluepy.btle.BTLEGattError: Bluetooth command failed (code: 3, error: Attribute can't be written)

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "./gateway.py", line 73, in mqtt.publish(_WORKERS_QUEUE.get(timeout=10).execute()) File "/home/pi/bt-mqtt-gateway/workers_manager.py", line 58, in execute messages = self._callback(*self._args) File "/home/pi/bt-mqtt-gateway/workers/linakdesk.py", line 27, in status_update topic=self.format_topic("height/cm"), payload=self._get_height() File "/home/pi/bt-mqtt-gateway/workers/linakdesk.py", line 54, in _get_height raise DeviceTimeoutError exceptions.DeviceTimeoutError ^C2020-01-26 17:47:26,298 INFO bt-mqtt-gw gateway.py:85: - Finish current jobs and shut down. If you need force exit use kill `

Server (please complete the following information):

zewelor commented 4 years ago

Be sure to use newest Bluez ( https://github.com/zewelor/bt-mqtt-gateway/wiki/Upgrade-Bluez-on-Raspbian ) and pair desk first https://wiki.archlinux.org/index.php/Bluetooth#Pairing . Its always possible that remote used in this desk has some change in protocol and it wont work, without some tweaks.

mountainsandcode commented 4 years ago

Bluez is up-to-date (5.5) and the desk is paired - indeed it appears that the protocol may have changed. Any suggestions for reverse engineering that helped you get the original setup running which you could share? Would be greatly appreciated!

mountainsandcode commented 4 years ago

By the way, this is what I can find with bluetoothctl

[NEW] Primary Service /org/bluez/hci0/dev_EF_XX_YY_ZZ_AA_BB/service000a 00001801-0000-1000-8000-00805f9b34fb Generic Attribute Profile [NEW] Characteristic /org/bluez/hci0/dev_EF_XX_YY_ZZ_AA_BB/service000a/char000b 00002a05-0000-1000-8000-00805f9b34fb Service Changed [NEW] Descriptor /org/bluez/hci0/dev_EF_XX_YY_ZZ_AA_BB/service000a/char000b/desc000d 00002902-0000-1000-8000-00805f9b34fb Client Characteristic Configuration [NEW] Primary Service /org/bluez/hci0/dev_EF_XX_YY_ZZ_AA_BB/service000e 99fa0001-338a-1024-8a49-009c0215f78a Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_EF_XX_YY_ZZ_AA_BB/service000e/char000f 99fa0002-338a-1024-8a49-009c0215f78a Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_EF_XX_YY_ZZ_AA_BB/service000e/char0011 99fa0003-338a-1024-8a49-009c0215f78a Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_EF_XX_YY_ZZ_AA_BB/service000e/char0011/desc0013 00002902-0000-1000-8000-00805f9b34fb Client Characteristic Configuration [NEW] Primary Service /org/bluez/hci0/dev_EF_XX_YY_ZZ_AA_BB/service0014 99fa0010-338a-1024-8a49-009c0215f78a Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_EF_XX_YY_ZZ_AA_BB/service0014/char0015 99fa0011-338a-1024-8a49-009c0215f78a Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_EF_XX_YY_ZZ_AA_BB/service0014/char0015/desc0017 00002902-0000-1000-8000-00805f9b34fb Client Characteristic Configuration [NEW] Primary Service /org/bluez/hci0/dev_EF_XX_YY_ZZ_AA_BB/service0018 99fa0020-338a-1024-8a49-009c0215f78a Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_EF_XX_YY_ZZ_AA_BB/service0018/char0019 99fa0021-338a-1024-8a49-009c0215f78a Vendor specific [NEW] Descriptor /org/bluez/hci0/dev_EF_XX_YY_ZZ_AA_BB/service0018/char0019/desc001b 00002902-0000-1000-8000-00805f9b34fb Client Characteristic Configuration [NEW] Characteristic /org/bluez/hci0/dev_EF_XX_YY_ZZ_AA_BB/service0018/char001c 99fa0029-338a-1024-8a49-009c0215f78a Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_EF_XX_YY_ZZ_AA_BB/service0018/char001e 99fa002a-338a-1024-8a49-009c0215f78a Vendor specific [NEW] Primary Service /org/bluez/hci0/dev_EF_XX_YY_ZZ_AA_BB/service0020 99fa0030-338a-1024-8a49-009c0215f78a Vendor specific [NEW] Characteristic /org/bluez/hci0/dev_EF_XX_YY_ZZ_AA_BB/service0020/char0021 99fa0031-338a-1024-8a49-009c0215f78a Vendor specific

zewelor commented 4 years ago

Maybe try to run one of this forks: https://github.com/kbancerz/linak_bt_desk or https://github.com/anetczuk/linak_bt_desk . Maybe they are handling desk better. For general BLE RE something like this: https://medium.com/@urish/reverse-engineering-a-bluetooth-lightbulb-56580fcb7546

I've also dissasembled Android app and used sniffing + reading dissasembled code to describe protcol, but now I don't remember much from it, it was long ago :\

EDIT: Did you registered desk via some android app ? I think there was some setting to allow bluetooth control etc. Maybe you need to turn it on ? Or maybe there is some other procedure now ( on some newer linak remotes etc )

mountainsandcode commented 4 years ago

Hmm, can't get it to work with either of the forks, unfortunately. Thanks for sharing the guide.

I'm using the iOS App, but can't see an option like that unfortunately but I guess that means it's unrestricted (I hope) - I'll see what I can figure out from reverse-engineering.

One small request (not time critical): Any chance you could see if you can extract your list of characteristics using bluetoothctl? Would make it much easier to try to compare what they have changed. No worries if not :)

zewelor commented 4 years ago

Whats the command for characteristis extract ?

mountainsandcode commented 4 years ago

Had a chance to do further debugging on this one - I tried using some alternative scripts for the debugging. With https://github.com/apicore-engineering/linaktray, I can read out the state (i.e. height) of the desk without any problems, but I can't subscribe to changes or control the desk to a specific height. Appears to me (not being an expert) this may be a hardware limitation of the IDASEN desk

zewelor commented 4 years ago

It might be that auto move is disabled is this controller. In mine I had to turn it on explicit via app ( and there was info not every controller will allow it ). Maybe you can find other controller and swap it ( not sure if there is any authorization between desk / controller )

angadsingh commented 3 years ago

if auto move is disabled, how are all of. the following able to do it: https://github.com/j5lien/esphome-idasen-desk-controller https://apps.apple.com/us/app/desk-remote-control/id1509037746 https://github.com/DWilliames/idasen-controller-mac