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!)
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:
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.