giejay / nukiPyBridge

30 stars 9 forks source link

Configuration and Setup #4

Open add30417 opened 4 years ago

add30417 commented 4 years ago

Dear Giejay,

Thanks for maintaining this project. Honestly I have the issue now to setup at my raspberry. I recognized you are using docker, but besides of the 10 Steps out of the Readme file, I can't go ahead.

Maybe, can you please add more step how someone, like me, setup this from scratch ?

Thanks in advanced Daniel

giejay commented 4 years ago

The readme is a bit outdated. You can use this script to set it up: https://github.com/giejay/nukiPyBridge/blob/master/install-bluetooth-deps.sh

At what step are you stuck?

add30417 commented 4 years ago

Thanks for your answer.

First of all, I used a RP 3 B+. I downloaded the latest version from Raspbian and wrote a new sd card.

I found out the MAC address of my nuki.

I followed the 10 steps from the Readme file.

But now what is the next step, I couldn't found the NaCl utility on my sd card. And even what should I do with the files from the git Repository? Can I use a docker? Or should I used without docker?

Honestly, I guess isn't rocket sicence any more, but just the next steps and the best way would be very helpful.

But I'm thanksful to you, you are maintining this project.

giejay commented 4 years ago

Did you try to install it using the file I mentioned? After that, you should have a running supervised service on port 5000. You can browse to that port and checkout the methods to use in server.py.

add30417 commented 4 years ago

Just doing at the moment.

First run, with the error: ./install-bluetooth-deps.sh: line 20: ./install-bluez.sh: No such file or directory cp: cannot stat 'nuki-supervised.conf': No such file or directory

giejay commented 4 years ago

Did you clone the entire repo or just copied that file?

add30417 commented 4 years ago

I see, I guessed it is a "web" installer. I'm doing again. --- I will do the testing in the next days.

add30417 commented 4 years ago

Now, I installed everything.

Following modifciation I did: connect-example.py: changing macaddress of nuki run.sh: changed with raspberry pi ip address nuki.conf: changed with raspberry pi ip address

I used the pi user.

When I'm excuting the server.py, following message I will got back:

./connect-example.py Starting BLE adapter... Failed to restart bluetooth.service: Unit bluetooth.service failed to load properly: Bad message. See system logs and 'systemctl status bluetooth.service' for details. Init Nuki BLE connection... Unable to connect, retrying..., retry count: 1 Starting BLE adapter... Failed to restart bluetooth.service: Unit bluetooth.service failed to load properly: Bad message. See system logs and 'systemctl status bluetooth.service' for details. Init Nuki BLE connection... Unable to connect, retrying..., retry count: 2 Starting BLE adapter... Failed to restart bluetooth.service: Unit bluetooth.service failed to load properly: Bad message. See system logs and 'systemctl status bluetooth.service' for details. Init Nuki BLE connection... Unable to connect, retrying..., retry count: 3 Nuki BLE connection established Traceback (most recent call last): File "./connect-example.py", line 18, in nuki.Nuki(nukiMacAddress).authenticateUser(myPublicKeyHex, myPrivateKeyHex, myID, myIDType, name) File "/home/pi/nuki/nuki.py", line 92, in authenticateUser pairingHandle = self.device.get_handle('a92ee101-5501-11e4-916c-0800200c9a66') AttributeError: 'NoneType' object has no attribute 'get_handle'

Error of bluetooth.service: /lib/systemd/system/bluetooth.service:1: Invalid section header '[Unit] Description=Bluetooth service Documentation=man:bluetoothd(8) Co...

giejay commented 4 years ago

There is something wrong with your bluetooth.service file. Can you post the content?

add30417 commented 4 years ago

cat /lib/systemd/system/bluetooth.service

[Unit] Description=Bluetooth service Documentation=man:bluetoothd(8) ConditionPathIsDirectory=/sys/class/bluetooth

[Service] Type=dbus BusName=org.bluez ExecStart=/usr/local/libexec/bluetooth/bluetoothd NotifyAccess=main

WatchdogSec=10

Restart=on-failure

CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE LimitNPROC=1 ProtectHome=true ProtectSystem=full

