Use case: Connecting a bathroom body (fat) scale (e.g. Medisana BS440) via Bluetooth / MQTT to openHAB - Step by step instruction

Funny coincidence: While I was trying to fix exactly this problem (data transfer not working anymore probably because the service died somehow) I read your message.

Did you test the addition to the service-file? If yes I’ll issue a Pull Request on Github.

openhabian@openhabian:~/BS440-HA-AD $ ps -ef | grep python
root     30979     1  0 19:23 ?        00:00:22 /usr/bin/python BS440.py
openhab+ 31857 29720  0 21:32 pts/5    00:00:00 grep --color=auto python
openhabian@openhabian:~/BS440-HA-AD $ sudo kill 30979
[sudo] password for openhabian:
openhabian@openhabian:~/BS440-HA-AD $ ps -ef | grep python
openhab+ 32069 29720  0 21:33 pts/5    00:00:00 grep --color=auto python

[wait > 15 s]

openhabian@openhabian:~/BS440-HA-AD $ ps -ef | grep python
root     32123     1  5 21:33 ?        00:00:00 /usr/bin/python BS440.py
openhab+ 32174 29720  0 21:33 pts/5    00:00:00 grep --color=auto python

Journal:

Jan 29 20:46:38 openhabian python[30979]: 2023-01-29 20:46:38,499 INFO     processIndication {'valid': True, 'timestamp': 1675021598, 'person': 255, 'kcal': 0, 'fat': 0.0, 'tbw': 0.0, 'muscle': 0.0, 'bone': 0.0}
Jan 29 20:47:04 openhabian python[30979]: 2023-01-29 20:47:04,804 INFO     <module> Could not disconnect...
Jan 29 20:47:04 openhabian python[30979]: 2023-01-29 20:47:04,804 INFO     <module> Done receiving data from scale
Jan 29 21:33:22 openhabian systemd[1]: bs440.service: Succeeded.
Jan 29 21:33:22 openhabian systemd[1]: bs440.service: Consumed 35.167s CPU time.
Jan 29 21:33:38 openhabian systemd[1]: bs440.service: Scheduled restart job, restart counter is at 1.
Jan 29 21:33:38 openhabian systemd[1]: Stopped Run BS440 BT scale monitor.
Jan 29 21:33:38 openhabian systemd[1]: bs440.service: Consumed 35.167s CPU time.
Jan 29 21:33:38 openhabian systemd[1]: Started Run BS440 BT scale monitor.
Jan 29 21:33:38 openhabian python[32123]: 2023-01-29 21:33:38,191 INFO     <module> Configured plugins: BS440mqtt
Jan 29 21:33:38 openhabian python[32123]: 2023-01-29 21:33:38,192 INFO     <module> Loading plugin: BS440mqtt
Jan 29 21:33:38 openhabian python[32123]: 2023-01-29 21:33:38,514 INFO     <module> All plugins loaded.
Jan 29 21:33:38 openhabian python[32123]: 2023-01-29 21:33:38,514 INFO     <module> BS440 Started
Jan 29 21:33:38 openhabian sudo[32125]:     root : PWD=/home/openhabian/BS440-HA-AD ; USER=root ; COMMAND=/usr/bin/btmgmt le on
Jan 29 21:33:38 openhabian sudo[32125]: pam_unix(sudo:session): session opened for user root(uid=0) by (uid=0)
Jan 29 21:33:38 openhabian sudo[32125]: pam_unix(sudo:session): session closed for user root
Jan 29 21:33:38 openhabian python[32123]: 2023-01-29 21:33:38,547 INFO     init_ble_mode b''
Jan 29 21:33:38 openhabian sudo[32127]:     root : PWD=/home/openhabian/BS440-HA-AD ; USER=root ; COMMAND=/usr/bin/systemctl restart bluetooth
Jan 29 21:33:38 openhabian sudo[32127]: pam_unix(sudo:session): session opened for user root(uid=0) by (uid=0)
Jan 29 21:33:38 openhabian sudo[32127]: pam_unix(sudo:session): session closed for user root
Jan 29 21:33:38 openhabian sudo[32133]:     root : PWD=/home/openhabian/BS440-HA-AD ; USER=root ; COMMAND=/usr/bin/hciconfig hci0 reset
Jan 29 21:33:38 openhabian sudo[32133]: pam_unix(sudo:session): session opened for user root(uid=0) by (uid=0)
Jan 29 21:33:38 openhabian sudo[32133]: pam_unix(sudo:session): session closed for user root
Jan 29 21:35:11 openhabian python[32123]: 2023-01-29 21:35:11,551 INFO     <module> Continue Comms: True
Jan 29 21:35:11 openhabian python[32123]: 2023-01-29 21:35:11,827 INFO     <module> Waiting for notifications for another 30 seconds
Jan 29 21:35:11 openhabian python[32123]: 2023-01-29 21:35:11,829 INFO     processIndication {'valid': True, 'person': 255, 'gender': 'male', 'age': 0, 'size': 0, 'activity': 'normal'}

