class BridgeGlobal: def __init__(self): """ Create global bridge, initialize the bridge client and the lock for synchronization. """ # initialize bridge self.bridge = BridgeClient() # initialize synchronization lock self.lock = Lock() def get(self, key): """ Get from the bridge with synchronization. :param key: Key of the value (memory address). :return: Value of the key. """ # acquire the synchronization lock self.lock.acquire() # get the value value = self.bridge.get(key) # release the synchronization lock self.lock.release() # return the value return value def put(self, key, value): """ Put the value in the bridge with synchronization. :param key: Key of the value (memory address). :param value: Value to set. """ # acquire the synchronization lock self.lock.acquire() # set the value self.bridge.put(key, value) # release the synchronization lock self.lock.release()
#!/usr/bin/python import cgi,sys,os form = cgi.FieldStorage() sys.path.insert(0,"/usr/lib/python2.7/bridge/") from bridgeclient import BridgeClient temps = float(form.getvalue("tempp")) client = BridgeClient() # loop to store most recent 20 temp/humid values max_str = 20 ttt = client.get("temp") jjj = ttt[0:len(ttt)-2] #for num in range(19): # shift former values, drop the oldest values if (len(jjj) < max_str): for num in range(len(jjj),20): jjj.append(0) #first_query = os.environ["QUERY_STRING"] #chk = client.get("temp"); #pp = len(chk) #if (first_query == ""): # ttt[0] = temps #else: # ttt[0] = 20 jjj.insert(0,temps) #ttt = [122.0,125,111,temps] ttt = jjj client.put("temp",ttt); # store the recent value
on_register=on_register) device_registration_with_retry() LED_flash(1) if __name__ == "__main__": isChange = 0 resetCounter = 1 reConnecting = 0 while True: try: for f_name, type_ in idf_list: type_ = json.dump(type_) tmp = client.get(f_name) if tmp is None: continue else: client.delete(f_name) v = type_(tmp) if v is not None: os.system( r'echo "default-on" > /sys/class/leds/ds:green:wlan/trigger' ) print('DAN.push({f}, {v!r})'.format( f=f_name, v=v, )) dan.push(f_name, v)
# in just a few lines.. sock = socket.socket(socket.AF_INET, # Internet socket.SOCK_DGRAM) # UDP sock.bind(("", UDP_PORT)) #----- # loop #repeats ad nauseam while True: # waits until it receives data data, addr = sock.recvfrom(512) # print "received %r from %r" % (data, addr) if data == "DISCOVERY_PACKET": hardware_count = bridge.get('hardware_count') data += ',' data += str(hardware_count) state_update = bridge.get('state_update') data += ',' data += state_update # sends discovery packet sock.sendto(data, (addr[0], UDP_PORT)) elif data == "7": bridge.put('udp', data) state_update = bridge.get('state_update') sock.sendto(state_update, (addr[0], UDP_PORT)) else: # puts data on arduino bridge bridge.put('udp', data) if should_echo and (data != last_packet):
incomming[f_name] = (incomming[f_name] + 1) % 10000 #client.put('incomming_'+f_name, str(incomming[f_name])) #print ('Bridge: change incomming state of '+f_name) print '{f}[{d}] -> {p} = {v}, incomming[{f}] = {i}'.format( f=f_name, d=index, p=pin_name, v=str(int(PIN[index])), i=incomming[f_name], ) os.system(r'echo "none" > /sys/class/leds/arduino:blue:wlan/trigger') for f_name, type_ in idf_list: tmp = client.get(f_name) if tmp is None: continue else: client.delete(f_name) v = type_(tmp) if v is not None: os.system(r'echo "default-on" > /sys/class/leds/arduino:blue:wlan/trigger') print 'DAN.push({f}, {v!r})'.format( f=f_name, v=v,) DAN.push(f_name, v) os.system(r'echo "none" > /sys/class/leds/arduino:blue:wlan/trigger') if reConnecting: LED_flash(1) reConnecting = 0
from bridgeclient import BridgeClient client = BridgeClient() logBuffer = [] logBufferMaxLen = 24 def writeBufferToFile(): logFile = open('tempLog.log', 'w') for entry in logBuffer: logFile.write(entry + '\n') logFile.close() while True: timeStruct = time.localtime(time.time()) # write into log every hour if timeStruct[4] == 0: val = client.get('temp') currentTime = time.strftime('%H:%M%:%S', timeStruct) entry = currentTime + ' ' + val logBuffer.append(entry) if len(logBuffer) > logBufferMaxLen: logBuffer.pop(0) writeBufferToFile() time.sleep(1)
addr = None #----- print "Setup the UDP Socket..." # set up UDP server. Python can do a simple server # in just a few lines.. sock = socket.socket(socket.AF_INET, # Internet socket.SOCK_DGRAM) # UDP sock.bind(("", UDP_PORT)) sock.settimeout(1.0) #----- # loop #repeats ad nauseam while True: # check if theres packets from serial, send if they are available arduino_data = bridge.get('from_arduino') if arduino_data and addr: #print "received %r with size %r" % (arduino_data, len(arduino_data)) sock.sendto(arduino_data, (addr[0], UDP_PORT)) bridge.put('from_arduino', "") # waits to receive data, timeout try: udp_data, addr = sock.recvfrom(512) #print "received %r from %r" % (udp_data, addr) if udp_data: bridge.put('from_udp', udp_data) except socket.timeout as err: pass
#!/usr/bin/python import sys,time,socket sys.path.insert(0,"/usr/lib/python2.7/bridge/") from bridgeclient import BridgeClient client = BridgeClient() s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(5) while 1: ttt = client.get("temp") dur = client.get("water_duration_min") print ttt[0] print dur if float(ttt[0]) > 35.0: print "turn on valve\n" try: s.connect(("192.168.240.146",8081)) except: print "Unable to connect" exit() s.send("GET /a0 /HTTP1.1 \r\n") time.sleep(int(dur)) print "turn off valve\n" s.send("GET /a1 HTTP/1.1\r\n") s.close() time.sleep(5) time.sleep(5)
# your projects. # # In no event shall ArduinoMeetsLinux.com be liable for any special, # indirect, incidental, or consequential damages (including, but not # limited to, loss of data and damage to systems) arising in any way # out of the use, reproduction, modification and/or distribution of # this code. import sys, time, os sys.path.insert(0, "/usr/lib/python2.7/bridge/") from bridgeclient import BridgeClient client = BridgeClient() #client.put("MCP9808_Temperature",54) #temp = float(client.get("MCP9808_Temperature")) temp1 = client.get("temp") temp = temp1[0] unit = os.environ["QUERY_STRING"] if unit == "": unit = "C" print "Content-type: text/html" print print """ <!DOCTYPE html> <html> <head> <title>Project 1 - Building a Web-Based Temperature Monitor</title> <link rel="stylesheet" href="/css/normal.css" type="text/css" /> <meta http-equiv="refresh" content="10"> <meta charset="UTF-8">