python wrapper for gatttool interaction with a Xiaomi Mi Scale (Version 1)
BLE reverse engineering from openScale
gatttool
To grant non-root access to BLE, assuming you are using a user named miscale
on debian/ubuntu:
edit /etc/dbus-1/system.d/bluetooth.conf
<policy user="miscale">
<allow own="org.bluez"/>
<allow send_destination="org.bluez"/>
<allow send_interface="org.bluez.GattCharacteristic1"/>
<allow send_interface="org.bluez.GattDescriptor1"/>
<allow send_interface="org.freedesktop.DBus.ObjectManager"/>
<allow send_interface="org.freedesktop.DBus.Properties"/>
</policy>
Then restart the dbus systemctl restart dbus
From command line:
miscale.py -m <MAC Address> [OPTION]
Requires at least one option with a mac address
Options
-l, --last-weight Fetches the last weight measurment performed by the scale.
-q, --weight-queue Checks the weight history queue of the scale.
>> -N, --keep-weight-queue Sub-option: Checks the weight history queue of the scale without clearing the queue.
-t, --check-datetime Checks scale DateTime against system Local DateTime.
-u, --update-datetime Checks scale DateTime against system Local DateTime and updates if needed.
>> -F, --force-update-datetime Sub-option: Updates scale DateTime against system Local DateTime.
Note: It is recomended to always use the update-datetime flag as the miscale clock is fairly inaccurate
$: miscale.py -m XX:XX:XX:XX:XX:XX -l -u
01/01/2017 06:11:01 75.1 kg
$: miscale.py -m XX:XX:XX:XX:XX:XX -q -u
2 Unread measurements
01/01/2017 06:11:01 75.3 kg
02/01/2017 06:12:25 75.1 kg
$: miscale.py -m XX:XX:XX:XX:XX:XX -q -N -u
2 Unread measurements
01/01/2017 06:11:01 75.3 kg
02/01/2017 06:12:25 75.1 kg
- datetime_update( mac_address( Required ) )
- check_time( mac_address( Required ) )
- Returns: python datetime object
- initialize( mac_address( Required ) )
- format_weight( list_of_lists_hex_data( Required ), datetime_format( Optional: %d/%m/%Y %H:%M:%S ) )
- Returns: List of lists
[ [ timestamp( str ), Weight( str ), Unit( str ) ] ]
- Returns: List of lists
- read_weight_history( mac_address( Required ) )
- Returns: List of lists (hex values)
[ ['40', '00', '00', 'b2', '08', '01', '01', '00', '00', '00'], ['62', 'b8', '3d', '00', '00', '00', '00', '00', '00', '00'] ]
- Returns: List of lists (hex values)
- read_weight_queue( mac_address( Required ), keep_weight_queue_flag( Optional: True/False ) )
- if queue empty Returns: 'No records'
- Returns: List of lists (hex values)
[ ['62', 'b8', '3d', '00', '00', '00', '00', '00', '00', '00'] ]
import miscale
mac_address="XX:XX:XX:XX:XX:XX"
# Update The scale Clock
miscale.datetime_update( mac_address )
# Activate Weight History and Notifications flags
miscale.initialize( mac_address )
# Read the weight queue
data = miscale.read_weight_queue( mac_address,keep_queue )
# Decode the hex date into usable data
records = miscale.format_weight( data )
if records != "No records":
for i in range( len( records ) ):
print( "DateTime: " + records[i][0] )
print( "Weight: " + records[i][1] )
print( "Unit: " + records[i][2] )
Bitcoin | Litecoin |
---|---|
Bitcoin Address: 155xGQ2sPt2BAbmLUp5myyfjLmAm3c8pPP | Litecoin Address: LNiBt4ksgDgTqGq4BYcLS8vtNkdn1Lrmmh |
Website: PeekRead.Info