Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Mi Body Composition Scale 2 (Model # XMTZC05HM) #4

Closed
pplucky opened this issue Oct 21, 2019 · 32 comments
Closed

Support for Mi Body Composition Scale 2 (Model # XMTZC05HM) #4

pplucky opened this issue Oct 21, 2019 · 32 comments

Comments

@pplucky
Copy link

pplucky commented Oct 21, 2019

My Mi Body Composition Scale show in lescan as MIBFS and it is shown as 'Mi Body Composition Scale 2' (Model # XMTZC05HM).

When I ran wrapper.sh with sudo, I got the following error:
Traceback (most recent call last):
File "/home/pi/xiaomi_mi_scale/Xiaomi_Scale.py", line 183, in
main()
File "/home/pi/xiaomi_mi_scale/Xiaomi_Scale.py", line 180, in main
devices = scanner.scan(5)
File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 853, in scan
self.process(timeout)
File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 842, in process
self.delegate.handleDiscovery(dev, (dev.updateCount <= 1), isNewData)
File "/home/pi/xiaomi_mi_scale/Xiaomi_Scale.py", line 113, in handleDiscovery
self._publish(round(measured, 2), unit, str(mitdatetime), miimpedance)
File "/home/pi/xiaomi_mi_scale/Xiaomi_Scale.py", line 153, in _publish
lib = Xiaomi_Scale_Body_Metrics.bodyMetrics(weight, height, age, sex, 0)
File "/home/pi/xiaomi_mi_scale/Xiaomi_Scale_Body_Metrics.py", line 17, in init
raise Exception("Weight is either too low or too high (limits: <10kg and >200kg)")
Exception: Weight is either too low or too high (limits: <10kg and >200kg)

If anyone is willing to guide me on testing it properly to add support to it, it would be great!

Thanks in advance.

@lolouk44
Copy link
Owner

I take it you managed to get your MAC address ok following the instructions?
Are you running the dockerized version? What OS/Device are you running it on?

@pplucky
Copy link
Author

pplucky commented Oct 22, 2019

Yes, I could get MAC address as described.

Running wrapper.sh (no docker), in a Raspberry Pi4 running Raspbian Buster properly updated (Linux fw 4.19.66-v7l+), python_version 3.7.3.

With my Python limited and basic skills, I tried printing the variables height weight and age before the error message and weight was like 0.3 (maybe weight is coming at a different offset?). Age and Height looked consistent with Mi Fit data.

Let me know how can I help more.

Thanks in advance for your help

@lolouk44
Copy link
Owner

It's a different scale so the message is probably different.
I'm not home at the moment so can't remember how I originally did it.
When I get the chance I'll dive in again and try to retrieve the command to get the original message.
then I'll ask you for that message and what readings the Mi Fit app reads.

@pplucky
Copy link
Author

pplucky commented Oct 22, 2019

OK, deal.

That "dive" info may also be helpful for me to try to do myself a similar job for another Xiaomi BT device I have here (currently outside Home Assistant, only in Mi Home).

@lolouk44
Copy link
Owner

sorry not had the chance to do this yet. Should have time tomorrow if you're around

@pplucky
Copy link
Author

pplucky commented Oct 24, 2019

Yeah, no problem.

Let me know when you can.

@ghost
Copy link

ghost commented Oct 24, 2019

Hi, I have also problem with connection so if you let me join to your discussion then i will be very pleased. After let say 3pm I will add here my errors in code and some other uncertainty :)

@lolouk44
Copy link
Owner

lolouk44 commented Oct 25, 2019

Hi @pplucky @patrickf10
I've uploaded test_xiaomi_scale.py
Can you let me know what you see?

sudo python3 test.py 
Scanning for devices...
    Device: XX:XX:XX:XX:XX:XX (random), -69 dBm . 
data:
06

data:
1b18

data:
5701d4abc8b94940

data:
1b1802a4b2070103051428fdffe43e
measured:
80.5
unit:
kg


data:
MIBCS

I'm mostly interested in the longest data string (in my example 1b1802a4b2070103051428fdffe43e) as well as your weight and unit from the actual scale / Mi App please

@lolouk44
Copy link
Owner

I'm on discord now (lolouk44#7180)

@ghost
Copy link

ghost commented Oct 25, 2019

I sent you a invitation on discord as Drochu#3314

@pplucky
Copy link
Author

pplucky commented Oct 25, 2019 via email

@pplucky
Copy link
Author

pplucky commented Oct 25, 2019

Hi @pplucky @patrickf10
I've uploaded test_xiaomi_scale.py
Can you let me know what you see?

sudo python3 test.py 
Scanning for devices...
    Device: XX:XX:XX:XX:XX:XX (random), -69 dBm . 
data:
06

data:
1b18

data:
5701d4abc8b94940

data:
1b1802a4b2070103051428fdffe43e
measured:
80.5
unit:
kg


data:
MIBCS

I'm mostly interested in the longest data string (in my example 1b1802a4b2070103051428fdffe43e) as well as your weight and unit from the actual scale / Mi App please

Device: c8:b2:1e:95:0c:01 (public), -52 dBm . 
data:
06

data:
0000181b-0000-1000-8000-00805f9b34fb

data:
1b180284e3070a19152e1200002800
measured:
0.2
unit:
kg


data:
MIBFS

data:
5701c8b21e950c01

What appears as measure is the weight showing on the scale while running the python, what is in Mi Fit is 63,25 and unit is KG (for the first user, there is a second one in Mi Fit, with 48,85 and unit is also KG).

What else do you need?

@lolouk44
Copy link
Owner

The data appears to be quite different than mine,
Can you do me a favour and do a few weighings?
I'd need a few examples with different weight (carry something in your hands)
I'd need a few examples with/without the body health measurements (so step bare foot and with socks/warm shoes)
I've so far figured out where the scale's date is, but that's about it :(

@pplucky
Copy link
Author

pplucky commented Oct 28, 2019

The data appears to be quite different than mine,
Can you do me a favour and do a few weighings?
I'd need a few examples with different weight (carry something in your hands)
I'd need a few examples with/without the body health measurements (so step bare foot and with socks/warm shoes)
I've so far figured out where the scale's date is, but that's about it :(

Here it is, using the latest version of test script:

Weight 63,25kg (with socks)
data:
1b1802a4e3070a1c162e3bfdff6a31

Weight 64,90kg (with socks)
data:
1b180624e3070a1c1635240000cc01

Weight 63,10kg (bare feet)
data:
1b1802a6e3070a1c163131f9014c31

Weight 64,80kg (bare feet)
data:
1b180284e3070a1c16331e00004001

@lolouk44
Copy link
Owner

Thanks. The data looks interesting and may I say promising.
It looks as though there may be more than one set of data being sent but we only captured one.
OR a 2nd reading sends some sort of incremental data. Still trying to figure this one out

That being set aside for now, when I get the data I expect, I get the right readings:
Book1.xlsx

Can you try and run the script more than once while you're being weighed to see if the data changes?

@pplucky
Copy link
Author

pplucky commented Oct 30, 2019

Thanks. The data looks interesting and may I say promising.
It looks as though there may be more than one set of data being sent but we only captured one.
OR a 2nd reading sends some sort of incremental data. Still trying to figure this one out

That being set aside for now, when I get the data I expect, I get the right readings:
Book1.xlsx

Can you try and run the script more than once while you're being weighed to see if the data changes?

All tests done with socks:

data:
1b180204e3070a1e163b0d00005230

data:
1b180204e3070a1e163b1c00004830

data:
1b180204e3070a1e163b270000e42f

data:
1b180224e3070a1e163b2cfdff3e30

data:
1b1802a4e3070a1e17000bfdff3e30

@lolouk44
Copy link
Owner

Well that's interesting. Every single reading gives a weight. I really don't understand why you sometimes get odd weight readings.
Were you on the scale each time you ran the script?
Would you have time today/tomorrow to come on discord for a chat?

@ghost
Copy link

ghost commented Oct 31, 2019

Hi Guys, i obtain such errors;
Traceback (most recent call last):
File "/home/pi/Desktop/xiaomi/src/Xiaomi_Scale.py", line 183, in
main()
File "/home/pi/Desktop/xiaomi/src/Xiaomi_Scale.py", line 180, in main
devices = scanner.scan(5)
File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 852, in scan
self.start(passive=passive)
File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 790, in start
self._mgmtCmd("le on")
File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 312, in _mgmtCmd
raise BTLEManagementError("Failed to execute management command '%s'" % (cmd), rsp)
bluepy.btle.BTLEManagementError: Failed to execute management command 'le on' (code: 20, error: Permission Denied)
./wrapper.sh: linia 33: */5: Nie ma takiego pliku ani katalogu

Any ideas how to solve it?

@pplucky
Copy link
Author

pplucky commented Oct 31, 2019

Well that's interesting. Every single reading gives a weight. I really don't understand why you sometimes get odd weight readings.
Were you on the scale each time you ran the script?
Would you have time today/tomorrow to come on discord for a chat?

Tonight after 10PM UTC (without access to the scale, but with some ideas to your questions) or Sunday same time or a bit later (with access to the scale).

@pplucky
Copy link
Author

pplucky commented Oct 31, 2019

Hi Guys, i obtain such errors;
Traceback (most recent call last):
File "/home/pi/Desktop/xiaomi/src/Xiaomi_Scale.py", line 183, in
main()
File "/home/pi/Desktop/xiaomi/src/Xiaomi_Scale.py", line 180, in main
devices = scanner.scan(5)
File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 852, in scan
self.start(passive=passive)
File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 790, in start
self._mgmtCmd("le on")
File "/usr/local/lib/python3.7/dist-packages/bluepy/btle.py", line 312, in _mgmtCmd
raise BTLEManagementError("Failed to execute management command '%s'" % (cmd), rsp)
bluepy.btle.BTLEManagementError: Failed to execute management command 'le on' (code: 20, error: Permission Denied)
./wrapper.sh: linia 33: */5: Nie ma takiego pliku ani katalogu

Any ideas how to solve it?

Are you running with sudo? If not, try your command with sudo before

@lolouk44
Copy link
Owner

@pplucky beat me to it on the "sudo" ;)

With regards to the discord chat. I'm not sure I can do tonight. If not will look at Sunday if possible.

@pplucky
Copy link
Author

pplucky commented Oct 31, 2019

@pplucky beat me to it on the "sudo" ;)

With regards to the discord chat. I'm not sure I can do tonight. If not will look at Sunday if possible.

I'll check discord tonight and on Sunday then ;-)

@ghost
Copy link

ghost commented Oct 31, 2019

Ya I forgot that sudo avoid permission... Now works and I think i should be properly
Sent data to topic miScale/Jo/weight: {"Weight":"80.70","BMI":"26.35","Basal Metabolism":"1686.54","Visceral Fat":"15.00","Lean Body Mass":"64.17","Body Fat":"21.47","Water":"53.87","Bone Mass":"3.23","Muscle Mass":"60.14","Protein":"20.66","TimeStamp":"2019-10-31 19:17:46"}
I know at this moment works only weight, now I need to look at the rest parameteres

@pplucky
Copy link
Author

pplucky commented Nov 5, 2019

As discussed with @lolouk44 on discord, script is reading the current/last weight and only works in the 5/10 minutes after you have weighed yourself in the scale (same as Xiaomi Mi Fit app). After that it goes to deep sleep and cannot be found by script (or the app).

Script also only works if impedance has been determined properly during the weight process (socks not allowed).

Using the standard script on this device, I got the following:
{"Weight":"63.40","BMI":"23.29","Basal Metabolism":"1314.59","Visceral Fat":"10.72","Lean Body Mass":"51.31","Body Fat":"20.33","Water":"54.65","Bone Mass":"2.57","Muscle Mass":"47.94","Protein":"20.98","TimeStamp":"2019-11-05 23:06:30"}