Looks OK to me.

Thx. Added a PR.

Since my service probably died some days ago, it’s a good test if this keeps the service running.

Question would be, though, what makes the service die / whether a root-cause fixed would be possible.

@Ap15e, is the service running smooth on your end?

For me the service seems to stop at some point in time, and even the Restart=always doesn’t seem to bring it up again. Last thing I see is:

pi@raspberrypi:~ $ journalctl -l -f -u bs440
-- Journal begins at Thu 2022-09-22 02:23:40 CEST. --
Jan 31 12:51:03 raspberrypi sudo[29421]: pam_unix(sudo:session): session opened for user root(uid=0) by (uid=0)
Jan 31 12:51:03 raspberrypi sudo[29421]: pam_unix(sudo:session): session closed for user root
Jan 31 14:51:05 raspberrypi sudo[5545]:     root : PWD=/home/pi/BS440-HA-AD ; USER=root ; COMMAND=/usr/bin/systemctl restart bluetooth
Jan 31 14:51:05 raspberrypi sudo[5545]: pam_unix(sudo:session): session opened for user root(uid=0) by (uid=0)
Jan 31 14:51:11 raspberrypi sudo[5545]: pam_unix(sudo:session): session closed for user root
Jan 31 14:51:11 raspberrypi sudo[5568]:     root : PWD=/home/pi/BS440-HA-AD ; USER=root ; COMMAND=/usr/bin/hciconfig hci0 reset
Jan 31 14:51:11 raspberrypi sudo[5568]: pam_unix(sudo:session): session opened for user root(uid=0) by (uid=0)
Jan 31 14:51:11 raspberrypi sudo[5568]: pam_unix(sudo:session): session closed for user root
Jan 31 16:50:46 raspberrypi python[2150]: 2023-01-31 16:50:46,512 WARNING  <module> Error getting handles
Jan 31 16:50:46 raspberrypi python[2150]: 2023-01-31 16:50:46,517 INFO     <module> Continue Comms: False

How do things look like on your end?

+1 - but due to journal rotation I have no clue what went wrong. I’ll monitor the service more closely and report back.

1 Like

@anon71759204, I dug a bit deeper and opened an issue on Github: Service stops randomly at some point with "Error getting handles" "Continue Comms: False" · Issue #109 · keptenkurk/BS440 · GitHub. Feel free to have a look at what I noted.

The service has been up and running since my last post.

… still up and running.

Very odd. You did not change anything, did you?

My final assessment:
I did not change anything - still up and running.

Thanks a lot ! to @Cplant for posting this solution. I had a withings scale before and struggled w/o success to somehow integrate it’s measurements into my system. It works nicely now with a medisana BS444 scale and the scripts advertised here.

