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

Decode temperature #1

Merged
merged 2 commits into from
Mar 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Contributors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Contributors
- [Nathan Faber](https://github.com/nathanfaber)
- [Eric Thomas](https://github.com/b0naf1de)
45 changes: 45 additions & 0 deletions meaterTemperature.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

__all__ = ['Temperature']

class Temperature:
def __init__(self, tempBytes):
self._tip = Temperature.bytesToInt(tempBytes[0], tempBytes[1])
self._ambient = Temperature.convertAmbient(tempBytes)

@staticmethod
def bytesToInt(byte0, byte1):
return byte1*256+byte0

@staticmethod
def convertAmbient(array):
tip = Temperature.bytesToInt(array[0], array[1])
ra = Temperature.bytesToInt(array[2], array[3])
oa = Temperature.bytesToInt(array[4], array[5])
return int(tip+(max(0,((((ra-min(48,oa))*16)*589))/1487)))

@staticmethod
def toCelsius(value):
return (float(value)+8.0)/16.0

@staticmethod
def toFahrenheit(value):
return ((Temperature.toCelsius(value)*9)/5)+32.0

def getTip(self):
return self._tip

def getTipF(self):
return Temperature.toFahrenheit(self._tip)

def getTipC(self):
return Temperature.toCelsius(self._tip)

def getAmbientF(self):
return Temperature.toFahrenheit(self._ambient)

def getAmbient(self):
return self._ambient

def getAmbientC(self):
return Temperature.toCelsius(self._ambient)

16 changes: 9 additions & 7 deletions readMeater.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import time
import pickle
import binascii

from meaterTemperature import Temperature

print "Connecting..."
addr = sys.argv[1]
Expand All @@ -40,18 +42,18 @@ def enumerateDev(dev):
print


calcF = lambda Accum, Count, Slope, Intercept: (Accum+Count*255)*Slope + Intercept

service=dev.services[2]
char=service.getCharacteristics()[1]
fp=open(addr + ".pickle", "ab")
while True:
r1 = bytearray(char.read())
tipF = calcF(r1[0], r1[1], 0.113, 31.7)
ambF = calcF(r1[2], r1[3], 1.04, 139)
print "%d,%d,%d,%d,%d,%d,%d,%d" % (r1[0], r1[1], r1[2], r1[3], r1[4], r1[5], r1[6], r1[7])
print "tip: %d %d (%fF) tail: %d %d (%fF)" % (r1[0], r1[1], tipF, r1[2], r1[3], ambF )
print ("".join("0x%02x, " % i for i in r1))

temp = Temperature(r1)
print " tip: %s %d %fF (%fC)" % (hex(temp.getTip()), temp.getTip(), temp.getTipF(), temp.getTipC())
print "ambient: %s %d %fF (%fC)" % (hex(temp.getAmbient()), temp.getAmbient(), temp.getAmbientF(), temp.getAmbientC())
print ""

pickle.dump((time.time(), r1), fp)
fp.flush()
time.sleep(1)