devbis / ble2mqtt

Bluetooth to MQTT bridge, add your bluetooth-capable (including controllable) devices to your smart home
MIT License
125 stars 26 forks source link

BM2 Bluetooth battery logger #50

Closed roblomq closed 1 year ago

roblomq commented 1 year ago

For several years I use several bluetooth Car battery loggers. It would be very nice if these can also be implemented in this ble2mqtt because then I can add the battery devices to my Domoticz system and monitor them more active. The hard part is that I'm no programmer so I do not have the knowledge to implement this. Hoping somebody is willing to help me with this. There is already some related decoding info done, this can be found here.

devbis commented 1 year ago

I can add support but I need help on your end to test and report results

roblomq commented 1 year ago

That would be great! Of course I am willing to help. Just let me know what to do.

devbis commented 1 year ago

@roblomq can you please start with

pip3 install -U https://github.com/devbis/ble2mqtt/archive/refs/heads/add-bm2.zip --no-cache-dir --force-reinstall

and trying to run this version.

Add this chunk to your config to devices section and replace the MAC.

        {
            "address": "11:22:33:aa:bb:de",
            "type": "voltage_bm2"
        }

pip3 install pycryptodome maybe needed for cryptography if not installed.

Or, if you use docker, switch to add-bm2 branch and build the container.

roblomq commented 1 year ago

Did some testing today, the built seems to run ok

pi@P1-Logger:/home/ble2mqtt-add-bm2 $ docker build -t ble2mqtt:add-bm2 .
[+] Building 1886.8s (15/15) FINISHED
 => [internal] load .dockerignore                                                                                                                                                                    0.1s
 => => transferring context: 2B                                                                                                                                                                      0.0s
 => [internal] load build definition from Dockerfile                                                                                                                                                 0.1s
 => => transferring dockerfile: 871B                                                                                                                                                                 0.0s
 => [internal] load metadata for docker.io/library/python:3-slim                                                                                                                                     0.8s
 => [internal] load build context                                                                                                                                                                    0.1s
 => => transferring context: 4.00kB                                                                                                                                                                  0.1s
 => [app 1/5] FROM docker.io/library/python:3-slim@sha256:551c9529e77896518ac5693d7e98ee5e12051d625de450ac2a68da1eae15ec87                                                                          22.1s
 => => resolve docker.io/library/python:3-slim@sha256:551c9529e77896518ac5693d7e98ee5e12051d625de450ac2a68da1eae15ec87                                                                               0.1s
 => => sha256:551c9529e77896518ac5693d7e98ee5e12051d625de450ac2a68da1eae15ec87 1.65kB / 1.65kB                                                                                                       0.0s
 => => sha256:6b19ac75ce642650f8d176c1a22055eccf23930ea233f9d1da59b337b03fc0c5 1.37kB / 1.37kB                                                                                                       0.0s
 => => sha256:a67006f2980ba81890a0c8c08255fb0ce63841849c679c584bf03d2883d2f187 6.85kB / 6.85kB                                                                                                       0.0s
 => => sha256:fd66b5af2fde2bb5ebcf018c90e3ab76a3e7e61404fec824d9b9324679a1201f 11.23MB / 11.23MB                                                                                                     6.1s
 => => sha256:907205b8529d308141dcf426367aa7e8f07417b84a07ea3b3038cd51c66633ce 241B / 241B                                                                                                           0.4s
 => => sha256:379d71632777079b10cc8f38c4aab9021c538f4bb2674a0872890181813dcf8b 3.37MB / 3.37MB                                                                                                       2.0s
 => => extracting sha256:fd66b5af2fde2bb5ebcf018c90e3ab76a3e7e61404fec824d9b9324679a1201f                                                                                                            8.8s
 => => extracting sha256:907205b8529d308141dcf426367aa7e8f07417b84a07ea3b3038cd51c66633ce                                                                                                            0.0s
 => => extracting sha256:379d71632777079b10cc8f38c4aab9021c538f4bb2674a0872890181813dcf8b                                                                                                            4.3s
 => [app 2/5] RUN apt-get update &&     apt-get install bluez -y &&     apt-get clean                                                                                                              154.5s
 => [builder 2/6] RUN apt-get update &&     apt-get install gcc git -y &&     apt-get clean                                                                                                        192.5s
 => [builder 3/6] WORKDIR /usr/src/app                                                                                                                                                               0.5s
 => [builder 4/6] COPY . .                                                                                                                                                                           1.0s
 => [builder 5/6] RUN pip install --user --no-warn-script-location -r requirements.txt                                                                                                            1619.2s
 => [builder 6/6] RUN pip install --user --no-warn-script-location .                                                                                                                                21.1s
 => [app 3/5] COPY --from=builder /root/.local /root/.local                                                                                                                                          5.7s
 => [app 4/5] COPY ./docker_entrypoint.sh docker_entrypoint.sh                                                                                                                                       0.3s
 => [app 5/5] RUN chmod +x docker_entrypoint.sh                                                                                                                                                      1.9s
 => exporting to image                                                                                                                                                                              15.0s
 => => exporting layers                                                                                                                                                                             15.0s
 => => writing image sha256:3f94f3845ee9a312495c7ce97bcf7f90268b29961cc2fef5c678314ea4574982                                                                                                         0.0s
 => => naming to docker.io/library/ble2mqtt:add-bm2                                                                                                                                                  0.0s