A few points though:

  • keptnkurk’s script is dated and abandoned. Everything is aimed at python2. While there have been some efforts to make it run on python3 you may run into version issues with plugins.
  • the instructions at the top here are sort of vague about python2/python3, too.
  • running everything with python3 I see ‘service seems to stop’-issue roughly every 90 mins. Feels like a memory leak…
    • after the service effectively stopped systemctl status still reports it as active and up thus any restart option in the service declaration seems unlikely to help much
    • restarting the .service with a cron job hourly seems to keep things going for me
  • I wonder whether there are extensions/variants of this script which handle other medisana health care products with ble as I also bought a medisana bu546 blood pressure manometer and hope to integrate it as well. Thanks in advance for any pointers!

For some reasons what should have been the easy path, extending the python script to listen on Blood Pressure Measurements, ended in failure for me - but doing the analogon as an Arduino sketch for an ESP32 mcu worked much better.
The ESP32 whith this sketch obtains the measurement data from the ble - equipped device and publishes them to MQTT. Unlike the python script this should work for every standard conform BPM device, (untested)
Just in case someone gets directed here by a search:
the sketch (plus some auxilliary files) can be found at ble_client.ino

True. Though (apart from the freezes) everything seems to be pretty smooth (at least for me).

Can you describe how you do this? I’d like to test this myself and then include it into the instruction above.

I had some free time in January and took this script as a trigger to learn some python. So I basically went through the entire code line by line (and included the comments which you can find in the most recent version on github). What I learned from that exercise: Including new devices can be really easy if they are similar to the scales that are covered already. So I’d encourage you to have a look at the code yourself, maybe it’s not that bad. Even for me (not having seen python code before) I could understand most of what’s going on in the code.

Since I set up the script to run as a service with systemd (and did a little renaming) the command to restart it reads
systemctl restart medisa-BS444
on my system.

There are plenty of online ressources to teach you how to add jobs to cron under linux and I recommend to go there and read it.
The way I actually did it is not necessarily good advice for a Howto… as I used webmin System/Scheduled Cron Jobs.
I like the gui of that tool for a few functions but it is a big install to only create 1 line in a crontab.

In the end, /var/spool/cron/crontabs/root needs to contain a line
@hourly systemctl restart medisa-BS444

I did and tried ways for a while but among other things there were practical issues. BLE works for a limited range only and the 2 devices (scale and BPM) are several meters and 3 walls apart.
So a scnd device was need anyways and dedicating another raspi to BLE scanning seemed a bit of overkill to me.
And then, as I wrote above, writing an Arduino sketch for one of the ESP32 mcu I had lying around turned out to be surprisingly straight forward.
Right now I have both BLE monitors running in parallel, both mostly up to it and at times failling for reasons I’m not sure about. As both of them report to mqtt the diversity doesn’t hurt but considering the hardware costs I still fav the ESP32 sketch and consider to write a Body Scale variant of it.

1 Like

There may be further issues with plugins. MQTT works nicely but the csv-plugin did not work for me.
Not much of an issue for me as with 2 services I needed a csv-generator as an mqtt-client with subscriptions on both streams.

Very very odd: The service kept crashing reliably, but now (had to re-install my system) running since 24 hrs already, which it never did before. Will continue to investigate.

I was about to try out what was reported here and eventually issue a PR, but, as said, it doesn’t crash anymore. :wink:

With a Raspi Zero W it does not work, the WLAN and the BT-LE connection to the scale do not want to work at the same time.
If you switch WLAN off, the BT connection works, if WLAN is on you get an error “function not implemented (38)” in gatttool.
I have solved this quick & dirty for myself by switching off the WLAN before the “main loop” (ifdown wlan0) and switching it on before calling the MQTT plugin and then switching it off again with a 30 sec delay.

Thanks for the great instructions

Thanks. Added your comments in the instruction above.

Hello everyone,

Since I also have a Medisana body scale (BS430), I tried to set up a RaspberryPi following the instructions. Since I still had an old Raspberry Pi 1 at home, I tried it with this one. I gave the Pi a USB WiFi adapter and a USB Bluetooth 4.0 adapter. I then flashed a current Raspberry Pi OS 12 Lite 32-bit onto an SD card and used it to boot the Pi. The Pi recognized both the WiFi adapter and the Bluetooth adapter. So everything is ok up to this point.