[Install] WantedBy=bluetooth.target Alias=dbus-org.bluez.service

giejay commented 4 years ago

Please put a line break between [Unit] and Description

add30417 commented 4 years ago

Seems like step ahead:

During the excution of "./connect-example.py" following output appears: Init Nuki BLE connection... Unable to connect, retrying..., retry count: 1 Starting BLE adapter... Init Nuki BLE connection... Unable to connect, retrying..., retry count: 2 Starting BLE adapter... Init Nuki BLE connection... Nuki BLE connection established Nuki Pairing UUID handle created: 008b Requesting Nuki Public Key using command: Nuki_REQ Payload: Nuki_PUBLIC_KEY Nuki Public key requested Nuki returned unexpected response (expecting PUBLIC_KEY): Nuki_ERROR Error Code: 10 Command Identifier: 0001

During second and third execution, following output appears: Starting BLE adapter... Init Nuki BLE connection... Unable to connect, retrying..., retry count: 1 Starting BLE adapter... Init Nuki BLE connection... Unable to connect, retrying..., retry count: 2 Starting BLE adapter... Init Nuki BLE connection... Unable to connect, retrying..., retry count: 3 Nuki BLE connection established Traceback (most recent call last): File "./connect-example.py", line 18, in nuki.Nuki(nukiMacAddress).authenticateUser(myPublicKeyHex, myPrivateKeyHex, myID, myIDType, name) File "/home/pi/nuki/nuki.py", line 92, in authenticateUser pairingHandle = self.device.get_handle('a92xxxxxxxxxxxxxxxxxxxxxxxxxx') AttributeError: 'NoneType' object has no attribute 'get_handle'

giejay commented 4 years ago

Did you start the pairing process on your Nuki by holding it for 5 seconds?

add30417 commented 4 years ago

Yes, I did.

Update: Even the config file (/home/pi/nuki/nuki.cfg) never created during the execution. I created (with touch command) a empty file, but no success.

giejay commented 4 years ago

Can you try it by using the connect endpoint? See server.py for the details

add30417 commented 4 years ago

Bascially the server.py isn't helpful me. Is it? I can't see any helpful information.

giejay commented 4 years ago

It mentions the exact methods you can use for connecting and unlocking. For example: @app.route("/connect//")

So go to localhost:5000/connect/macaddress/frontdoor and it will try to pair

add30417 commented 4 years ago

the server isn't working at my raspberry

giejay commented 4 years ago

Are you running it using the supervised service? What does 'sudo supervisorctl status nuki' give you?

add30417 commented 4 years ago

sudo supervisorctl status nuki

nuki FATAL can't find command '/usr/local/bin/flask'

Seems not.

giejay commented 4 years ago

The install bluetooth deps script should have installed flask...

add30417 commented 4 years ago

It is located in /usr/local/, I changed the config file

add30417 commented 4 years ago

When I execute the server.py, I got the same error message back.

172.17.0.50 - - [14/Sep/2020 21:19:55] "GET /favicon.ico HTTP/1.1" 404 - [2020-09-14 21:20:27,854] ERROR in app: Exception on /connect/xxxxxxx/FrontDoor [GET] Traceback (most recent call last): File "/usr/lib/python3/dist-packages/flask/app.py", line 2292, in wsgi_app response = self.full_dispatch_request() File "/usr/lib/python3/dist-packages/flask/app.py", line 1815, in full_dispatch_request rv = self.handle_user_exception(e) File "/usr/lib/python3/dist-packages/flask/app.py", line 1718, in handle_user_exception reraise(exc_type, exc_value, tb) File "/usr/lib/python3/dist-packages/flask/_compat.py", line 35, in reraise raise value File "/usr/lib/python3/dist-packages/flask/app.py", line 1813, in full_dispatch_request rv = self.dispatch_request() File "/usr/lib/python3/dist-packages/flask/app.py", line 1799, in dispatch_request return self.view_functionsrule.endpoint File "/home/pi/nuki/server.py", line 40, in connect nuki.Nuki(mac_address).authenticateUser(myPublicKeyHex, myPrivateKeyHex, myID, myIDType, name) File "/home/pi/nuki/nuki.py", line 92, in authenticateUser pairingHandle = self.device.get_handle('a92ee101-5501-11e4-916c-0800200c9a66') AttributeError: 'NoneType' object has no attribute 'get_handle' 172.17.0.50 - - [14/Sep/2020 21:20:27] "GET /connect/xxxxxxxxx/FrontDoor HTTP/1.1" 500 - 172.17.0.50 - - [14/Sep/2020 21:20:27] "GET /favicon.ico HTTP/1.1" 404 -

