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
|Gewicht (kg)||Vet (%)||Spieren (%)||Botten (kg)||Water (%)||Verbruik (kCal)|
Feel free to ask for more clarification and meanwhile: enjoy coding!