In point 1.3 (Test the connectivity) I had to add the -t random parameter to the gattool command, otherwise I could not establish a connection to my scale. After that the connection worked.

In point 1.8 (Install Pygatt) I received the error message “externally-managed-environment” after executing the command sudo pip install “Pygatt[GATTTOOL]”. To fix this, I had to delete the EXTERNALLY-MANAGED file under /usr/lib/python3.11. The pip packages could then be installed.

But now I’m stuck on executing the Pythen script BS440.py. When I start the script interactively I get the following output:

root@bodyscalepi01:/opt/BS440# python BS440.py 
2024-03-29 14:18:25,774 INFO     <module> Configured plugins: BS440mqtt
2024-03-29 14:18:25,782 INFO     <module> Loading plugin: BS440mqtt
2024-03-29 14:18:27,415 INFO     <module> All plugins loaded.
2024-03-29 14:18:27,421 INFO     <module> BS440 Started
2024-03-29 14:18:27,604 INFO     init_ble_mode b'hci0 Set Low Energy complete, settings: powered ssp br/edr le secure-conn \n'

Nothing happens after that. When I then activate the scale, nothing is detected.

If I call journalctl -f in parallel in a second console window, I get the following log entries:

Mar 29 14:18:27 bodyscalepi01 sudo[2047]:     root : TTY=pts/0 ; PWD=/opt/BS440 ; USER=root ; COMMAND=/usr/bin/btmgmt le on
Mar 29 14:18:27 bodyscalepi01 sudo[2047]: pam_unix(sudo:session): session opened for user root(uid=0) by pi(uid=0)
Mar 29 14:18:27 bodyscalepi01 sudo[2047]: pam_unix(sudo:session): session closed for user root
Mar 29 14:18:27 bodyscalepi01 sudo[2050]:     root : TTY=pts/0 ; PWD=/opt/BS440 ; USER=root ; COMMAND=/usr/bin/systemctl restart bluetooth
Mar 29 14:18:27 bodyscalepi01 sudo[2050]: pam_unix(sudo:session): session opened for user root(uid=0) by pi(uid=0)
Mar 29 14:18:28 bodyscalepi01 systemd[1]: Stopping bluetooth.service - Bluetooth service...
Mar 29 14:18:28 bodyscalepi01 bluetoothd[2012]: Terminating
Mar 29 14:18:28 bodyscalepi01 bluetoothd[2012]: Stopping SDP server
Mar 29 14:18:28 bodyscalepi01 bluetoothd[2012]: Exit
Mar 29 14:18:28 bodyscalepi01 systemd[1]: bluetooth.service: Deactivated successfully.
Mar 29 14:18:28 bodyscalepi01 systemd[1]: Stopped bluetooth.service - Bluetooth service.
Mar 29 14:18:28 bodyscalepi01 systemd[1]: bthelper@hci0.service: Deactivated successfully.
Mar 29 14:18:28 bodyscalepi01 systemd[1]: Stopped bthelper@hci0.service - Raspberry Pi bluetooth helper.
Mar 29 14:18:28 bodyscalepi01 systemd[1]: Stopping bthelper@hci0.service - Raspberry Pi bluetooth helper...
Mar 29 14:18:28 bodyscalepi01 systemd[1]: hciuart.service - Configure Bluetooth Modems connected by UART was skipped because of an unmet condition check (ConditionFileNotEmpty=/proc/device-tree/soc/gpio@7e200000/bt_pins/brcm,pins).
Mar 29 14:18:28 bodyscalepi01 systemd[1]: Starting bthelper@hci0.service - Raspberry Pi bluetooth helper...
Mar 29 14:18:28 bodyscalepi01 bthelper[2056]: Not a UART-attached BT Modem
Mar 29 14:18:28 bodyscalepi01 systemd[1]: Finished bthelper@hci0.service - Raspberry Pi bluetooth helper.
Mar 29 14:18:28 bodyscalepi01 (uetoothd)[2060]: ConfigurationDirectory 'bluetooth' already exists but the mode is different. (File system: 755 ConfigurationDirectoryMode: 555)
Mar 29 14:18:28 bodyscalepi01 systemd[1]: Starting bluetooth.service - Bluetooth service...
Mar 29 14:18:29 bodyscalepi01 bluetoothd[2060]: Bluetooth daemon 5.66
Mar 29 14:18:29 bodyscalepi01 systemd[1]: Started bluetooth.service - Bluetooth service.
Mar 29 14:18:29 bodyscalepi01 sudo[2050]: pam_unix(sudo:session): session closed for user root
Mar 29 14:18:29 bodyscalepi01 bluetoothd[2060]: Starting SDP server
Mar 29 14:18:29 bodyscalepi01 bluetoothd[2060]: profiles/audio/vcp.c:vcp_init() D-Bus experimental not enabled
Mar 29 14:18:29 bodyscalepi01 bluetoothd[2060]: src/plugin.c:plugin_init() Failed to init vcp plugin
Mar 29 14:18:29 bodyscalepi01 bluetoothd[2060]: profiles/audio/mcp.c:mcp_init() D-Bus experimental not enabled
Mar 29 14:18:29 bodyscalepi01 bluetoothd[2060]: src/plugin.c:plugin_init() Failed to init mcp plugin
Mar 29 14:18:29 bodyscalepi01 bluetoothd[2060]: profiles/audio/bap.c:bap_init() D-Bus experimental not enabled
Mar 29 14:18:29 bodyscalepi01 bluetoothd[2060]: src/plugin.c:plugin_init() Failed to init bap plugin
Mar 29 14:18:30 bodyscalepi01 dbus-daemon[326]: [system] Activating via systemd: service name='org.freedesktop.hostname1' unit='dbus-org.freedesktop.hostname1.service' requested by ':1.41' (uid=0 pid=2060 comm="/usr/libexec/bluetooth/bluetoothd")
Mar 29 14:18:30 bodyscalepi01 sudo[2061]:     root : TTY=pts/0 ; PWD=/opt/BS440 ; USER=root ; COMMAND=/usr/bin/hciconfig hci0 reset
Mar 29 14:18:30 bodyscalepi01 bluetoothd[2060]: Bluetooth management interface 1.22 initialized
Mar 29 14:18:30 bodyscalepi01 sudo[2061]: pam_unix(sudo:session): session opened for user root(uid=0) by pi(uid=0)
Mar 29 14:18:30 bodyscalepi01 bluetoothd[2060]: profiles/sap/server.c:sap_server_register() Sap driver initialization failed.
Mar 29 14:18:30 bodyscalepi01 bluetoothd[2060]: sap-server: Operation not permitted (1)
Mar 29 14:18:30 bodyscalepi01 systemd[1]: Starting systemd-hostnamed.service - Hostname Service...
Mar 29 14:18:30 bodyscalepi01 sudo[2061]: pam_unix(sudo:session): session closed for user root
Mar 29 14:18:30 bodyscalepi01 bluetoothd[2060]: Failed to set privacy: Rejected (0x0b)
Mar 29 14:18:32 bodyscalepi01 dbus-daemon[326]: [system] Successfully activated service 'org.freedesktop.hostname1'
Mar 29 14:18:32 bodyscalepi01 systemd[1]: Started systemd-hostnamed.service - Hostname Service.
Mar 29 14:19:02 bodyscalepi01 systemd[1]: systemd-hostnamed.service: Deactivated successfully.
Mar 29 14:19:02 bodyscalepi01 systemd[1]: systemd-hostnamed.service: Consumed 1.357s CPU time.

Unfortunately I’m stuck at this point and hope someone can give me a tip.

Thank you in advance for your help.
Christian

In case no one can help consider opening an issue here.