bderleta / vevor-ble-bridge

An attempt to make Home Assistant MQTT bridge for the Vevor Diesel Heater (and probably a lot of other ones)
GNU General Public License v3.0
11 stars 5 forks source link

Error when stopped via mqtt #10

Open lukyrys opened 1 month ago

lukyrys commented 1 month ago
vevor-ble-bridge_1  | 2024-10-15 10:31:57 +0000 [DEBUG] {'running_state': 1, 'error': 0, 'error_msg': 'No fault', 'running_step': 3, 'running_step_msg': 'Running', 'altitude': 219, 'running_mode': 1, 'set_level': 4, 'set_temperature': None, 'supply_voltage': 11.6, 'case_temperature': 234, 'cab_temperature': 38, 'md': 3}
vevor-ble-bridge_1  | 2024-10-15 10:31:59 +0000 [INFO] Received STOP command
vevor-ble-bridge_1  | 2024-10-15 10:31:59 +0000 [DEBUG] {'running_state': 0, 'error': 0, 'error_msg': 'No fault', 'running_step': 3, 'running_step_msg': 'Running', 'altitude': 220, 'running_mode': 1, 'set_level': 0, 'set_temperature': None, 'supply_voltage': 11.5, 'case_temperature': 234, 'cab_temperature': 38, 'md': 3}
vevor-ble-bridge_1  | Exception in thread Thread-1 (_thread_main):
vevor-ble-bridge_1  | Traceback (most recent call last):
vevor-ble-bridge_1  |   File "/usr/local/lib/python3.11/threading.py", line 1045, in _bootstrap_inner
vevor-ble-bridge_1  |     self.run()
vevor-ble-bridge_1  |   File "/usr/local/lib/python3.11/threading.py", line 982, in run
vevor-ble-bridge_1  |     self._target(*self._args, **self._kwargs)
vevor-ble-bridge_1  |   File "/usr/local/lib/python3.11/site-packages/paho/mqtt/client.py", line 3591, in _thread_main
vevor-ble-bridge_1  |     self.loop_forever(retry_first_connection=True)
vevor-ble-bridge_1  |   File "/usr/local/lib/python3.11/site-packages/paho/mqtt/client.py", line 1756, in loop_forever
vevor-ble-bridge_1  |     rc = self._loop(timeout)
vevor-ble-bridge_1  |          ^^^^^^^^^^^^^^^^^^^
vevor-ble-bridge_1  |   File "/usr/local/lib/python3.11/site-packages/paho/mqtt/client.py", line 1164, in _loop
vevor-ble-bridge_1  |     rc = self.loop_read()
vevor-ble-bridge_1  |          ^^^^^^^^^^^^^^^^
vevor-ble-bridge_1  |   File "/usr/local/lib/python3.11/site-packages/paho/mqtt/client.py", line 1556, in loop_read
vevor-ble-bridge_1  |     rc = self._packet_read()
vevor-ble-bridge_1  |          ^^^^^^^^^^^^^^^^^^^
vevor-ble-bridge_1  |   File "/usr/local/lib/python3.11/site-packages/paho/mqtt/client.py", line 2439, in _packet_read
vevor-ble-bridge_1  |     rc = self._packet_handle()
vevor-ble-bridge_1  |          ^^^^^^^^^^^^^^^^^^^^^
vevor-ble-bridge_1  |   File "/usr/local/lib/python3.11/site-packages/paho/mqtt/client.py", line 3033, in _packet_handle
vevor-ble-bridge_1  |     return self._handle_publish()
vevor-ble-bridge_1  |            ^^^^^^^^^^^^^^^^^^^^^^
vevor-ble-bridge_1  |   File "/usr/local/lib/python3.11/site-packages/paho/mqtt/client.py", line 3327, in _handle_publish
vevor-ble-bridge_1  |     self._handle_on_message(message)
vevor-ble-bridge_1  |   File "/usr/local/lib/python3.11/site-packages/paho/mqtt/client.py", line 3570, in _handle_on_message
vevor-ble-bridge_1  |     on_message(self, self._userdata, message)
vevor-ble-bridge_1  |   File "//./main.py", line 315, in on_message
vevor-ble-bridge_1  |     dispatch_result(vdh.stop())
vevor-ble-bridge_1  |                     ^^^^^^^^^^
vevor-ble-bridge_1  |   File "/vevor.py", line 176, in stop
vevor-ble-bridge_1  |     return self._send_command(3, 0, 85)
vevor-ble-bridge_1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
vevor-ble-bridge_1  |   File "/vevor.py", line 164, in _send_command
vevor-ble-bridge_1  |     if self.peripheral.waitForNotifications(1) and self._last_notification:
vevor-ble-bridge_1  |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
vevor-ble-bridge_1  |   File "/usr/local/lib/python3.11/site-packages/bluepy/btle.py", line 560, in waitForNotifications
vevor-ble-bridge_1  |     resp = self._getResp(['ntfy','ind'], timeout)
vevor-ble-bridge_1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
vevor-ble-bridge_1  |   File "/usr/local/lib/python3.11/site-packages/bluepy/btle.py", line 407, in _getResp
vevor-ble-bridge_1  |     resp = self._waitResp(wantType + ['ntfy', 'ind'], timeout)
vevor-ble-bridge_1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
vevor-ble-bridge_1  |   File "/usr/local/lib/python3.11/site-packages/bluepy/btle.py", line 375, in _waitResp
vevor-ble-bridge_1  |     raise BTLEInternalError("Unexpected response (%s)" % respType, resp)
vevor-ble-bridge_1  | bluepy.btle.BTLEInternalError: Unexpected response (wr)
bderleta commented 1 month ago

