def parseSOSmsg(self): while True: try: msg = self.queue.get(True, 10) pkt = msg['data'] except: pass try: data = pysos.unpack('<BbB', pkt) temp = float(data[1]) # Moving average alpha = 0.8 if data[0] == 2 and data[2] == 200: self.Chorssi[0] = self.Chorssi[0] * (1 - alpha) + alpha * temp elif data[0] == 3 and data[2] == 200: self.Chorssi[1] = self.Chorssi[1] * (1 - alpha) + alpha * temp elif data[0] == 7 and data[2] == 200: self.Chorssi[2] = self.Chorssi[2] * (1 - alpha) + alpha * temp elif data[0] == 8 and data[2] == 200: self.Chorssi[3] = self.Chorssi[3] * (1 - alpha) + alpha * temp elif data[0] == 2 and data[2] == 201: self.Kimrssi[0] = self.Kimrssi[0] * (1 - alpha) + alpha * temp self.Kimcount[0] = self.Kimcount[0] + 1 elif data[0] == 3 and data[2] == 201: self.Kimrssi[1] = self.Kimrssi[1] * (1 - alpha) + alpha * temp self.Kimcount[1] = self.Kimcount[1] + 1 elif data[0] == 7 and data[2] == 201: self.Kimrssi[2] = self.Kimrssi[2] * (1 - alpha) + alpha * temp self.Kimcount[2] = self.Kimcount[2] + 1 elif data[0] == 8 and data[2] == 201: self.Kimrssi[3] = self.Kimrssi[3] * (1 - alpha) + alpha * temp self.Kimcount[3] = self.Kimcount[3] + 1 except: # pass try: data = pysos.unpack('<B', pkt[0]) # print data # print pkt[1:] except: pass
def generic_test(msg): """ Small example of test driver usage. It simulates a virtual dice and shows which side of the dice is up. """ global oldstate global state print "message recieved" signal.alarm(ALARM_LEN) #unpack the values we are expecting, in this case it is a node id, the acclerometer id, # and a value from the sensor (node_id, node_state, data) = pysos.unpack("<BBB", msg['data']) if node_id not in state.keys(): state[node_id] = 0 oldstate[node_id] = 0 # these are some simple calculations to test the sensor value we have gotten # this is the part which you need to fill in in order to verify that the function is working # it is recommended that you send all error messages to stderr since the test suite will be checking that output # to determine if any tests failed if (node_state == START_DATA): print "initialization began correctly" if (node_state == 0): state[node_id] = data if (node_state == 155): print >> sys.stderr, "a message was freed to early, or not recieved correctly on count %d" %data if (node_state == 255): print "the message was transfered correctly on count %d" %data if (node_state == 1 and state[node_id] != data): print >> sys.stderr, " a message was lost somewhere on node %d before count %d" %(node_id,data) if (node_state == FINAL_DATA): print "finalization worked correctly"
def generic_test(msg): """ Small example of test driver usage. It simulates a virtual dice and shows which side of the dice is up. """ global oldstate global state print "message recieved" signal.alarm(ALARM_LEN) #unpack the values we are expecting, in this case it is a node id, the acclerometer id, # and a value from the sensor (node_id, node_state, data) = pysos.unpack("<BBB", msg['data']) if node_id not in state.keys(): state[node_id] = 0 oldstate[node_id] = 0 # these are some simple calculations to test the sensor value we have gotten # this is the part which you need to fill in in order to verify that the function is working if (node_state == START_DATA): print "initialization began correctly" elif (node_state == FINAL_DATA): print "finalization worked correctly" elif (node_state != data): print >> sys.std.err, "error, sys_hw_type returns %d while HW_TYPE is %d" %(data, node_state)
def generic_test(msg): """ Small example of test driver usage. It simulates a virtual dice and shows which side of the dice is up. """ global oldstate global state print "message recieved" signal.alarm(ALARM_LEN) #unpack the values we are expecting, in this case it is a node id, the acclerometer id, # and a value from the sensor (node_id, accelid, value) = pysos.unpack("<BBH", msg['data']) if node_id not in state.keys(): state[node_id] = 0 oldstate[node_id] = 0 # these are some simple calculations to test the sensor value we have gotten # this is the part which you need to fill in in order to verify that the driver is working accelid -= 1 if abs(value-centroid[accelid]) > 100: print >> sys.stderr, "the value is to far out of range for sensorid %d, the value is %d" %(accelid+1, value) print >> sys.stderr, "if the enviroment is not changing, then this is an error" else: print "the value is acceptedable, for sensorid %d, the value is %d" %(accelid+1, value)
def sht11_humidity(msg): # Note that pysos.unpack returns a tuple. The comma after the lval # notes that we want to unpack the singleton tuple. humidity, = pysos.unpack('<H', msg['data']) cal_hum = -4 + 0.0405 * humidity + -2.8 * 10**(-6) * (humidity ** 2) print("Node %d has humidity %f" % (msg['saddr'], cal_hum)) return
def generic_test(msg): """ Small example of test driver usage. It simulates a virtual dice and shows which side of the dice is up. """ global oldstate global state print "message recieved" signal.alarm(ALARM_LEN) #unpack the values we are expecting, in this case it is a node id, the acclerometer id, # and a value from the sensor (node_id, accelid, value) = pysos.unpack("<BBH", msg['data']) if node_id not in state.keys(): state[node_id] = 0 oldstate[node_id] = 0 # these are some simple calculations to test the sensor value we have gotten # this is the part which you need to fill in in order to verify that the driver is working accelid -= 6 if abs(value - centroid[accelid]) > 100: print >> sys.stderr, "the value is to far out of range for magid %d, the value is %d" % ( accelid + 6, value) print >> sys.stderr, "if the mote is not moving, then this is an error" else: print "the value is acceptable, for magid %d, the value is %d" % ( accelid + 6, value)
def generic_test(msg): """ Small example of test driver usage. It simulates a virtual dice and shows which side of the dice is up. """ global oldstate global state print "message recieved" signal.alarm(ALARM_LEN) #unpack the values we are expecting, in this case it is a node id, the acclerometer id, # and a value from the sensor (node_id, accelid, value) = pysos.unpack("<BBH", msg['data']) if node_id not in state.keys(): state[node_id] = 0 oldstate[node_id] = 0 print accelid print value # these are some simple calculations to test the sensor value we have gotten # this is the part which you need to fill in in order to verify that the driver is working g = (value-512)/1024.0*3000/333.0 if abs(g)>0.8: if g > 0: state[node_id] = accelid else: state[node_id] = 7-accelid if oldstate[node_id] != state[node_id]: print "For Node %d Side %d is up"%(node_id, state[node_id],) oldstate[node_id] = state[node_id]
def result_handler(msg): (sid, qid, num_remaining, sensor, value) = pysos.unpack('<BHHBH', msg['data']) print "node id %d" %sid print "QueryId %d" %qid print "SensorId %d" %sensor print "Value %d" %value
def generic_test(msg): """ Small example of test driver usage. It simulates a virtual dice and shows which side of the dice is up. """ global oldstate global state print "message recieved" signal.alarm(ALARM_LEN) # unpack the values we are expecting, in this case it is a node id, the acclerometer id, # and a value from the sensor (node_id, node_state, data) = pysos.unpack("<BBB", msg["data"]) if node_id not in state.keys(): state[node_id] = 0 oldstate[node_id] = 0 # these are some simple calculations to test the sensor value we have gotten # this is the part which you need to fill in in order to verify that the function is working if node_state == START_DATA: print "initialization began correctly" if node_state == ORDER_FAIL: print >> sys.stderr, "messages sent out of order for node %d on count %d" % (node_id, data) if node_state == ORDER_GOOD: print "messages sent in order for node %d on count %d" % (node_id, data) if node_state == 0: state[node_id] = data if node_state == 1 and state[node_id] != data: print >> sys.stderr, " a message was lost somewhere" if node_state == FINAL_DATA: print "finalization worked correctly"
def sht11_temperature(msg): # Note that pysos.unpack returns a tuple. The comma after the lval # notes that we want to unpack the singleton tuple. temperature, = pysos.unpack('<H', msg['data']) cal_temp = -39.60 + 0.01 * temperature print("Node %d has temperature %f" % (msg['saddr'], cal_temp)) return
def generic_test(msg): """ Small example of test driver usage. It simulates a virtual dice and shows which side of the dice is up. """ global oldstate global state # print "message recieved" signal.alarm(ALARM_LEN) # unpack the values we are expecting, in this case it is a node id, the acclerometer id, # and a value from the sensor (node_id, node_state, data) = pysos.unpack("<BBB", msg["data"]) if node_id not in state.keys(): state[node_id] = 0 oldstate[node_id] = 0 # these are some simple calculations to test the sensor value we have gotten # this is the part which you need to fill in in order to verify that the function is working if node_state == START_DATA: print "initialization began correctly" state[node_id] = 0 if node_state == 0 and data == 200: state[node_id] = 1 if node_state == 0 and data == 255: if state[node_id] != 1: print >> sys.stderr, "something has gone wrong and is out of order on node %d" % node_id state[node_id] = 0 if node_state == 155: print >> sys.stderr, "realloc failed to correctly copy the %dth value" % data if node_state == FINAL_DATA: print "finalization worked correctly"
def generic_test(msg): """ Small example of test driver usage. It simulates a virtual dice and shows which side of the dice is up. """ global oldstate global state print "message recieved" signal.alarm(ALARM_LEN) #unpack the values we are expecting, in this case it is a node id, the acclerometer id, # and a value from the sensor (node_id, node_state, data) = pysos.unpack("<BBB", msg['data']) if node_id not in state.keys(): state[node_id] = 0 oldstate[node_id] = 0 # these are some simple calculations to test the sensor value we have gotten # this is the part which you need to fill in in order to verify that the function is working if (node_state == START_DATA): print "initialization began correctly" if (node_state == 0): state[node_id] = data if (node_state == 101): rmmod_cmd = ['sos_tool.exe', '--rmmod=%d' %data] subprocess.call(rmmod_cmd) srv.post_net(129, 129, MSG_MOD_REMOVED, 0, '', 0, 0) if (node_state == 1 and state[node_id] != data): print >> sys.stderr, " a message was lost somewhere on node %d before count %d" %(node_id,data) if (node_state == 155): print >> sys.stderr, "an error occured with the transfered memory" if (node_state == FINAL_DATA): print "finalization worked correctly"
def generic_test(msg): """ Small example of test driver usage. It simulates a virtual dice and shows which side of the dice is up. """ global oldstate global state #print "message recieved" signal.alarm(ALARM_LEN) #unpack the values we are expecting, in this case it is a node id, the acclerometer id, # and a value from the sensor (node_id, node_state, data) = pysos.unpack("<BBB", msg['data']) if node_id not in state.keys(): state[node_id] = 0 oldstate[node_id] = 0 # these are some simple calculations to test the sensor value we have gotten # this is the part which you need to fill in in order to verify that the function is working if (node_state == START_DATA): print "initialization began correctly" state[node_id] = 0 if (node_state == 0 and data == 200): state[node_id] = 1 if (node_state == 0 and data == 255): if (state[node_id] != 1): print >> sys.stderr, "something has gone wrong and is out of order on node %d" % node_id state[node_id] = 0 if (node_state == 155): print >> sys.stderr, "realloc failed to correctly copy the %dth value" % data if (node_state == FINAL_DATA): print "finalization worked correctly"
def generic_test(msg): """ Small example of test driver usage. It simulates a virtual dice and shows which side of the dice is up. """ global oldstate global state print "message recieved" signal.alarm(ALARM_LEN) #unpack the values we are expecting, in this case it is a node id, the acclerometer id, # and a value from the sensor (node_id, node_state, data) = pysos.unpack("<BBB", msg['data']) if node_id not in state.keys(): state[node_id] = 0 oldstate[node_id] = 0 # these are some simple calculations to test the sensor value we have gotten # this is the part which you need to fill in in order to verify that the function is working if (node_state == START_DATA): print "initialization began correctly" if (node_state == 0): state[node_id] = data if (node_state == 1 and state[node_id] != data): print >> sys.stderr, " a message was lost somewhere on node %d before count %d" % ( node_id, data) if (node_state == FINAL_DATA): print "finalization worked correctly"
def result_handler(msg): (sid, qid, num_remaining, sensor, value) = pysos.unpack('<BHHBH', msg['data']) print "node id %d" % sid print "QueryId %d" % qid print "SensorId %d" % sensor print "Value %d" % value
def parseSOSmsg(self): while True: try: msg = self.queue.get(True,10) pkt = msg['data'] except: pass try: data = pysos.unpack('<BbB',pkt) temp = float(data[1]) # Moving average alpha = 0.8 if data[0] == 2 and data[2] == 200: self.Chorssi[0] = self.Chorssi[0]*(1-alpha)+alpha*temp elif data[0] == 3 and data[2] == 200: self.Chorssi[1] = self.Chorssi[1]*(1-alpha)+alpha*temp elif data[0] == 7 and data[2] == 200: self.Chorssi[2] = self.Chorssi[2]*(1-alpha)+alpha*temp elif data[0] == 8 and data[2] == 200: self.Chorssi[3] = self.Chorssi[3]*(1-alpha)+alpha*temp elif data[0] == 2 and data[2] == 201: self.Kimrssi[0] = self.Kimrssi[0]*(1-alpha)+alpha*temp self.Kimcount[0] = self.Kimcount[0]+1 elif data[0] == 3 and data[2] == 201: self.Kimrssi[1] = self.Kimrssi[1]*(1-alpha)+alpha*temp self.Kimcount[1] = self.Kimcount[1]+1 elif data[0] == 7 and data[2] == 201: self.Kimrssi[2] = self.Kimrssi[2]*(1-alpha)+alpha*temp self.Kimcount[2] = self.Kimcount[2]+1 elif data[0] == 8 and data[2] == 201: self.Kimrssi[3] = self.Kimrssi[3]*(1-alpha)+alpha*temp self.Kimcount[3] = self.Kimcount[3]+1 except: # pass try: data = pysos.unpack('<B',pkt[0]) # print data # print pkt[1:] except: pass
def generic_test(msg): """ Small example of test driver usage. It simulates a virtual dice and shows which side of the dice is up. """ global oldstate global state print "message recieved" signal.alarm(ALARM_LEN) #unpack the values we are expecting, in this case it is a node id, the acclerometer id, # and a value from the sensor (node_id, node_state, data) = pysos.unpack("<BBH", msg['data']) if node_id not in state.keys(): state[node_id] = [] # these are some simple calculations to test the sensor value we have gotten # this is the part which you need to fill in in order to verify that the function is working if (node_state == START_DATA): print "initialization began correctly" elif (node_state == FINAL_DATA): print "finalization worked correctly" else: if len(state[node_id]) < 100: state[node_id].append(data) else: expected = 100/66.0 counts = [] for i in range(66): counts.append(0) for num in state[node_id]: counts[num / 1000] += 1 avg_count = 0.0 for count in counts: avg_count += count / 66.0 print "the average count is : %f, it should be %f" %(avg_count, expected) if abs(expected - avg_count) > 2: print sys.stderr, "this is less than random" state[node_id] = []
def generic_test(msg): """ Small example of test driver usage. It simulates a virtual dice and shows which side of the dice is up. """ global oldstate global state print "message recieved" signal.alarm(ALARM_LEN) #unpack the values we are expecting, in this case it is a node id, the acclerometer id, # and a value from the sensor (node_id, accelid, value) = pysos.unpack("<BBH", msg['data']) if node_id not in state.keys(): state[node_id] = 0 oldstate[node_id] = 0 print accelid print (1.223 * 1024 / value), "volts"
def accel_test(msg): """ Small example of accelerometer usage. It simulates a virtual dice and shows which side of the dice is up. """ global oldstate global state signal.alarm(60) (nodeid, change_freq, packetid, accelid, value) = pysos.unpack("<BBHBH", msg['data']) if nodeid not in count.keys(): count[nodeid] = packetid total[nodeid] = 0 else: if (packetid % 1000 == 0): print "total packets lost for node %d after 1000 messages is %d" % ( nodeid, total[nodeid]) total[nodeid] = 0 if (packetid - count[nodeid]) > 1: total[nodeid] = total[nodeid] + 1 if (change_freq == 2): print "node %d changing frequency to 50hz" % nodeid elif (change_freq == 1): print "node %d changing frequency to 100hz" % nodeid count[nodeid] = packetid accelid -= 4 if abs(value - centroid[accelid]) > 100: print >> sys.stderr, "the value is to far out of range for accel id %d, the value is %d" % ( accelid + 4, value) print >> sys.stderr, "if the mote is not moving, then this is an error" else: print "the value is acceptedable, for accelid %d, the value is %d" % ( accelid + 4, value)
def generic_test(msg): """ Small example of test driver usage. It simulates a virtual dice and shows which side of the dice is up. """ global oldstate global state print "message recieved" signal.alarm(ALARM_LEN) #unpack the values we are expecting, in this case it is a node id, the acclerometer id, # and a value from the sensor (node_id, node_state, data) = pysos.unpack("<BBB", msg['data']) if node_id not in state.keys(): state[node_id] = 0 oldstate[node_id] = 0 # these are some simple calculations to test the sensor value we have gotten # this is the part which you need to fill in in order to verify that the function is working if (node_state == START_DATA): print "initialization began correctly" if (node_state == 0): state[node_id] = data if (node_state == 101): rmmod_cmd = ['sos_tool.exe', '--rmmod=%d' % data] subprocess.call(rmmod_cmd) srv.post_net(129, 129, MSG_MOD_REMOVED, 0, '', 0, 0) if (node_state == 1 and state[node_id] != data): print >> sys.stderr, " a message was lost somewhere on node %d before count %d" % ( node_id, data) if (node_state == 155): print >> sys.stderr, "an error occured with the transfered memory" if (node_state == FINAL_DATA): print "finalization worked correctly"
def accel_test(msg): """ Small example of accelerometer usage. It simulates a virtual dice and shows which side of the dice is up. """ global oldstate global state signal.alarm(60) (nodeid, change_freq, packetid, accelid, value) = pysos.unpack("<BBHBH", msg['data']) if nodeid not in count.keys(): count[nodeid] = packetid total[nodeid] = 0 else: if ( packetid % 1000 == 0): print "total packets lost for node %d after 1000 messages is %d" %(nodeid, total[nodeid]) total[nodeid] = 0 if (packetid - count[nodeid] ) > 1 : total[nodeid] = total[nodeid] + 1 if (change_freq == 2): print "node %d changing frequency to 50hz" %nodeid elif (change_freq == 1): print "node %d changing frequency to 100hz" %nodeid count[nodeid] = packetid accelid -= 4 if abs(value-centroid[accelid]) > 100: print >> sys.stderr, "the value is to far out of range for accel id %d, the value is %d" %(accelid+4, value) print >> sys.stderr, "if the mote is not moving, then this is an error" else: print "the value is acceptedable, for accelid %d, the value is %d" %(accelid+4, value)
def func(msg): (addr, t1, t2) = pysos.unpack('<HLL', msg['data']) if addr == 0: print 50* "-" print "addr: ", addr, "time: ", t1, "refreshed: ", t2
#!/usr/bin/python import pysos try: srv = pysos.sossrv() except: print "Please run sos_server first" while True: try: msg = srv.listen(type=36) pkt = msg[0]['data'] addr = pysos.unpack('<b', pkt[0]) print addr[0] except: pass
#!/usr/bin/python import pysos try: srv = pysos.sossrv() except: print "Please run sos_server first" while True: try: msg = srv.listen(type=36) pkt = msg[0]['data'] addr = pysos.unpack('<b',pkt[0]) print addr[0] except: pass
def func(msg): (addr, t1, t2) = pysos.unpack("<HLL", msg["data"]) print "addr: ", addr, "time: ", t1, "refreshed: ", t2 if addr == 1: print 50 * "-"