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
550 stars 117 forks source link

Error with service startup #195

Open amaisano opened 3 years ago

amaisano commented 3 years ago

[x] I've read the Troubleshooting Wiki, my problem is not described there and I am already using the specified minimum bluez version.

Describe the bug Manually running sudo ./gateway.py -d works fine and I can see correct output. This is when I try to start the service:

A couple issues here. Looks like the logger cannot load the yaml module. It also says something about start request repeated too quickly.

(.venv) pi@raspberrypi:~/bt-mqtt-gateway $ sudo systemctl status bt-mqtt-gateway
● bt-mqtt-gateway.service - Bluetooth MQTT gateway
   Loaded: loaded (/etc/systemd/system/bt-mqtt-gateway.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2020-12-10 17:19:31 GMT; 980ms ago
     Docs: https://github.com/zewelor/bt-mqtt-gateway
 Main PID: 9799 (service.sh)
    Tasks: 2 (limit: 881)
   CGroup: /system.slice/bt-mqtt-gateway.service
           ├─9799 /bin/sh /home/pi/bt-mqtt-gateway/service.sh
           └─9802 python3 ./gateway.py

Dec 10 17:19:31 raspberrypi systemd[1]: Started Bluetooth MQTT gateway.
Dec 10 17:19:32 raspberrypi service.sh[9799]: Traceback (most recent call last):
Dec 10 17:19:32 raspberrypi service.sh[9799]:   File "./gateway.py", line 11, in <module>
Dec 10 17:19:32 raspberrypi service.sh[9799]:     import logger
Dec 10 17:19:32 raspberrypi service.sh[9799]:   File "/home/pi/bt-mqtt-gateway/logger.py", line 3, in <module>
Dec 10 17:19:32 raspberrypi service.sh[9799]:     import yaml
Dec 10 17:19:32 raspberrypi service.sh[9799]: ModuleNotFoundError: No module named 'yaml'
(.venv) pi@raspberrypi:~/bt-mqtt-gateway $ sudo systemctl status bt-mqtt-gateway
● bt-mqtt-gateway.service - Bluetooth MQTT gateway
   Loaded: loaded (/etc/systemd/system/bt-mqtt-gateway.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Thu 2020-12-10 17:19:32 GMT; 9s ago
     Docs: https://github.com/zewelor/bt-mqtt-gateway
  Process: 9799 ExecStart=/home/pi/bt-mqtt-gateway/service.sh (code=exited, status=1/FAILURE)
 Main PID: 9799 (code=exited, status=1/FAILURE)

Dec 10 17:19:32 raspberrypi systemd[1]: bt-mqtt-gateway.service: Service RestartSec=100ms expired, scheduling restart.
Dec 10 17:19:32 raspberrypi systemd[1]: bt-mqtt-gateway.service: Scheduled restart job, restart counter is at 5.
Dec 10 17:19:32 raspberrypi systemd[1]: Stopped Bluetooth MQTT gateway.
Dec 10 17:19:32 raspberrypi systemd[1]: bt-mqtt-gateway.service: Start request repeated too quickly.
Dec 10 17:19:32 raspberrypi systemd[1]: bt-mqtt-gateway.service: Failed with result 'exit-code'.
Dec 10 17:19:32 raspberrypi systemd[1]: Failed to start Bluetooth MQTT gateway.

To Reproduce Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior Service to start and stay running

Config

mqtt:
  host: 192.168.**.**
  port: 1883
  ca_verify: False
  client_id: bt-mqtt-gateway
  availability_topic: lwt_topic

manager:
  sensor_config:
    topic: homeassistant
    retain: true
  topic_subscription:
    update_all:
      topic: homeassistant/status
      payload: online
  command_timeout: 35
  workers:
    toothbrush_homeassistant:
      args:
        autodiscovery_prefix: homeassistant
        topic_prefix: toothbrush
        devices:
          quip:
            name: Quip
            mac: E9:*********
      update_interval: 10

Debug gateway logs

(.venv) pi@raspberrypi:~/bt-mqtt-gateway $ sudo ./gateway.py -d
2020-12-10 17:25:17,859 INFO bt-mqtt-gw gateway.py:75:<module> - Starting
2020-12-10 17:25:17,866 DEBUG bt-mqtt-gw.mqtt mqtt.py:30:__init__ - Setting LWT to: lwt_topic
2020-12-10 17:25:26,001 DEBUG bt-mqtt-gw.workers_manager workers_manager.py:107:register_workers - Added toothbrush_homeassistant worker with 10 seconds 
interval and a 35 seconds timeout
2020-12-10 17:25:26,057 DEBUG bt-mqtt-gw.mqtt mqtt.py:117:callbacks_subscription - Subscribing to: toothbrush/update_interval
2020-12-10 17:25:26,061 DEBUG bt-mqtt-gw.mqtt mqtt.py:117:callbacks_subscription - Subscribing to: homeassistant/status
2020-12-10 17:25:26,091 DEBUG bt-mqtt-gw.workers_manager workers_manager.py:173:update_all - Updating all workers
2020-12-10 17:25:26,505 DEBUG bt-mqtt-gw.workers.toothbrush_homeassistant toothbrush_homeassistant.py:102:handleDiscovery - Discovered new device: 1c:*********
.....

Server (please complete the following information):

Additional context PiZero W

zewelor commented 3 years ago

Probably you don't have required packages installed as root user, or pi user ( depending if you changed service file to run as user )

harristom commented 3 years ago

I have the same issue, also on a Pi Zero. I installed the required packages within the virtual environment following the instructions. I see that service.sh adds the venv to path so theoretically it should work but I suspect the problem lies here somewhere.

shaaati commented 3 years ago

I had the same issue and at least for me, the solution in #194 solved it. Seems like there is a minor mistake in the readme file.

I haven't checked it extensively so far, but at least the service now starts.

h4de5 commented 3 years ago

similar problem on rpi0w but starting it within docker. up until now i was not able to get out any logs from the container as it keeps restarting.

mike0xt commented 3 years ago

I had issues getting the service.sh started as well. I had to do #194 by comment out lines 3-9. Before doing that, when executing ./gateway.sh, I ran into btle.py errors, which was fixed with setcap...probably not very secure.

sudo setcap 'cap_net_raw,cap_net_admin+eip' /usr/local/lib/python3.7/dist-packages/bluepy/bluepy-helper

I also had to modify the bt-mqtt-gateway.service to add ExecStartPre=/bin/sleep 30 because the service failed to start when system was reboot. Probably needs some python service started first.