giejay commented 4 years ago

Are you using a nuki v2?

add30417 commented 4 years ago

Yes, I'm using v2

giejay commented 4 years ago

Can you add --experimental to: ExecStart=/usr/local/libexec/bluetooth/bluetoothd.

What version are you using of bluez?

add30417 commented 4 years ago

I got following log:

Sep 14 22:05:22 rasppi bluetoothd[2448]: Endpoint registered: sender=:1.43 path=/org/bluez/hci0/A2DP/SBC/Source/1 Sep 14 22:05:22 rasppi bluetoothd[2448]: Endpoint registered: sender=:1.43 path=/org/bluez/hci0/A2DP/SBC/Source/2 Sep 14 22:05:22 rasppi kernel: [ 176.998435] debugfs: File 'le_min_key_size' in directory 'hci0' already present! Sep 14 22:05:22 rasppi kernel: [ 176.998459] debugfs: File 'le_max_key_size' in directory 'hci0' already present! Sep 14 22:05:31 rasppi systemd[1]: Stopping BluezALSA proxy... Sep 14 22:05:31 rasppi bluetoothd[2448]: Endpoint unregistered: sender=:1.43 path=/org/bluez/hci0/A2DP/SBC/Source/1 Sep 14 22:05:31 rasppi bluetoothd[2448]: Endpoint unregistered: sender=:1.43 path=/org/bluez/hci0/A2DP/SBC/Source/2 Sep 14 22:05:31 rasppi systemd[1]: bluealsa.service: Succeeded. Sep 14 22:05:31 rasppi systemd[1]: Stopped BluezALSA proxy. Sep 14 22:05:31 rasppi bluetoothd[2448]: Terminating Sep 14 22:05:31 rasppi systemd[1]: Stopping Bluetooth service... Sep 14 22:05:31 rasppi bluetoothd[2448]: Stopping SDP server Sep 14 22:05:31 rasppi bluetoothd[2448]: Exit Sep 14 22:05:31 rasppi systemd[1]: bluetooth.service: Succeeded. Sep 14 22:05:31 rasppi systemd[1]: Stopped Bluetooth service. Sep 14 22:05:32 rasppi systemd[1]: Starting Bluetooth service... Sep 14 22:05:32 rasppi bluetoothd[2597]: Bluetooth daemon 5.51 Sep 14 22:05:32 rasppi systemd[1]: Started Bluetooth service. Sep 14 22:05:32 rasppi bluetoothd[2597]: Starting SDP server Sep 14 22:05:32 rasppi bluetoothd[2597]: Bluetooth management interface 1.14 initialized Sep 14 22:05:32 rasppi systemd[1]: Started BluezALSA proxy. Sep 14 22:05:32 rasppi bluetoothd[2597]: Endpoint registered: sender=:1.45 path=/org/bluez/hci0/A2DP/SBC/Source/1 Sep 14 22:05:32 rasppi bluetoothd[2597]: Endpoint registered: sender=:1.45 path=/org/bluez/hci0/A2DP/SBC/Source/2 Sep 14 22:05:32 rasppi kernel: [ 187.132985] debugfs: File 'le_min_key_size' in directory 'hci0' already present! Sep 14 22:05:32 rasppi kernel: [ 187.133011] debugfs: File 'le_max_key_size' in directory 'hci0' already present! Sep 14 22:05:38 rasppi systemd[1]: Stopping BluezALSA proxy... Sep 14 22:05:38 rasppi systemd[1]: bluealsa.service: Succeeded. Sep 14 22:05:38 rasppi bluetoothd[2597]: Endpoint unregistered: sender=:1.45 path=/org/bluez/hci0/A2DP/SBC/Source/1 Sep 14 22:05:38 rasppi bluetoothd[2597]: Endpoint unregistered: sender=:1.45 path=/org/bluez/hci0/A2DP/SBC/Source/2 Sep 14 22:05:38 rasppi systemd[1]: Stopped BluezALSA proxy. Sep 14 22:05:38 rasppi bluetoothd[2597]: Terminating Sep 14 22:05:38 rasppi systemd[1]: Stopping Bluetooth service... Sep 14 22:05:38 rasppi bluetoothd[2597]: Stopping SDP server Sep 14 22:05:38 rasppi bluetoothd[2597]: Exit Sep 14 22:05:38 rasppi systemd[1]: bluetooth.service: Succeeded. Sep 14 22:05:38 rasppi systemd[1]: Stopped Bluetooth service. Sep 14 22:05:38 rasppi systemd[1]: Starting Bluetooth service... Sep 14 22:05:38 rasppi bluetoothd[2774]: Bluetooth daemon 5.51 Sep 14 22:05:38 rasppi systemd[1]: Started Bluetooth service. Sep 14 22:05:38 rasppi bluetoothd[2774]: Starting SDP server Sep 14 22:05:38 rasppi bluetoothd[2774]: Bluetooth management interface 1.14 initialized Sep 14 22:05:38 rasppi systemd[1]: Started BluezALSA proxy. Sep 14 22:05:38 rasppi kernel: [ 193.211177] debugfs: File 'le_min_key_size' in directory 'hci0' already present! Sep 14 22:05:38 rasppi kernel: [ 193.211200] debugfs: File 'le_max_key_size' in directory 'hci0' already present! Sep 14 22:05:40 rasppi bluetoothd[2774]: Endpoint registered: sender=:1.47 path=/org/bluez/hci0/A2DP/SBC/Source/1 Sep 14 22:05:40 rasppi bluetoothd[2774]: Endpoint registered: sender=:1.47 path=/org/bluez/hci0/A2DP/SBC/Source/2