Still then, the corresponding readings in Mi Fit are not the same:
{"Weight":"63.40","BMI":"23.2","Basal Metabolism":"1179","Visceral Fat":"6","Lean Body Mass":"NA","Body Fat":"34.3","Water":"46.8","Bone Mass":"2.50","Muscle Mass":"39.10","Protein":"14.8","TimeStamp":"2019-11-05 23:06:30"}

@lolouk44
Copy link
Owner

lolouk44 commented Nov 6, 2019

Thanks for the feedback @pplucky .
It sounds like it's all working fine then.
The body composition measurement will most likely differ a bit from the mi app. The formulas were retrieved from reverse engineering an android app some time ago. Most likely they were changed since. Plus there is no official way to calculate these based on impedance, weight, height, gender and age so one could always argue that either / both sets of values from the script or the mi app aren't correct anyway.
If it's all the same let's close this issue since the script works with your scale?

@pplucky
Copy link
Author

pplucky commented Nov 6, 2019

Agree to close.

@pplucky pplucky closed this as completed Nov 6, 2019
@ghost
Copy link

ghost commented Nov 6, 2019

@pplucky could you give me your discord name? I wanna ask about something :)

@lolouk44
Copy link
Owner

lolouk44 commented Nov 6, 2019

I'm on discord (lolouk44#7180)

@prototux
Copy link

prototux commented Nov 10, 2019

Thanks for the feedback @pplucky .
It sounds like it's all working fine then.
The body composition measurement will most likely differ a bit from the mi app. The formulas were retrieved from reverse engineering an android app some time ago. Most likely they were changed since. Plus there is no official way to calculate these based on impedance, weight, height, gender and age so one could always argue that either / both sets of values from the script or the mi app aren't correct anyway.
If it's all the same let's close this issue since the script works with your scale?

The body metrics classes has last been updated one month ago, based on the latest version of amazfir/mi fit, so i'm pretty sure it's calculations are up to date, as long as you've been keeping your fork up to date as well (which doesn't seems to be the case), especially based on the very slow release cycle of the holtek's SDK (which AFAIK hasn't been updated since some months). The current version of the body metrics classes has all the same informations the mi fit app has, the only differences being that mi fit rounds the floats.

