Connecting the Medisana BS440 Bluetooth scale (part 1)

About a year ago I bought the Medisana BS440 bathroom scale. It measures not only weight but also the percentage of muscle, fat and water in your body. And it has Bluetooth! Although it comes with an Android/iOS app to upload data to Medisana’s VitaDock cloud (tough luck with my Windows Phone) I couldn’t resist the challenge to get the data off the scale  to a Raspberry Pi and handle the data the way I like (like sending an email to my wife when… No! Better don’t!)

ScaleProject

The goal for this project

As an absolute rookie to the workings of Bluetooth and how to use it in programs, my first idea was that the scale would broadcast the data somehow and I would just need to standby and catch it when data would come along… That thought appeared to be a litte naive.

First we need to put up our Raspberry Pi and equip it with a Bletooth 4.0 and Wireless dongle so it can be placed in the vicinity of the scale without bothering about the cabling. Do the usual: Use W32DiskImager to put the latest Raspbian Jessie image onto your flashcard, do the raspiconfig, update, upgrade and put your WiFi credentials in the wpa_supplicant.conf file.

Ok, time to move on to the Bluetooth part. My Raspberry is equiped with a AC400 mini Bluetooth (BT) dongle. Can’t say anything about other types or models, this one just works fine. You might need to enable the BT dongle by

hciconfig hci0 up

after which you should be able to see if there are any BT devices around with:

hcitool lescan

For my project however I need to communicate with my BT scale. That’s where gatttool comes in. Gatttool is a commandline utility able to connect and communicate with BT devices. Gatttool is part of the BlueZ stack which we need to install and compile ourselves. No worry! Just enter the magical commands and grab a cup of coffee. It’ll take half an hour or so.

get a recent bluez version from http://www.bluez.org/ and change the version number accordingly. (Note: Enter commands that continue on the next line in one go) 
# wget https://www.kernel.org/pub/linux/bluetooth/bluez-5.4.tar.xz
extract
# tar xvf bluez-5.4.tar.xz

get the necessary libs
# apt-get install libusb-dev libdbus-1-dev libglib2.0-dev automake libudev-dev libical-dev libreadline-dev

configure and build SW:
# cd bluez-5.4
# ./configure --disable-systemd
# make
# make install

Then copy gatttool manually into the /usr/local/bin directory:

# cp attrib/gatttool /usr/local/bin/

Now you can start playing with gatttool. Gatttool supports two modes: the commandline mode and the interactive mode. This site gives a fair overvieuw of commands in the commandline mode, but we will be using the interactive mode which is entered by starting gatttool -I

A good question at this point would be: “How could we use a utility like gatttool in a programming language like Python?” Well, someone has found out and called it “pexpect“. Using pexpect we can launch a program from a Python script, feed it with keystrokes and evaluate the data comming back. After some evenings I have pexpect running more or less but I’m still unsure if I’m using pexpect the right way to have a solid piece of code.

Am I really the first to try this? Nah, I’m not that bright! Frankly, I stole the idea of using pexpect from Pygatt which I ignored earlier because I didn’t understand the beauty of it.  So it took me another few evenings to grasp what’s going on inside that module and to cite Christopher Peplin, the current maintainer, “In the end it appears to be easier than expected”

I’ll cover the installation and use of Pygatt in part 2 on this subject.

This entry was posted in BLE and tagged , , , , , , , . Bookmark the permalink.

10 Responses to Connecting the Medisana BS440 Bluetooth scale (part 1)

  1. Frank says:

    Hi, thanks for this great post. I am currently trying to achieve something similar by connecting my scale to Domoticz, an open source home automation system. Looking forward to your next post om this subject.

    Like

    • keptenkurk says:

      Frank,
      Stay tuned… finally – after stepping up and down the scale like a million times – i now have a stable setup.
      Code is available in my Github repo at https://github.com/keptenkurk/BS440
      But it was quite a journey to get there and will report about that in part 2 which i will write this Easter weekend.
      Thanks for your interest!

      Like

  2. Martin says:

    Hi, thank you for the detailed description of your great project! It’s very similar to what I also intend to realize somewhen: Connecting my scale (BS430) to FHEM home automation server for further processing and without sending data to other services. Do you think that the BS430 uses the same data format as the BS440?

    Like

    • keptenkurk says:

      Thanks for your comment Martin. I definately suspect that the BS430 and BS440 will behave equally. They are both supported by the VitaDock app and it makes no sense for them to behave differently on Bluetooth. And still, if they differ, you will have an enjoyable project to find out. Don’t hesitate to ask for help along the way.

      Like

  3. CM Rahman says:

    Looks good. I am trying to do the same but via oximeter, which constantly send data until it is being turned off.

    Liked by 1 person

  4. Pingback: Connecting the Medisana BS440 Bluetooth scale (part 1) | Projects by Keptenkurk – iphop・5g

  5. Mark says:

    Just stumbled across your posts, really helpful as I’m trying to integrate a personal project with these scales. I’m struggling though – do you *have* to have a user profile on the scales to get them to transmit the data? It seems very long winded if so? I thought they’d try and send after any reading was successful, but I can’t get that to happen….

    Like

    • keptenkurk says:

      Hi Mark, thanks for showing your interest.
      You will need to define profiles because nearly all values other than the weight (like fat, water, BMI, calories, bones) are calculated from the profile’s length, age, activity level, gender and the weight. So there’s no other measurement involved than weight.
      Storing data is done pretty clever. Your profile is selected automatically based on the weight seen (or if in doubt, the scale will present a choice between 2 profiles). When the profile in use is selected the last 30 datasets for this profile will be transmitted. By doing so you will only need to communicate to the scale once every 30 measurements to still get the full data.
      To get your profile to recognize you, you need to first select your profile and than step on the scale once. After this you can step on it right away.
      Paul

      Like

      • Mark says:

        Thanks for this, Paul. It’s as I expected really. I’ll be using the scales to measure multiple people, so selecting a profile first (there’ll be more than 8 users) isn’t really going to work. I wonder if I can force the profile to always be user 1 (I only ever want the weight reading, the app I writing will determine which user it’s for). Trial and error I guess, although it’s likely I’ll have to send these back and find another set that simply transmit the weight when it’s received, without any user management (if you know of any models that do this, please let me know!!)

        Thanks again,

        Mark

        Like

Leave a comment