add30417 commented 4 years ago

bluez is already the newest version (5.50-1.2~deb10u1+rpt2).

add30417 commented 4 years ago

Any recommendation how I can fix this topic?

Thanks in advanced.

giejay commented 4 years ago

Something is stopping your Bluez service and I'm not sure what. Before doing anything with the lock, you have to make sure you have a stable bluetooth service.

Do you only see those logs when interacting with the lock or are the logs always visible?

NopfatGithub commented 4 years ago

https://learn.adafruit.com/install-bluez-on-the-raspberry-pi/installation

Blues should work out of the box. Maybe check in the raspberry installation menu : raspi-config

Maybe restart the raspberry : sudo reboot

Does : hcitool lescan show devices?

Try step 2 from this tutorial: https://github.com/tobiasfeistmantl/python-nuki-client

Did you copy the gattool to path : ... Pyhton3.7 ....

Btw: thank you @giejay !

add30417 commented 4 years ago

Thanks @giejay and @NopfatGithub for your comments.

I tried with a second PI (PI 4) with fresh installation but also not working. Bluetooth in general is working and stable, tested with headset and a LEGO smart hub.

hcitool shows me around 10 devices, including my Nuki.

Yes, I copied the gatttool.py in the right folder. And I'm using python3.7 for execution of the scripts.

But the same results as before.

giejay commented 4 years ago

I'm gonna try a brand new install tomorrow on a raspberry pi and I'll see if I run into the same issues as you.

add30417 commented 4 years ago

@giejay, Thanks looking forward to your feedback.

giejay commented 4 years ago

I have updated the repo and added retry mechanism (to improve stability and speed) and updated the Readme. Also added logging to get some more insights into your problem. Can you try again?

add30417 commented 4 years ago

Thanks for your feedback. I tried it again.

One issue, I could fixed: cp: cannot create regular file '/usr/local/lib/python3.6/dist-packages/pygatt/backends/gatttool/gatttool.py': No such file or directory

For the second one, I don't have any experiences: pi@rasp: /home/pi/nukiPyBridge# sudo supervisorctl start nuki nuki: ERROR (no such file)

Do you have any advice for me?

giejay commented 4 years ago

