-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyunmai-mqtt.py
More file actions
114 lines (84 loc) · 3.6 KB
/
Copy pathyunmai-mqtt.py
File metadata and controls
114 lines (84 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python3.4
from bluepy.btle import Scanner, DefaultDelegate, Peripheral
from YunmailLib import YunmaiLib
from time import sleep
import paho.mqtt.client as mqtt
import codecs
mac="0C:B2:B7:17:D1:F0"
sex=1 #1=Male, 2=Female
height=183
veryActive= True
age=35
mqttServer="172.16.2.1"
mqttPrefix="paxyhome"
client = mqtt.Client()
class MyDelegate(DefaultDelegate):
def __init__(self):
DefaultDelegate.__init__(self)
def handleNotification(self, cHandle, data):
if(len(data) < 20):
return
#print(" Data {} size {}".format(data.hex(),len(data)))
self.processData(data)
def processData(self,data):
#data=data.hex()
data=codecs.encode(data,'hex')
weight=int((data[26:28] + data[28:30]), 16) * 0.01
resistance=int((data[30:32] + data[32:34]), 16)
fat=int((data[34:36] + data[36:38]), 16) * 0.01
#print (fat)
if(resistance==0):
client.publish('{}/yunmai/weight'.format(mqttPrefix), round(weight,1))
print('{"weight":',round(weight,1),' }')
return
scale=YunmaiLib(sex==1,height,veryActive)
fat=scale.getFat(age,weight,resistance)
muscle=scale.getMuscle(fat)
water=scale.getWater(fat)
boneMass=scale.getBoneMass(muscle,weight)
skeletalMuscle=scale.getSkeletalMuscle(fat)
leanBodyMass=scale.getLeanBodyMass(weight,fat)
visceralFat=scale.getVisceralFat(fat,age)
client.publish('{}/yunmai/weight'.format(mqttPrefix), round(weight,1))
client.publish('{}/yunmai/fat'.format(mqttPrefix), round(fat))
client.publish('{}/yunmai/muscle'.format(mqttPrefix), round(muscle))
client.publish('{}/yunmai/water'.format(mqttPrefix), round(water))
client.publish('{}/yunmai/boneMass'.format(mqttPrefix), round(boneMass))
client.publish('{}/yunmai/skeletalMuscle'.format(mqttPrefix), round(skeletalMuscle))
client.publish('{}/yunmai/leanBodyMass'.format(mqttPrefix), round(leanBodyMass))
client.publish('{}/yunmai/visceralFat'.format(mqttPrefix), round(visceralFat))
print('{"weight":',round(weight,1),', "fat":',round(fat),' , "muscle":',round(muscle),', "water":',round(water),', "boneMass":',round(boneMass),', "skeletalMuscle":',round(skeletalMuscle),', "leanBodyMass":',round(leanBodyMass),', "visceralFat":',round(visceralFat),' }')
return
while True:
try:
client.connect(mqttServer)
client.loop_start();
p = Peripheral(mac)
p.setDelegate( MyDelegate() )
packet=bytearray.fromhex("12100100003a85")
packet.extend(height.to_bytes(1, 'little'))
packet.extend(sex.to_bytes(1, 'little'))
packet.extend(age.to_bytes(1, 'little'))
packet.extend(bytearray.fromhex("555a00000103")) # set scale data
checksum = 0
for el in packet:
checksum ^= el
packet.extend(bytes(checksum.to_bytes(1, 'little')))
value = bytearray(b'\x0d')
value.extend(packet)
handle=44
p.writeCharacteristic(handle, value, True)
#print(" Connected to {}".format(mac))
handle=40
value=bytes([1, 0])
p.writeCharacteristic(handle, value, True)
#print(" Write {} to {:02x}".format(value, handle))
while True:
if p.waitForNotifications(1.0):
#print(" Collecting data")
continue
#print ("Waiting...")
except Exception as e:
print('{"error": "',e,'"}')
raise e
sleep(1)