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
548 stars 116 forks source link

pi@raspberrypi:~/bt-mqtt-gateway $ sudo systemctl status bt-mqtt-gateway error #47

Closed kenand1970 closed 5 years ago

kenand1970 commented 5 years ago

I try ble mqtt gateway pi zero w and Amlogic TV box with armbian. I receive same error. ./gateway.py -d works I see data: [{'payload': 21.6, 'topic': 'mithermometer/living_room/temperature'}, {'payload': 54.4, 'topic': 'mithermometer/living_room/humidi ty'}, {'payload': 69, 'topic': 'mithermometer/living_room/battery'}]

but when I check bt-mqtt-gateway status a see error: 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 Fri 2019-01-25 11:49:57 CET; 3h 34min ago Docs: https://github.com/zewelor/bt-mqtt-gateway Main PID: 421 (python3) CGroup: /system.slice/bt-mqtt-gateway.service └─421 python3 /home/pi/bt-mqtt-gateway/gateway.py

jan 25 15:01:03 raspberrypi gateway.py[421]: ret.append(MqttMessage(topic=self.format_topic(name, attr), payload=poller.parameter_value(attr))) jan 25 15:01:03 raspberrypi gateway.py[421]: File "/usr/local/lib/python3.5/dist-packages/mithermometer/mithermometer_poller.py", line 136, in param jan 25 15:01:03 raspberrypi gateway.py[421]: self.fill_cache() jan 25 15:01:03 raspberrypi gateway.py[421]: File "/usr/local/lib/python3.5/dist-packages/mithermometer/mithermometer_poller.py", line 67, in fill_c jan 25 15:01:03 raspberrypi gateway.py[421]: with self._bt_interface.connect(self._mac) as connection: jan 25 15:01:03 raspberrypi gateway.py[421]: File "/usr/local/lib/python3.5/dist-packages/btlewrap/base.py", line 44, in enter jan 25 15:01:03 raspberrypi gateway.py[421]: self._backend.connect(self._mac) jan 25 15:01:03 raspberrypi gateway.py[421]: File "/usr/local/lib/python3.5/dist-packages/btlewrap/bluepy.py", line 32, in _func_wrapper jan 25 15:01:03 raspberrypi gateway.py[421]: raise BluetoothBackendException() from last_error jan 25 15:01:03 raspberrypi gateway.py[421]: btlewrap.base.BluetoothBackendException pi@raspberrypi:~/bt-mqtt-gateway $

And I do not see qttt data in nodered

How cna I solve this error?

zewelor commented 5 years ago

There is some error with bluetooth adapter. From that logs and info, maybe you try to run 2 instances of the gateway ? One manually with ./gateway.py -d and second one via system service ?

kenand1970 commented 5 years ago

Dear Zewelor, Thank you very much your fast answer. You are right, I run 2 instances.
How Can I test system service? Becuse I do not see bt-mqtt-gateway data my nodred

zewelor commented 5 years ago

I test only ./gateway -d , and if it works I just start system service. It should work then. You can check logs via: journalctl -xef -u bt-mqtt-gateway . And be sure to run only system service then. Maybe there will be something in he logs. Also I would suggest to use mosquitto_sub to monitor all of the mqtt messages, if anything is being sent.

kenand1970 commented 5 years ago

Dear zewelor, I stopped system service and I use onlly ./gateway -d

It woked in morning, but a bit later error message occured

pi@raspberrypi:~/bt-mqtt-gateway $ sudo ./gateway.py -d Starting Setting LWT to: hostname/lwt_topic WARNING: No module named 'pexpect' Added: mithermometer with 300 seconds interval Added: miflora with 300 seconds interval Subscribing to: hostname/homeassistant/status Updating all workers [{'topic': 'mithermometer/living_room/temperature', 'payload': 22.3}, {'topic': 'mithermometer/living_room/humidity', 'payload': 53.1}, {'topic': 'mithermometer /living_room/battery', 'payload': 69}, {'topic': 'mithermometer/corridor/tempera ture', 'payload': 25.3}, {'topic': 'mithermometer/corridor/humidity', 'payload': 44.5}, {'topic': 'mithermometer/corridor/battery', 'payload': 98}] [{'topic': 'miflora/herbs/temperature', 'payload': 21.0}, {'topic': 'miflora/her bs/moisture', 'payload': 48}, {'topic': 'miflora/herbs/light', 'payload': 164}, {'topic': 'miflora/herbs/conductivity', 'payload': 1137}, {'topic': 'miflora/her bs/battery', 'payload': 99}] [{'topic': 'mithermometer/living_room/temperature', 'payload': 22.3}, {'topic': 'mithermometer/living_room/humidity', 'payload': 52.9}, {'topic': 'mithermometer /living_room/battery', 'payload': 69}] [] [] [] [] [] ^CExiting allowing jobs to finish. If you need force exit use kill pi@raspberrypi:~/bt-mqtt-gateway $ sudo ./gateway.py -d Starting Setting LWT to: hostname/lwt_topic WARNING: No module named 'pexpect' Added: mithermometer with 300 seconds interval Exception: Traceback (most recent call last): File "/usr/lib/python3/dist-packages/pip/basecommand.py", line 215, in main status = self.run(options, args) File "/usr/lib/python3/dist-packages/pip/commands/install.py", line 353, in ru n wb.build(autobuilding=True) File "/usr/lib/python3/dist-packages/pip/wheel.py", line 749, in build self.requirement_set.prepare_files(self.finder) File "/usr/lib/python3/dist-packages/pip/req/req_set.py", line 380, in prepare _files ignore_dependencies=self.ignore_dependencies)) File "/usr/lib/python3/dist-packages/pip/req/req_set.py", line 666, in _prepar e_file check_dist_requires_python(dist) File "/usr/lib/python3/dist-packages/pip/utils/packaging.py", line 48, in chec k_dist_requires_python feed_parser.feed(metadata) File "/usr/lib/python3.5/email/feedparser.py", line 178, in feed self._input.push(data) File "/usr/lib/python3.5/email/feedparser.py", line 104, in push self._partial.write(data) TypeError: string argument expected, got 'NoneType' Added: miflora with 300 seconds interval Subscribing to: hostname/homeassistant/status Updating all workers [] []

kenand1970 commented 5 years ago

Dear zewelor, bt-mqtt gateway sometimes wokrs, sometimes do not work. (pi zero w) I switched to miflora-mqtt daemon isntead. (https://github.com/ThomDietrich/miflora-mqtt-daemon) miflora-mqtt works well everytime. But I have 3 Xiaomi Mijia LCD sensors also, that I sould prefer your bt-mqtt-gateway.....

zewelor commented 5 years ago

Hi

For rpi zero w, be sure to use latest bluez ( https://blog.bluetooth.com/bluez-on-raspberry-pi-update ). From what I can see. miflora-mqtt-daemon uses the same miflora lib, but with different bluetooth backend ( https://github.com/ThomDietrich/miflora-mqtt-daemon/blob/master/miflora-mqtt-daemon.py#L220 ), which according to https://github.com/ChristianKuehnel/btlewrap#bluezgatttool-wrapper is less stable, but maybe in older version it works more stable ? I think best way would be to update bluez and try with it. If not I could make git branch with different btlewrap backend to test.

kenand1970 commented 5 years ago

Dear zwelor, I updated my pi zero w to bluez 5.50

I tested bt-mqtt-gateway with sudo ./gateway.py -d

Starting Setting LWT to: hostname/lwt_topic WARNING: No module named 'pexpect' Added: mithermometer with 120 seconds interval Subscribing to: hostname/homeassistant/status Updating all workers [{'topic': 'mithermometer/corridor/temperature', 'payload': 23.4}, {'topic': 'mithermometer/corridor/humidity', 'payload': 50.2}, {'topic': 'mithermometer/corridor/battery', 'payload': 97}, {'topic': 'mithermometer/living_room/temperature', 'payload': 22.0}, {'topic': 'mithermometer/living_room/humidity', 'payload': 55.1}, {'topic': 'mithermometer/living_room/battery', 'payload': 69}] [{'topic': 'mithermometer/corridor/temperature', 'payload': 23.2}, {'topic': 'mithermometer/corridor/humidity', 'payload': 49.8}, {'topic': 'mithermometer/corridor/battery', 'payload': 97}, {'topic': 'mithermometer/living_room/temperature', 'payload': 21.9}, {'topic': 'mithermometer/living_room/humidity', 'payload': 55.3}, {'topic': 'mithermometer/living_room/battery', 'payload': 69}] [{'topic': 'mithermometer/corridor/temperature', 'payload': 23.3}, {'topic': 'mithermometer/corridor/humidity', 'payload': 49.9}, {'topic': 'mithermometer/corridor/battery', 'payload': 97}] [] [{'topic': 'mithermometer/corridor/temperature', 'payload': 23.3}, {'topic': 'mithermometer/corridor/humidity', 'payload': 50.1}, {'topic': 'mithermometer/corridor/battery', 'payload': 97}, {'topic': 'mithermometer/living_room/temperature', 'payload': 21.9},

I tray to subscribe message with mosquitto_sub an other teminal:

(for exapmle: topic mithermometer/corridor/temperature)

pi@raspberrypi:~ $ mosquitto_sub -h 192.168.1.123 -d -t mithermometer/corridor/temperature Client mosqsub/764-raspberrypi sending CONNECT Client mosqsub/764-raspberrypi received CONNACK Client mosqsub/764-raspberrypi sending SUBSCRIBE (Mid: 1, Topic: mithermometer/corridor/temperature, QoS: 0) Client mosqsub/764-raspberrypi received SUBACK Subscribed (mid: 1): 0 Client mosqsub/764-raspberrypi sending PINGREQ Client mosqsub/764-raspberrypi received PINGRESP Client mosqsub/764-raspberrypi sending PINGREQ Client mosqsub/764-raspberrypi received PINGRESP Client mosqsub/764-raspberrypi sending PINGREQ Client mosqsub/764-raspberrypi received PINGRESP Client mosqsub/764-raspberrypi sending PINGREQ Client mosqsub/764-raspberrypi received PINGRESP

But did not receive any mesage ....... Is it correct ? mosquitto_sub -h 192.168.1.123 -d -t mithermometer/corridor/temperature

zewelor commented 5 years ago

Try to set client_id in config.yaml . I think that helps sometimes. From that log everything looks fine.

kenand1970 commented 5 years ago

Dear zewelor, how can I set client_id?

This is my configuration.yaml

mqtt: host: 192.168.1.123 port: 1883 username: password: topic_prefix: hostname client_id: bt-mqtt-gateway availability_topic: lwt_topic

manager: topic_subscription: update_all: topic: homeassistant/status payload: online workers: mithermometer: args: devices: living_room: 4C:65:A8:D8:AD:A1

corridor: 4C:65:A8:DC:53:82

    topic_prefix: mithermometer
  update_interval: 120

It's seems to me sudo ./gateway.py -d

put data only the terminal, but I can not receive data from MQTT

zewelor commented 5 years ago

Delete username and password from config. Also for mosquitto_sub try topic -t \# ( listen to all topic ). As you've left topic_prefix, the actual topic is hostname/mithermometer/corridor/temperature , thats why when you filter to only mithermometer/corridor/temperature you don't see anything. Delete topic_prefix also, from the config.

This should print all messages. mosquitto_sub -h 192.168.1.123 -d -t \#

kenand1970 commented 5 years ago

Dear zeweleor,

Thank you very much your patience, and your help. It works for me at now.

zewelor commented 5 years ago

Great to hear that. Maybe you can propose some README improvements, to make it easier to setup ?

kenand1970 commented 5 years ago

Dear zewelor,

Many user use this topic for to compile Bluez https://www.domoticz.com/wiki/Mi_Flora_Bluetooth_LE This use Bluez 5.44 only Link newest bluez description: https://www.bluetooth.com/~/media/files/developer/t1804_how%20to%20set%20up%20bluez_lfc_final.ashx?la=en

And suggestion to delete _topicprefix in config.yaml also helped for me and mosquitto_sub -h localhost -d -t # command also help for me to test MQTT messages

Regars Andras

kenand1970 commented 5 years ago

DEar zwelor,

Sorry but I rexeived new error today

pi@raspberrypi:~ $ 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: inactive (dead) since Wed 2019-01-30 19:03:07 CET; 4s ago Docs: https://github.com/zewelor/bt-mqtt-gateway Process: 463 ExecStart=/home/pi/bt-mqtt-gateway/gateway.py (code=killed, signal=TERM) Main PID: 463 (code=killed, signal=TERM)

jan 30 19:00:12 raspberrypi gateway.py[463]: self.fill_cache() jan 30 19:00:12 raspberrypi gateway.py[463]: File "/usr/local/lib/python3.5/dist-packages/mithermometer/mithermometer_poller.py", line 67, jan 30 19:00:12 raspberrypi gateway.py[463]: with self._bt_interface.connect(self._mac) as connection: jan 30 19:00:12 raspberrypi gateway.py[463]: File "/usr/local/lib/python3.5/dist-packages/btlewrap/base.py", line 44, in enter jan 30 19:00:12 raspberrypi gateway.py[463]: self._backend.connect(self._mac) jan 30 19:00:12 raspberrypi gateway.py[463]: File "/usr/local/lib/python3.5/dist-packages/btlewrap/bluepy.py", line 32, in _func_wrapper jan 30 19:00:12 raspberrypi gateway.py[463]: raise BluetoothBackendException() from last_error jan 30 19:00:12 raspberrypi gateway.py[463]: btlewrap.base.BluetoothBackendException

After stop and start gatewy service it works again

pi@raspberrypi:~ $ sudo systemctl stop bt-mqtt-gateway pi@raspberrypi:~ $ sudo systemctl start bt-mqtt-gateway pi@raspberrypi:~ $ 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 Wed 2019-01-30 19:03:21 CET; 3s ago Docs: https://github.com/zewelor/bt-mqtt-gateway Main PID: 12398 (python3) CGroup: /system.slice/bt-mqtt-gateway.service └─12398 python3 /home/pi/bt-mqtt-gateway/gateway.py

jan 30 19:03:21 raspberrypi systemd[1]: Started Bluetooth MQTT gateway.

zewelor commented 5 years ago

After such error, you dont need to restart gateway. All errors should be catched, and on another interval time, gateway will make another read. I got usually few per day ( i got 7 devices ). You can just ignore it. Yesterday I've made small update and added "-q" option to skip printing that errors, you can add it in the sevice file to keep logs less verbose.

kenand1970 commented 5 years ago

Thank you

kenand1970 commented 5 years ago

Thank you. I will increase update_interval.