Where does your python install the packages? What version do you have? Can you do an "ls /usr/local/lib"?

Because that step failed, the step after that also fails:

sudo cp nuki-supervised.conf /etc/supervisor/conf.d/nuki.conf

You can do this manually and then do a start nuki again.

add30417 commented 4 years ago

Ls output: cups pypy2.7 python2.7 python3.7

I did "sudo cp nuki-supervised.conf /etc/supervisor/conf.d/nuki.conf" already before.

giejay commented 4 years ago

Can you try a sudo supervisorctl reread and a "sudo supervisorctl update"?

And this should be the step for the gatttool copy:

sudo cp gatttool.py /usr/local/lib/python3.7/dist-packages/pygatt/backends/gatttool/gatttool.py
add30417 commented 4 years ago

The copy command was ok. But I updated the nuki.conf file: [program:nuki]

command=/usr/bin/flask run -h 172.17.0.70 #I updated the path information and IP address environment=FLASK_APP="server.py",PYTHONUNBUFFERED=true directory=/home/pi/nukiPyBridge/ #I updated the path information redirect_stderr=true startsecs=5 autorestart=true itdout_logfile=/home/pi/nukiPyBridge/server.log #I updated the path information

The server is starting now, but I can't access from my PC.

add30417 commented 4 years ago

I also updated the run.sh:

cd /home/pi/nukiPyBridge

export FLASK_APP=server.py flask run -h 172.17.0.70 -p 10000

add30417 commented 4 years ago

I moved to /home/pi/nuki everything and updated the all above mention path.

But same results again.

But I found out, no server.log is created.

giejay commented 4 years ago

Can you do a

sudo supervisorctl tail nuki

giejay commented 4 years ago

You probably also need

pip3 install retry

add30417 commented 4 years ago

root@raspberrypi:/home/pi/nuki# pip3 install retry Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple Requirement already satisfied: retry in /usr/local/lib/python3.7/dist-packages (0.9.2) Requirement already satisfied: py<2.0.0,>=1.4.26 in /usr/local/lib/python3.7/dist-packages (from retry) (1.9.0) Requirement already satisfied: decorator>=3.4.2 in /usr/local/lib/python3.7/dist-packages (from retry) (4.4.2)

giejay commented 4 years ago

And the tail command?

add30417 commented 4 years ago

right

/home/pi/nuki# sudo supervisorctl tail nuki

If I using http://172.17.0.70:5000/connect/54:ddddedsd/frontdoor follow update is comming: Connected to 54: ddddedsd

But still no server log.

add30417 commented 4 years ago

If I'm using http://172.17.0.70:5000/frontdoor/lock or unlock following message is coming:

Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

giejay commented 4 years ago

If you do

sudo supervisorctl tail -f nuki

And then call the lock endpoint, you don't see any logs?

add30417 commented 4 years ago

sudo supervisorctl tail -f nuki ==> Press Ctrl-C to exit <== ckages/flask/app.py", line 2292, in wsgi_app response = self.full_dispatch_request() File "/usr/lib/python3/dist-packages/flask/app.py", line 1815, in full_dispatch_request rv = self.handle_user_exception(e) File "/usr/lib/python3/dist-packages/flask/app.py", line 1718, in handle_user_exception reraise(exc_type, exc_value, tb) File "/usr/lib/python3/dist-packages/flask/_compat.py", line 35, in reraise raise value File "/usr/lib/python3/dist-packages/flask/app.py", line 1813, in full_dispatch_request rv = self.dispatch_request() File "/usr/lib/python3/dist-packages/flask/app.py", line 1799, in dispatch_request return self.view_functionsrule.endpoint File "/home/pi/nuki/server.py", line 45, in lock_door return execute_action('LOCK', door) File "/home/pi/nuki/server.py", line 64, in execute_action nuki.Nuki(config[door]).lockAction(type) KeyError: 'frontdoor' INFO:werkzeug:172.17.0.50 - - [07/Oct/2020 20:24:48] "GET /frontdoor/lock HTTP/1.1" 500 -

giejay commented 4 years ago

Ah yes. That's still a bug, you need to restart after connecting a new lock.

sudo supervisorctl restart nuki