Seems like either a bug in bluepy, or some other problem, maybe the heater firmware is buggy and sends response twice? "wr" response is expected, but should be returned in response to characteristic.write:

https://github.com/bderleta/vevor-ble-bridge/blob/76ed9da87de15a94e9d10284f972db4999d59b4c/vevor.py#L161

Please try to reproduce the issue, maybe add print(response) in line 163 in that file to check what's going on.

lukyrys commented 1 month ago

i mean that this caused by low signal on bluetooth. Before i have bt dongle 5m away now i run bridge on rpi4 cca 50cm behind heater and i have no problem. In bridge script is missing some fail restore, maybe restart after fail or sth like this..

bderleta commented 3 weeks ago

If you used bundled docker-compose, docker should restart it automatically for you. Didn't it work or you expected something more elegant inside the script itself? I deliberately left such tricky cases to freely crash as proper BLE stack recovery would take too much work, but I encourage to PR your improvements, if you made some.

lukyrys commented 2 weeks ago

Sometimes script does not stop and gets stuck on backtrace and dont crash, so changing 'restart: always' in docker-compose.yaml won't work properly. In some debugging i added on vevor.py:164 try/catch with exit(0). There is no such problem on rpi but i have another problem with lost connection with mqtt (without any error but homeassistant has lost connection and script prints usual output with data from BLE) but BLE connection work further. This casue after few days work, maybe some problem with wifi signal. My ugly but functional solution for this is naming bridge service and adding subroutine 'vevor-restarter', which restarted docker each hour for sure.

version: '3.6'
services:
    vevor-ble-bridge:
        image: vevor-ble-bridge
        container_name: vevor_bridge
        network_mode: host
        restart: always
        cap_add:
            - NET_ADMIN
            - NET_RAW
        environment:
            BLE_MAC_ADDRESS: ${BLE_MAC_ADDRESS}
            BLE_PASSKEY: ${BLE_PASSKEY}
            DEVICE_NAME: ${DEVICE_NAME}
            DEVICE_MANUFACTURER: ${DEVICE_MANUFACTURER}
            DEVICE_MODEL: ${DEVICE_MODEL}
            MQTT_HOST: ${MQTT_HOST}
            MQTT_USERNAME: ${MQTT_USERNAME}
            MQTT_PASSWORD: ${MQTT_PASSWORD}
            MQTT_PREFIX: ${MQTT_PREFIX}
    vevor-restarter:
        image: docker
        container_name: vevor_bridge_restarter
        volumes: ["/var/run/docker.sock:/var/run/docker.sock"]
        command: ["/bin/sh", "-c", "while true; do sleep 3600; docker restart vevor_bridge; done"]
        restart: unless-stopped
lukyrys commented 2 weeks ago

Now on rpi a have same problem :D with docker-compose above and dont crash

obrazek