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.

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

6 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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s