This only apply to the "old" MIBCS though, not the new glass one, as huami may have changed parts and/or SDK with this new model.
Please don't blame my RE work as being incorrect there without keeping your fork up-to-date first :)

@lolouk44
Copy link
Owner

Thanks for the feedback @pplucky .
It sounds like it's all working fine then.
The body composition measurement will most likely differ a bit from the mi app. The formulas were retrieved from reverse engineering an android app some time ago. Most likely they were changed since. Plus there is no official way to calculate these based on impedance, weight, height, gender and age so one could always argue that either / both sets of values from the script or the mi app aren't correct anyway.
If it's all the same let's close this issue since the script works with your scale?

The body metrics classes has last been updated one month ago, based on the latest version of amazfir/mi fit, so i'm pretty sure it's calculations are up to date, as long as you've been keeping your fork up to date as well (which doesn't seems to be the case), especially based on the very slow release cycle of the holtek's SDK (which AFAIK hasn't been updated since some months). The current version of the body metrics classes has all the same informations the mi fit app has, the only differences being that mi fit rounds the floats.

This only apply to the "old" MIBCS though, not the new glass one, as huami may have changed parts and/or SDK with this new model.
Please don't blame my RE work as being incorrect there without keeping your fork up-to-date first :)

Thanks @prototux for the comments.
I wasn't trying to have a stab at your awesome work, I was just saying that there are many ways to compute the metrics. I actually spent several weeks (months?) trying to find formulas for the various body measurements and found that regardless of which "scientific papers" I was looking at, there were many versions and results. In the end I decided to use the work that you had done as it was the most complete and was taken out of the mi app (so for a mi scale or made sense)

Anyway thanks for letting me know you've updated the formulas, I'll have a look at your repo and will look to integrate it in "my" script
This time I'll probably look to fork your repo instead of copying the code ;)

@ghost
Copy link

ghost commented Nov 11, 2019

Hey Guys, some of you thinkng to present this data in some graphical way? Or do you have some ideas?

@prototux
Copy link

Thanks for the feedback @pplucky .
It sounds like it's all working fine then.
The body composition measurement will most likely differ a bit from the mi app. The formulas were retrieved from reverse engineering an android app some time ago. Most likely they were changed since. Plus there is no official way to calculate these based on impedance, weight, height, gender and age so one could always argue that either / both sets of values from the script or the mi app aren't correct anyway.
If it's all the same let's close this issue since the script works with your scale?

The body metrics classes has last been updated one month ago, based on the latest version of amazfir/mi fit, so i'm pretty sure it's calculations are up to date, as long as you've been keeping your fork up to date as well (which doesn't seems to be the case), especially based on the very slow release cycle of the holtek's SDK (which AFAIK hasn't been updated since some months). The current version of the body metrics classes has all the same informations the mi fit app has, the only differences being that mi fit rounds the floats.
This only apply to the "old" MIBCS though, not the new glass one, as huami may have changed parts and/or SDK with this new model.
Please don't blame my RE work as being incorrect there without keeping your fork up-to-date first :)

Thanks @prototux for the comments.
I wasn't trying to have a stab at your awesome work, I was just saying that there are many ways to compute the metrics. I actually spent several weeks (months?) trying to find formulas for the various body measurements and found that regardless of which "scientific papers" I was looking at, there were many versions and results. In the end I decided to use the work that you had done as it was the most complete and was taken out of the mi app (so for a mi scale or made sense)

Anyway thanks for letting me know you've updated the formulas, I'll have a look at your repo and will look to integrate it in "my" script
This time I'll probably look to fork your repo instead of copying the code ;)

AFAIK, there's no real standard as to how to represent the impedance, and a set of formulas to apply to such impedance, so, basically, what i've seen is that every scale OEM (or more precisely, the companies that makes ASICs for such scales) basically does "whatever they want" and most doesn't even really uses the most used formulas (for example, the mi fit app only does a reverse of the BMI calculation, to get your "ideal weight" even if it's not how "ideal weight" are usually calculated), but well, even the various specialists doesn't seems to agree on standard values so...

The most problematic thing would be that they (xiaomi/holtek) obviously made their models for chinese people, and added a generic model for western people as a afterthough, so, we get a simpler and less precise model, especially as basically western here means "anybody that doesn't speak mandarin" (as even say, an indian, would get the "western model" only because he set his phone language to something other than mandarin). That is especially true for kids (as some formulas are quite precise for chinese people, with something like 5 models depending on the age, where non-chinese kids get the same model from 5 to 18).

Anyway, these scales are only here to give some tendencies, not precise values, so it's good enough for that purpose, i think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants