In [74]:
#!/usr/bin/python
import os
import io
import string
import sys, getopt
def bitstring(bitcount):
stream=""
bits=fh.read(bitcount*2)
if (len(bits)<bitcount*2):
return ""
for pos in range(bitcount):
stream+="%d" % ord(bits[pos*2])
return stream
def bytestring(reverse):
bitcount=8
pos=0
bits=fh.read(bitcount*2)
word=0
if (len(bits)<bitcount*2):
return 0
for pos in range(bitcount):
bit=ord(bits[pos*2])
word=word+(bit<<pos)
if reverse:
word=((word&0xF)<<4)+((word&0xF0)>>4)
return word
def convertflags(flag):
flagstr=""
negative=""
if (flag&2)==2:
negative="-"
flagstr+="Negative temperature"
if flagstr!="":
flagstr+=","
if (flag&8)==8:
flagstr+="Battery low"
return [flagstr,negative]
#111111111111 01100 1111 1111 111111000000000110111011000000000000000
#111111111111 01100 1101 0010 010010000000000110111011000000000000000
if __name__ == '__main__':
inputfile = ""
outputfile = ""
try:
opts, args = getopt.getopt(sys.argv,"hi:o:",["ifile=","ofile="])
for opt, arg in opts:
if opt == '-h':
print 'OregonScientific.py -i <inputfile> -o <outputfile>'
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
print 'Output file is "', outputfile
except getopt.GetoptError:
print "OregonScientific.py -i <inputfile> -o <outputfile>"
inputfile="/tmp/fifo"
print 'Input file is ', inputfile
try:
fh=open(inputfile,"rb")
except IOError:
print("Couldn't open file : "+inputfile)
exit()
bitcounter=0
init = [1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0]
syncV1 = "01100"
syncV2 = "11110"
for block in iter(lambda: fh.read(1), ""):
if bitcounter<(11*2):
if init[bitcounter]==ord(block):
bitcounter=bitcounter+1
else:
bitcounter=0
else:
print("Sync found")
fh.read(1)
bitsync=bitstring(5)
if (syncV1==bitsync):
v1=bytestring(True)
temp1=bytestring(True)
temp2=bytestring(True)
checksum=bytestring(True)
if (v1+temp1+temp2 == checksum):
print("%02X%02X%02X" % (v1,temp1,temp2))
rolcode=(v1&0xF0)>>4
channel=((v1&0xF)/4)+1
flag=(temp2&0xF)
flaglist=convertflags(flag)
negative=flaglist[0]
flagstr=flaglist[1]
temperature=("%s%d.%01d" % (negative,((((temp2&0xF0)>>4)<<4)+(temp1&0xF)),(temp1&0xF0)>>4))
print("Oregon Scientific V1 - Rolling code %01X - Channel %d - Temperature %s C - Flags (%s) - Checksum: %02X" % (rolcode,channel,temperature,flagstr,checksum))
elif (syncV2==bitsync):
bitstring(3)
sensorid=(bytestring(True)<<8)+bytestring(True)
nibble45=bytestring(True)
nibble67=bytestring(True)
channel=(nibble45&0xF0)>>4
rollingcode=((nibble45&0xF)<<4)+((nibble67&0xF0)>>4)
flag=nibble67&0xF
flaglist=convertflags(flag)
negative=flaglist[0]
flagstr=flaglist[1]
temperature=""
if (sensorid==0xEC40):
temp1=bytestring(True)
temp2=bytestring(True)
checksum=bytestring(True)
checksum=((checksum&0xF)<<4)+((checksum&0xF0)>>4)
temperature=("Temperature %s%d.%01d C" % (negative,((((temp2&0xF0)>>4)<<4)+(temp1&0xF)),(temp1&0xF0)>>4))
else:
temperature=("RawData: %02X %02X %02X %02X " % (bytestring(True),bytestring(True),bytestring(True),bytestring(True)))
checksum=0
#temperature=("%s%d.%01d" % (negative,((((temp2&0xF0)>>4)<<4)+(temp1&0xF)),(temp1&0xF0)>>4))
print("Oregon Scientific V2 - Sensor Id %04X - Rolling code %01X - Channel %d - %s - Flags (%s) - Checksum: %02X" % (sensorid, rollingcode,channel,temperature,flagstr,checksum))
bitcounter=0
In [70]:
In [37]:
In [32]:
In [32]:
In [ ]: