Created
November 5, 2016 21:50
-
-
Save zougloub/cb28428946807992fe0328fb08e6b889 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 vi:noet | |
# Thermoco reader | |
import subprocess | |
class Thermoco(object): | |
def __init__(self, mac): | |
self._mac = mac | |
def get_temp(self): | |
cmd = [ | |
"gatttool", | |
"--device=%s" % self._mac, | |
"--char-read", | |
"--handle=0x0029", | |
] | |
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE) | |
out = proc.stdout.read() | |
res = proc.wait() | |
if res != 0: | |
return -273.15 | |
vals = out.decode("utf-8").split()[-2:] | |
temp_code = int(vals[0], 16)*256 + int(vals[1], 16) | |
temp = temp_code / 256.0 | |
if int(vals[0], 16) >= 128: | |
temp -= 256 | |
return temp | |
if __name__ == "__main__": | |
import sys, io, os, time, datetime, argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument("mac", | |
nargs="+", | |
) | |
args = parser.parse_args() | |
thermocos = [ Thermoco(x) for x in args.mac ] | |
while True: | |
print("%s" % datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S")) | |
for thermoco in thermocos: | |
sys.stdout.write("- %s" % thermoco._mac) | |
print(": %9.4f" % (thermoco.get_temp())) | |
time.sleep(1.0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment