Beispiel #1
0
 def getData(self):
     """
     On button click a MiBand2 object is created and feed the MAC address.
     This then connects to device, downloads data from last update and
     then stores in database. Updates Properties of watch in Datbase
     ARGS:       None
     Returns:    None
     """
     if testing:
         print("Downloading")
     band = MiBand2(self.MAC, debug=False)
     band.setSecurityLevel(level="medium")
     band.authenticate()
     band._auth_previews_data_notif(True)
     start_time = (datetime.utcfromtimestamp(
         self.u_time).strftime("%d.%m.%Y %H:%M"))
     start_time = datetime.strptime(start_time, "%d.%m.%Y %H:%M")
     if testing:
         print("Getting Data")
     band.start_get_previews_data(start_time)
     while band.active:
         band.waitForNotifications(0.1)
     d_base.set_para('battery', band.get_battery_info()['level'])
     d_base.set_para('soft_ver', band.get_revision())
     d_base.set_para('hard_ver', band.get_hrdw_revision())
     d_base.set_para('ser_num', band.get_serial())
     band.send_alert(ALERT_TYPES.MESSAGE)
     band.disconnect()
     self.battery_label_text.set("{}%".format(d_base.get_para('battery')))
     time_new = datetime.fromtimestamp(d_base.get_para('u_time') +
                                       43200).strftime('%Y-%m-%d %H:%M')
     self.time_u.set(time_new)
     if testing:
         print("Download Complete")
Beispiel #2
0
def threadPulsaciones(a):
    print "entrado a pulsaciones"
    MAC = 'F5:CF:1D:4D:71:B7'
    band = MiBand2(MAC, debug=True)
    band.setSecurityLevel(level="medium")
    band.authenticate()
    print "inicializado"
    band.start_heart_rate_realtime(heart_measure_callback=l)
    band.disconnect()
def connect():
    print('connect_msg_support.connect')
    sys.stdout.flush()
    print('initiate mi band 2')
    sys.stdout.flush()
    band = MiBand2(MAC, debug=True)
    band.setSecurityLevel(level="medium")
    if band.initialize():
        w.Label1.configure(text='''Mi Band Connected!''')
        w.but40.configure(text="Ok", command=destroy_window)
    band.disconnect()
Beispiel #4
0
 def initialise_device(self):
     if testing:
         print("Initialising Device")
     MAC = self.c_mac.get()
     print(MAC)
     band = MiBand2(MAC, debug=False)
     band.setSecurityLevel(level="medium")
     #if band.initialize():
     #print("Init OK")
     band.set_heart_monitor_sleep_support(enabled=True)
     band.disconnect()
     d_base.set_para('mac_add', MAC)
     if testing:
         print("Initailisation Complete")
     self.destroy
Beispiel #5
0
def get_heartrate():
    MAC = "DF:2B:B6:A1:E3:8D"
    band = MiBand2(MAC, debug=True)
    band.initialize()
    try:
        band.setSecurityLevel(level="medium")
        band.authenticate()
        obj = band.start_raw_data_realtime()
        for _ in range(100):
            heart = obj.next()
            post("http://localhost:5000/heartrate", data={'heartrate': heart})
            print(heart)
        band.disconnect()
    except Exception as e:
        print(e)
        return
Beispiel #6
0
def main():
    MAC = sys.argv[1]
    zctx = zmq.Context()
    zsock = zctx.socket(zmq.PUB)
    zsock.bind("tcp://127.0.0.1:6001")
    band = MiBand2(MAC, zsock, debug=True)
    band.setSecurityLevel(level="medium")

    if len(sys.argv) > 2:
        if band.initialize():
            print("Init OK")

    band.authenticate()
    band.set_heart_monitor_sleep_support(enabled=False)

    #print 'getting data'
    #print 'Soft revision:',band.get_revision()
    #print 'Hardware revision:',band.get_hrdw_revision()
    #print 'Serial:',band.get_serial()
    #print 'Battery:', band.get_battery_info()
    #print 'Time:', band.get_current_time()
    #print 'Steps:', band.get_steps()
    #band.enumerate()
    band.record_data()
Beispiel #7
0
import sys
import time
from base import MiBand2
from constants import ALERT_TYPES
from datetime import datetime

MAC = sys.argv[1]

band = MiBand2(MAC, debug=True)
band.setSecurityLevel(level="medium")

if len(sys.argv) > 2:
    if band.initialize():
        print("Init OK")

band.authenticate()

#print 'getting data'
#print 'Soft revision:',band.get_revision()
#print 'Hardware revision:',band.get_hrdw_revision()
#print 'Serial:',band.get_serial()
#print 'Battery:', band.get_battery_info()
#print 'Time:', band.get_current_time()
#print 'Steps:', band.get_steps()

band.record_data()
Beispiel #8
0
try:
    f = open(basepath + sys.argv[2] + ".time", 'r')
    timestamp = int(float(f.read()))
    prev_time = datetime.fromtimestamp(timestamp)
    f.close()
    diff = datetime.now() - prev_time
    if diff.seconds / 60 < int(config.get('DEFAULT', 'check_frequency')):
        sys.exit(0)
except Exception, e:
    print "File read error?"

print "\nRunning for", sys.argv[1], "-", sys.argv[2]
print "Time:", datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ")

try:
    band = MiBand2(sys.argv[1], debug=False)
    band.setSecurityLevel(level="medium")
except Exception, e:
    print "Connection failed?"
    #print e
    sys.exit(0)

heartrates = []

client = InfluxDBClient(config.get('DEFAULT', 'influx_host'),
                        config.get('DEFAULT', 'influx_port'),
                        config.get('DEFAULT', 'influx_user'),
                        config.get('DEFAULT', 'influx_pass'),
                        config.get('DEFAULT', 'influx_db'))

hist_ins_dst = sys.argv[2] + "_activity"
Beispiel #9
0
parser.add_argument('-r', '--retry', help='Number of retries if cannot connect to the smartband', default=1)
args = parser.parse_args()


def init(mac, kafka, interval, retry):
  band = connect(mac, retry)
  while True:
    message = getHeartRate(band)
    sendMessage(kafka, message)
    time.sleep(float(interval))


def connect(mac, retry):
  for (attempt in retry):
    try:
      band = MiBand2(mac, debug=True)
      band.setSecurityLevel(level="medium")
      band.authenticate()
      return band
    except:
      print("Oops!",sys.exc_info()[0],"occured."))


def getHeartRate(band):
  data = {
    'time': datetime.now().strftime("%Y%m%dT%H%M%S"),
    'value': band.get_heart_rate_one_time()
  }
  return data