WARNING: buildx: failed to read current commit information with git rev-parse --is-inside-work-tree

But when I start the container it crashes immediately and I get the following log.

Starting system message bus: dbus.

Starting system message bus: dbus.
Starting bluetooth: bluetoothd.
Traceback (most recent call last):
  File "/root/.local/bin/ble2mqtt", line 5, in <module>
    from ble2mqtt.__main__ import main
  File "/root/.local/lib/python3.11/site-packages/ble2mqtt/__main__.py", line 7, in <module>
    from ble2mqtt.ble2mqtt import Ble2Mqtt
  File "/root/.local/lib/python3.11/site-packages/ble2mqtt/ble2mqtt.py", line 14, in <module>
    from .devices.base import (BINARY_SENSOR_DOMAIN, CLIMATE_DOMAIN, COVER_DOMAIN,
  File "/root/.local/lib/python3.11/site-packages/ble2mqtt/devices/__init__.py", line 12, in <module>
    from .voltage_bm2 import BM2Voltage  # noqa: F401
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.local/lib/python3.11/site-packages/ble2mqtt/devices/voltage_bm2.py", line 33, in <module>
    class VoltageTesterBM2(SubscribeAndSetDataMixin, Sensor):
  File "/root/.local/lib/python3.11/site-packages/ble2mqtt/devices/base.py", line 103, in __new__
    newclass.ACTIVE_CONNECTION_MODE is not None
AssertionError: VoltageTesterBM2 requires ACTIVE_CONNECTION_MODE to be set
devbis commented 1 year ago

Yes, that was my fault. I updated the commit, can you please rerun it? Probably you need to delete local branch and refetch remote one, I did forced push. Will avoid force pushed for this branch in the future

roblomq commented 1 year ago

It works!!

2023-05-21 14:53:58 INFO: [Blind_AM43_Zonnescherm_Voorraam] send state={'linkquality': None, 'battery': 100, 'illuminance': 37.5, 'cover': {'state': 'closed', 'position': 100}}
2023-05-21 14:54:05 INFO: [BM2_Battery_Monitor_BM2_Losse_accu] send state=SensorState(voltage=12.42)
2023-05-21 14:55:05 INFO: [BM2_Battery_Monitor_BM2_Losse_accu] send state=SensorState(voltage=12.41)

Also Domoticz does automatically create a device through MQTT autodiscovery. 2023-05-21_17h11_48

The only thing is that my car is about 5m away and it doesn't pick up the battery monitor. I'm currently using a CSR4.0 do you think the range can be improved with another transceiver. If yes, do you have any advice for a good bluetooth receiver?

devbis commented 1 year ago

Great! I'll create some docs and make a release soon.

As for bluetooth receivers, I don't use them much. Probably a bigger antenna can help in your case or a dongle with big antenna. But I can't recommend any brand because I'm not aware of details.

devbis commented 1 year ago

Done in version 0.1.6