#jleblanc 7.30.08 #python #XBee API parser for WUP with error handling class Sensor: def __init__(self, id, watts, RSSI): self.id = id self.last_watts ='-10.0' self.update(watts, RSSI) self.total = 0 self.hits = 0 self.RSSI = RSSI self.timelast = time.time() def update(self, watts, RSSI): try: insert_current(self.id, watts, RSSI) self.total=self.total+float(watts) self.hits = self.hits+1.0 if time.time() - self.timelast > 300: insert_history(self.id, str(float(self.total/self.hits))) self.timelast = time.time() self.total = 0 self.hits = 0 ######################################### except: print 'error updating' def insert_current(id, w, rssi): #print 'now watts: '+w+' RSSI: '+str(rssi) postdata(current_root,id,w, rssi) def insert_history(id, w): #print 'mysql'+w postdata(history_root,id,w, -1) def postdata(url_root,id,watts,rssi): msg = url_root+'id='+str(id)+'&watts='+str(watts) if rssi != -1: msg = msg+'&rssi='+str(rssi) try: f = urllib.urlopen(msg) print "posted to server: "+msg except: print "couldn't write to server" def getserialbyte(ser): byte = ser.read() if len(byte)==1: #check that we got a real byte byte = ord(byte) else: byte = -1 return byte def OpenSerialPort(keyword): #Find and open proper serial port if os.name == 'posix': #mac ports = glob.glob('/dev/tty.*') #the 2nd port in the list is typically the serial device portnum=2 #still, we search for the first port with 'usb' in the name print '>> found '+str(len(ports))+ ' ports:' for i in range(0, len(ports)): print '['+str(i)+']: '+ports[i] if ports[i].find(keyword) != -1: portnum=i serialport = serial.Serial(ports[portnum], 9600, timeout=2) print ">> opening serial port ["+str(portnum)+"]: "+serialport.portstr else: #assume is windows (xp comes up as 'nt') serialport = serial.Serial(1, 9600, timeout=1) # the 3rd port in the list is typically the usb device print serialport.portstr return serialport def getMSG(Serial, list, possibles): Watts='' #look for header byte h = 0 while h != 126: h=getserialbyte(Serial) #read the rest of the bytes: lengthMSB = getserialbyte(Serial) lengthLSB = getserialbyte(Serial) API_ID = getserialbyte(Serial) addrMSB = getserialbyte(Serial) addrLSB = getserialbyte(Serial) #create the ID ID = hex(addrMSB*0x100+addrLSB).replace('0xaa','') RSSI = getserialbyte(Serial) RSSI = int(RSSI) #check this is correct options = getserialbyte(Serial) #read the data bits for i in range(lengthLSB-5): Watts = Watts+str(chr(getserialbyte(Serial))) try: Watts = str(float(Watts)/10.0) except: print 'input error' #print Watts checksum = getserialbyte(Serial) #act on input if ID in possibles : if ID not in list : list[ID] = Sensor(ID,Watts, RSSI) #print '' else : list[ID].update(Watts, RSSI) #print '' else : print 'non true attempt'+str(ID) #MAIN import serial import glob import urllib import os import time addresses = [ '00'] sensors = {} history_root = 'http://itp.nyu.edu/electricity-monitoring/interface/insert_history.php?' current_root = 'http://itp.nyu.edu/electricity-monitoring/interface/insert_current.php?' S = OpenSerialPort('usbserial-A1000iMO') #flush the buffer out S.flush() while 1==1: getMSG(S, sensors, addresses) S.close()