def getdata(): global name irbuffer = [] # place to store multiple IR readings type==1 ambientbuffer = [] # place to store multiple Ambient readings type==2 luxbuffer = [] # place to store multiple Lux readings type==3 # number of reads to save in the bufer for x in range(1, 20): irbuffer.append(luxread(1, autorange=False)) luxbuffer.append(luxread(3, autorange=False)) ambientbuffer.append(luxread(2, autorange=False)) # calculate the average value within the buffer a = (sum(ambientbuffer) / len(ambientbuffer)) b = int((sum(luxbuffer) / len(luxbuffer))) # convert to integer c = (sum(irbuffer) / len(irbuffer)) writeData(a, b, c) # Open up data stream to cosm with your URL and key try: pac = eeml.Pachube(API_URL, API_KEY) # data to send to cosm. Stuff in the '' is important because these are the titles # used in the COSM data stream. Change to whatever you like. pac.update(eeml.Data(name, b)) #pac.update(eeml.Data('IR', c)) #pac.update(eeml.Data('Ambient', a)) # send data to cosm pac.put() except: print("NETWORK ERROR") # in case you want to get the data to the terminal return ("Lux: %.2f Ambient: %.2f IR: %.2f" % (b, a, c))
def logtopachube(lnSensorNum, lnAvgUnits, ApiFeedXML, lnMaxVal, rssi, thisAdc): if ApiFeedXML == "": return if ApiFeedXML == "0": return # Send Data to Pachube # feedUrl = "/api/" + ApiFeedXML + ".xml" # feedUrl = "/v2/feeds/" + ApiFeedXML + ".xml" feedUrl = int(float(ApiFeedXML)) print "pachube feed url: " + str(feedUrl) # added use_https=False to this method due to update in eeml code lib # will set to true later when https is supported pac = eeml.Pachube(feedUrl, PACHUBE_KEY, use_https=False) # pac.update(eeml.Data(0, lnAvgUnits, minValue=0, maxValue=None, unit=eeml.Unit(name='watt', type='derivedSI', symbol='W'))) # pac.update(eeml.Data(0, lnAvgUnits, minValue=0, maxValue=lnMaxVal)) pac.update(eeml.Data(thisAdc, lnAvgUnits, minValue=0, maxValue=lnMaxVal)) try: retVal = pac.put() # print "eeml update status: "+str(retVal) # print "Sensor# ", lnSensorNum, "logged ", lnAvgUnits, " to Pachube feed: ", ApiFeedXML except Exception, e: print "TLSM.logtopachube - eeml exception: " + str(e) syslog.syslog("TLSM.logtopachube exception: eeml: " + str(e))
def on_loop(self): if not self.queue.empty(): llapMsg = self.queue.get() #print(llapMsg) devID = llapMsg[1:3] payload = llapMsg[3:] #main state machine to handle llapMsg's if payload.startswith('TMPA'): #got Temp from solar temp = payload[4:] cosm = eeml.Pachube(self.COSMUrl, self.COSMAPIKey) #send celsius data cosm.update([eeml.Data(devID + "_Temperature", temp, unit=eeml.Celsius())]) print("Cosm updated "+devID+"_Temperature with value: "+temp); # push data to cosm try: cosm.put() except : # that didnt work now what? print("Failed to Send") elif payload.startswith('BATT'): # strip temp from llap voltage = payload[4:8] # open cosm feed cosm = eeml.Pachube(self.COSMUrl, self.COSMAPIKey) #send celsius data cosm.update([eeml.Data(devID + "_Voltage", voltage, unit=eeml.Unit('Volt', 'derivedSI', 'V'))]) print("Cosm updated "+ devID +"_Voltage with value: "+voltage); # push data to cosm try: cosm.put() except : # that didnt work now what? print("Failed to Send")
def read_temperature(): FIRMATA_PIN_GREEN_LED.write(1) gettemp = sub.Popen(['gpio/tmp102/temperature_read.sh'], stdout=sub.PIPE, stderr=sub.PIPE) temp = gettemp.communicate() COSM_API_KEY = "oXbwnXgQ3OXIXNtSP8SnSt4C0U2SAKxJdlo1OEpoVTFMbz0g" # Cosm API key COSM_FEED_ID = 99986 # your Cosm feed ID COSM_API_URL = '/v2/feeds/{feednum}.xml'.format(feednum=COSM_FEED_ID) FIRMATA_PIN_GREEN_LED.write(0) FIRMATA_PIN_RED_LED.write(1) pac = eeml.Pachube(COSM_API_URL, COSM_API_KEY) pac.update([eeml.Data(0, temp[0], unit=eeml.Celsius())]) pac.put() FIRMATA_PIN_RED_LED.write(0) return temp[0]
def logtocosm(lnSensorNum, lnAvgUnits, ApiFeedXML, lnMaxVal, rssi, thisAdc): if ApiFeedXML == "": return # Send Data to cosm # feedUrl = "/api/" + ApiFeedXML + ".xml" feedUrl = "/v2/feeds/" + ApiFeedXML + ".xml" # print "feedUrl: ", feedUrl pac = eeml.Pachube(feedUrl, COSM_KEY) # pac.update(eeml.Data(0, lnAvgUnits, minValue=0, maxValue=None, unit=eeml.Unit(name='watt', type='derivedSI', symbol='W'))) # pac.update(eeml.Data(0, lnAvgUnits, minValue=0, maxValue=lnMaxVal)) pac.update(eeml.Data(thisAdc, lnAvgUnits, minValue=0, maxValue=lnMaxVal)) try: retVal = pac.put() # print "eeml update status: "+str(retVal) # print "Sensor# ", lnSensorNum, "logged ", lnAvgUnits, " to cosm feed: ", ApiFeedXML except Exception, e: print "TLSM.logtocosm - eeml exception: " + str(e) syslog.syslog("TLSM.logtocosm exception: eeml: " + str(e))
def push_data(): """Get all available data and push to Pachube. Needs a file called keys.py with the following: API_KEY = 'YOUR PERSONAL API KEY' API_URL = 'YOUR PERSONAL API URL, LIKE /api/1275.xml' """ import keys current_temp, critical_temp = get_temp() current, last_capacity = get_battery() load = get_load() load_av, threads_and_procs_running = load[1], load[4] pachube = eeml.Pachube(keys.API_URL, keys.API_KEY) pachube.update([ eeml.Data(0, current_temp), eeml.Data(1, load_av), eeml.Data(2, threads_and_procs_running), eeml.Data(3, current), eeml.Data(4, last_capacity), eeml.Data(5, critical_temp) ]) pachube.put() return
API_KEY = 'YOUR API KEY' API_URL = '/v2/feeds/79839.xml' port = '/dev/tty.usbserial-A600eoro' ser = serial.Serial(port, 9600, timeout=20) while True: data = ser.readline() if len(data) > 0: data = string.split(data, ':') pprint.pprint(data) try: print "Temperature is " + data[0] + " and lux is " + data[1] # Log data to cosm pac = eeml.Pachube(API_URL, API_KEY) pac.update([ eeml.Data('Temperature', data[0], unit=eeml.Fahrenheit()), eeml.Data('Lux', data[1]) ]) pac.put() sleep(10) except Exception, err: print "data error" ser.close()
retVal = pac.put() # print "eeml update status: "+str(retVal) # print "Sensor# ", lnSensorNum, "logged ", lnAvgUnits, " to cosm feed: ", ApiFeedXML except Exception, e: print "TLSM.logtocosm - eeml exception: " + str(e) syslog.syslog("TLSM.logtocosm exception: eeml: " + str(e)) # update cosm with the rssi (signal strength) of the xbee sensor # pac = eeml.Pachube("/api/9982.xml", COSM_KEY) lcSensorNum = str(lnSensorNum) if len(lcSensorNum) == 1: radioNo = lcSensorNum else: radioNo = lcSensorNum[:-1] pac = eeml.Pachube("/v2/feeds/9982.xml", COSM_KEY) pac.update(eeml.Data(radioNo, rssi, minValue=0, maxValue=100)) try: retVal = pac.put() # print "eeml update status: "+str(retVal) # print "Sensor# ", lnSensorNum, "logged ", rssi, " dB to a cosm signal strength feed: ", ApiFeedXML except Exception, e: print "TLSM.logtocosm - eeml exception: " + str(e) syslog.syslog("TLSM.logtocosm exception: eeml: " + str(e)) ############################################################## # log to Sen.se # CJ, 03.12.2011, added logtosense() to send feeds to Sen.se def logtosense(lnSensorNum, lnAvgUnits, ApiFeedKey):
def pachube(sensors): temp, light = sensors print str(temp) + " C - " + str(light) pac = eeml.Pachube(API_URL, API_KEY) pac.update([eeml.Data(1, temp, 'C'), eeml.Data(2, light, '')]) pac.put()
import eeml ##put your feed key## API_KEY = '********************************' ##put your feed ID FEED = 70623 ## dont change this API_URL = '/v2/feeds/{feednum}.xml'.format(feednum=FEED) # you can put it in an loop and run it as sudo python cpuloger.py & while (True): cpuper = psutil.cpu_percent() memVir = psutil.virtmem_usage() memPhy = psutil.phymem_usage() diskUsageRoot = psutil.disk_usage('/') # for debug only # print ("CPU USAGE:%s" % cpuper) # print ("Virtual Mem:%s" % memVir.percent) # print ("Physical Mem:%s" % memPhy.percent) # print ("Disk Usage:%s" % diskUsageRoot.percent) cosm = eeml.Pachube(API_URL, API_KEY) cosm.update([eeml.Data(0, cpuper, unit=eeml.RH())]) cosm.update([eeml.Data(1, memVir.percent, unit=eeml.RH())]) cosm.update([eeml.Data(2, memPhy.percent, unit=eeml.RH())]) cosm.update([eeml.Data(3, diskUsageRoot.percent, unit=eeml.RH())]) cosm.put() time.sleep(60)
# will set to true later when https is supported pac = eeml.Pachube(feedUrl, PACHUBE_KEY, use_https=False) # pac.update(eeml.Data(0, lnAvgUnits, minValue=0, maxValue=None, unit=eeml.Unit(name='watt', type='derivedSI', symbol='W'))) # pac.update(eeml.Data(0, lnAvgUnits, minValue=0, maxValue=lnMaxVal)) pac.update(eeml.Data(thisAdc, lnAvgUnits, minValue=0, maxValue=lnMaxVal)) try: retVal = pac.put() # print "eeml update status: "+str(retVal) # print "Sensor# ", lnSensorNum, "logged ", lnAvgUnits, " to Pachube feed: ", ApiFeedXML except Exception, e: print "TLSM.logtopachube - eeml exception: " + str(e) syslog.syslog("TLSM.logtopachube exception: eeml: " + str(e)) # update pachube with the rssi (signal strength) of the xbee sensor pac = eeml.Pachube(9982, PACHUBE_KEY, use_https=False) pac.update(eeml.Data(lnSensorNum, rssi, minValue=0, maxValue=100)) try: retVal = pac.put() # print "eeml update status: "+str(retVal) # print "Sensor# ", lnSensorNum, "logged ", rssi, " dB to a Pachube signal strength feed: ", ApiFeedXML except Exception, e: print "TLSM.logtopachube - eeml exception: " + str(e) syslog.syslog("TLSM.logtopachube exception: eeml: " + str(e)) ############################################################## # log to Sen.se # CJ, 03.12.2011, added logtosense() to send feeds to Sen.se def logtosense(lnSensorNum, lnAvgUnits, ApiFeedKey):
def main(): global n1pointer, n1temps, n2pointer, n2temps, n3pointer, n3temps n1pointer = 0 n2pointer = 0 n3pointer = 0 n1temps = [] n2temps = [] n3temps = [] def runningAverage(node, temperature, pointer, temps): #print "RunningAverage function; pointer = %d" % pointer total = 0 # initially build-up the list of temperatures if len(temps) < 4: temps.extend([temperature]) for j in range(0, (pointer + 1)): addend = float(temps[j]) total = float(addend + total) avgx = total / (pointer + 1) # the list of temperatures is full, now replace the oldest one # with a new one else: temps[pointer] = temperature for j in range(0, 4): addend = float(temps[j]) total = float(addend + total) avgx = total / 4 average = round(avgx, 2) # increment or reset pointer if pointer < 3: pointer += 1 else: pointer = 0 return (node, average, pointer, temps) def calcTemp(): # routine to calculate single temperature units unkn = rawx.pop(0).strip() # don't know what this is, drop for now place = nodeList[int( node)] # substitute node for actual place in house wholeTemp = rawx.pop(0).strip() # whole number part of temperature fracTemp = rawx.pop(0).strip() # decimal part of temperature temperature = wholeTemp + "." + fracTemp return temperature # pass this on! nodeList = 'n', 'Garage', 'MBL_Room', 'In-law_Suite', 'Node4', 'Battery_Test' # CPU monitor routine def getCPUtemperature(): res = os.popen('vcgencmd measure_temp').readline() return (res.replace("temp=", "").replace("'C\n", "")) cpuTemp = int(float(getCPUtemperature())) # COSM variables. API_KEY = 'suinLKP1uD3GCkuUN-xBmvZzSzWSAKxEcnQrdUJyTHJRND0g' FEED = 129833 API_URL = '/v2/feeds/{feednum}.xml'.format(feednum=FEED) ser = serial.Serial('/dev/ttyUSB0', 57600) while True: x = ser.readline() OKtest = re.match('OK', x) # match tests for match at beginning of string, # otherwise use search if OKtest: pac = eeml.Pachube(API_URL, API_KEY) #print "Raw data received: " + x rawx = x.split() # split on whitespace ###################### # initial parsing ack = rawx.pop(0).strip() # "OK": do nothing with it node = rawx.pop(0).strip() # Node number if node == '1': temperature = calcTemp() #new code goes here pac.update( [eeml.Data('Garage', temperature, unit=eeml.Celsius())]) try: pac.put() except CosmError, e: print('ERROR: pac.put(): {}'.format(e)) except StandardError: print('ERROR: StandardError') except: