
Getting data from our Xiaomi Smart Scale with Raspberry Pi
If you have the Xiaomi Smart Scale scale, follow the steps in this document and you will be able to read your weight from a Raspberry Pi, by connecting via bluetooth we will store the records in a MySQL database, and so if we want we can exploit them super easily with Grafana! I will leave you with a series of documents that I think are very interesting to obtain information from different devices that control our life and visualize it in a super cool way!
Database,
Therefore, we will need a Raspberry Pi with Raspbian installed and updated. Then, if we do not have a database server, we must install MySQL and the client optionally to manage the database, otherwise, very easily if we want to manage remote bases of this computer remotely we can do it from a Windows with HeidiSQL. GOOD, to install MySQL:
[SourceCode]sudo apt-get install mysql-server mysql-client[/SourceCode]
After installation, we can now connect to our MySQL, Create a Database, and create a Table where we will store the weighing records, Which is very simple, let's say it has 2 Fields, an initial one that will be where the weight is stored called 'peso’ How could you not, and another called 'created_at’ that will be filled with the date when the script is executed. We do this with:
[SourceCode]mysql -u root -p
create database NOMBRE_BASE_DATOS;
CREATE TABLE 'NOMBRE_TABLA' (
DECIMAL 'weight'(4,2) NOT NULL,
'created_at' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)
;
[/SourceCode]
And this is what we will have created, Yes? Our table where we will store all our weight! and with little time we will be able to analyze and see if we gain weight or not!!!
Bluetooth,
After preparing the DB, now we will configure the Bluetooth access of our Raspberry Pi to the Xiaomi Smart Scale, we'll install the requirements to use bluetooth with Python, Running:
[SourceCode]Sudo apt-get install bluez python-bluez python-mysqldb[/SourceCode]
Before you go any further, if we are not using a Raspberry Pi 3, We must connect a small Bluetooth dongle to be able to connect to the scale, to verify that we have it ready, we execute the following command that should return the MAC address of our bluetooth device if it has been recognized by the Pi:
[SourceCode]He did tool dev[/SourceCode]
We can try scanning and see what bluetooth devices we find with:
[SourceCode]HiTool Scan[/SourceCode]
Script,
We must create the following Python script that will be the one that connects to our scale, The script I put is a modification of este en GitHub. En mi caso lo llamaré lee_bascula.py:
[SourceCode]import blescan
import sys
import time
import bluetooth._bluetooth as bluez
dev_id = 0
sock = bluez.hci_open_dev(dev_id)
blescan.hci_le_set_scan_parameters(sock)
blescan.hci_enable_le_scan(sock)
measured_anterior = 0
try:
while True:
returnedList = blescan.parse_events(sock, 1)
If Len(returnedList) > 0:
(mac, uuid, major, minor, txpower, rssi) = returnedList[0].split(‘,’, 6)
# CAMBIAR LA DIRECCION MAC
if mac == ’88:0f:10:XX:XX:XX’ and uuid[0:22] == ‘01880f1096ab190d161d18′:
measunit = uuid[22:24]
measured = int((uuid[26:28] + uuid[24:26]), 16) * 0.01
unit = ”
if measunit.startswith((’03’, ‘b3’)): unit = ‘lbs’
if measunit.startswith((’12’, ‘b2’)): unit = ‘jin’
if measunit.startswith((’22’, ‘a2’)): unit = ‘Kg’ ; measured = measured / 2
if unit:
if measured != measured_anterior:
print("measured : %s %s" % (measured, unit))
measured_anterior = measured
import MySQLdb
db = MySQLdb.connect("127.0.0.1","root","CONTRASEÑA_ROOT_MYSQL","NOMBRE_BASE_DATOS")
cursor = db.cursor()
cursor.execute("""INSERT INTO NOMBRE_TABLA (peso) VALUES (%s) """,(measured))
db.commit()
time.sleep(2)
except KeyboardInterrupt:
sys.exit(1)[/SourceCode]
If we look closely at the script with making a couple of modifications you will have it running right away! You will need to change the MAC address of your scale, the MySQL root user's password, the name of the BD and little else! We can try to execute it with the following command that will listen for someone to weigh themselves, it will show each weigh-in every time you step on the scale and apart from telling you in a message the weight, it will put you in the MySQL DB that we are telling you. Try:
[SourceCode]sudo python /home/pi/lee_bascula.py[/SourceCode]
And now, If everything goes well, we must make that when the Raspberry Pi restarts it loads the script directly and we have it ready! We do it with cron and add:
[SourceCode]crontab -e
@reboot sudo python /home/pi/lee_bascula.py[/SourceCode]
Graphic,
If we remember, in this previous document, we saw how to assemble Grafana, If you have it unfolded, great since we are going to use it to visualize the data and make some incredible panels! And if you don't have it installed… I recommend you install it as we are going to use it, Follow the steps in the document to install it on a machine (virtual) dedicated or even on the Raspberry Pi itself.
After having Grafana mounted, the usual thing we should do is create a new Dashboard, in this simple example we will make a graphical one that will query the MySQL DB to show us the records we have stored, Then everyone will customize the chart to their liking. But first of all, we must create a MySQL data source to make queries from our Dashboards. To do this,, since “Data Sources” We will click on “Add” and we add a connection to our MySQL server which is the Raspberry, set up the connection correctly and record with “Save & Test”,
Now we can create our Dashboard, we added a Graph Panel, and edit it by clicking on its title, We change everything as we see fit, colors, information, Axes… but in the “Metrics” we must enter the SELECT that will make the query to the DB, We specify something like:
[SourceCode]SELECT
UNIX_TIMESTAMP(created_at) So time_sec,
weight as value,
"Weight" as metric
FROM tilt
WHERE $__timeFilter(created_at)
ORDER BY created_at, time_sec ASC[/SourceCode]
And nothing, After fiddling around you'll see that you can make some cool graphs, yes indeed, You need to fill in the table with records, So give time to time!!! This post, as I say, is a first step towards a marvel that we are going to be able to do, putting all the pieces of the puzzle together… See you soon, Take care of yourselves!!