Connecting the Medisana BS440 Bluetooth scale (part 4)

To start with: i am not a professional programmer, never was, never will be. Started off with some assembly on a 8 bit (yes!) SC/MP processor in my early days and ended with Turbo Pascal on MS-DOS6.2 as highest level of sophistication. Maybe that’s why i enjoy poking around on the credit-card size Raspberry in Python. The wealth of internet resources surely helps. This journey learned me about C-structs, callback routines, running shell commands, logging, parsing ini files, exception handling, pep8 and of course a little bluetooth. The complete code can be found  on my Github pages

The sequence of commands we want to issue through gatttool after being connected to the scale are basically these (when issued from the shell, pygatt will use the interactive mode of gatttool):

gatttool -t random -b F1:37:57:XX:XX:XX --char-write --handle 0x001f -n 0200
gatttool -t random -b F1:37:57:XX:XX:XX --char-write --handle 0x001c -n 0200
gatttool -t random -b F1:37:57:XX:XX:XX --char-write --handle 0x0026 -n 0200
gatttool -t random -b F1:37:57:XX:XX:XX --char-write-req --handle 0x0023 -n 0000000000 --listen

But the problem is we need to connect first and connecting is only possible for about one minute after the person has stepped onto the scale and we don’t know when that will be. The first attempt was just endless keep on trying to connect with 5 second timeouts. This is what it looked like:

adapter = pygatt.backends.GATTToolBackend()
adapter.start()
# wait for someone to step on the scale
while True:  
    try:
        device = adapter.connect('f1:37:57:xx:xx:xx', 5, 'random')
        break
    except pygatt.exceptions.NotConnectedError:
        print('Waiting...')

after which the program would continue with regestering to the indications and reading data… However every connection attempt/timeout would take more and more time every go, eventually slowing down to once a minute. Also CPU utilization would rise to 100% for some obscure reason.

The current approach uses the “hcitool lescan” command to scan for active bluetooth devices (filtered on our device of interest) before attempting to connect. This works better although the bluetooth driver needs a reset after every “hcitool lescan”/timeout by issuing the pygatt “adapter.reset()” command. Probably because “hcitool lescan” is not nicely terminated. There is some discussion about this behaviour of Pygatt at the Pygatt repo of Peplin. For now i just reset the interface after every timeout. Not nice but it works!

Awaiting the code to put data in a database and adding a nice web interface to it, last three measurements will be sent to the user by e-mail. The first step in tracking your weight!

 

21-03 07:4723-03 07:2323-03 23:30
76.375.976.9
17.717.417.7
36.736.936.7
2.92.92.9
64.564.764.5
258725792600

Datum
Gewicht (kg) Vet (%) Spieren (%) Botten (kg) Water (%) Verbruik (kCal)

Feel free to ask for more clarification and meanwhile: enjoy coding!

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

2 Responses to Connecting the Medisana BS440 Bluetooth scale (part 4)

  1. Hi, I’m trying to read data from a Beurer BF700 scale.

    Using gatttool I found about 30 readable properties (two of them are just “Beurer” and “Beurer BF700” strings) which always return the same values.

    Only 3 of them are writeable (the 0x07, 0x11, 0x14 ones); I don’t have an Android phone; I cannot yet try to sniff what values get actually written in them to be able to read actual scale data. I’m trying to write random values, but did not yet read anything new.

    — Please, add tags and links to your blog series: google only indexed your “part 1” page.

    Like

    • keptenkurk says:

      Jimmi,
      Thanks for spotting the lack of tags! Corrected it.
      The GATT protocol sends information through notifications or indications (the latter are will be acknowledged by the receiver). The receiver (=you) first needs to register to them. This post nicely describes how to do this in gatttool and how to find your way through the devices usefull characteristics.
      Knowing the steps to get your data in Gatttool this can be replicated in Pygatt just like that.
      /paul

      Like

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