def __init__(self, api_key, feed, serial_port, delay=5): """Setup hardware and remote feeds. @param api_key This is the key from Cosm.com @param feed This is the feed ID from Cosm.com @param serial_port Where to read data from @param delay Number of seconds to wait between read/sends. """ self.api_key = api_key self.feed = feed self.serial_port = serial_port self.delay = delay self.arduino = Arduino(self.serial_port) self.iterator = util.Iterator(self.arduino) self.iterator.start() self.cosm = CosmSender(self.api_key, self.feed, {}, cacheSize=0) self.streams = {}
# We're accessing the 1-wire bus directly from python but # if you want to use owserver: # ow.init( 'localhost:3030' ) # /opt/owfs/bin/owserver -p 3030 -u -r sensors = ow.Sensor("/").sensorList() dataStreamDefaults = { "unit": { "type" : "derivedSI", "label" : "degree Celsius", "symbol": u"\u00B0C"} } c = CosmSender(API_KEY, FEED, dataStreamDefaults, cacheSize=3) # We're only interested in temperature sensors so remove # any 1-wire devices which aren't temperature sensors for sensor in sensors[:]: if sensor.type != 'DS18B20': sensors.remove( sensor ) # Print column headers for sensor in sensors: print sensor.r_address + "\t", print "\n", # Print temperatures while True: print int(time.time()), "\t",
######################################### # initialise serial port ser = serial.Serial(SERIAL_PORT, 57600) ser.flushInput() # Setup CosmSender dataStreamDefaults = { "min_value" : "0.0", "unit": { "type" : "derivedSI", "label" : "watt", "symbol": "W" } } c = CosmSender(API_KEY, FEED, dataStreamDefaults, cacheSize=10) # continually pull data from current cost, write to file, print to stout and send to Cosm while True: # Get data from Current Cost Envi sensor, watts = pullFromCurrentCost() # open data file try: datafile = open(FILENAME, 'a+') except Exception, e: sys.stderr.write("""ERROR: Failed to open data file " + FILENAME + Has it been configured correctly in config.xml?""") raise # Print data to the file
class AlternatePowerReporter(object): """Read a load of things, send to cosm.""" def __init__(self, api_key, feed, serial_port, delay=5): """Setup hardware and remote feeds. @param api_key This is the key from Cosm.com @param feed This is the feed ID from Cosm.com @param serial_port Where to read data from @param delay Number of seconds to wait between read/sends. """ self.api_key = api_key self.feed = feed self.serial_port = serial_port self.delay = delay self.arduino = Arduino(self.serial_port) self.iterator = util.Iterator(self.arduino) self.iterator.start() self.cosm = CosmSender(self.api_key, self.feed, {}, cacheSize=0) self.streams = {} def add_sensor(self, pin_string, feed_id, fn): """Add a sensor (on the arduino) to our read list. :param pin_string: e.g. a:0:i (see pyFirmata docs) :param feed_id: e.g. solar_current (datastream on cosm) :param fn: the function to run on the pin readout before sending """ self.streams[feed_id] = { 'pin': self.arduino.get_pin(pin_string), 'fn': fn, } def read_sensors(self): """Read all of the pins we know about, return them.""" results = {} for i in range(self.delay * 1000): for stream, params in self.streams.items(): val = params['pin'].read() if val: results.setdefault(stream, {'sum': 0, 'reads': 0}) results[stream]['sum'] += params['fn'](val) results[stream]['reads'] += 1 time.sleep(1 / 1000.0) for stream, params in results.items(): results[stream]['value'] = ( results[stream]['sum'] / float(results[stream]['reads'])) log.debug("Data is %s" % results) return results def send_data(self, data): """Send stuff to cosm.""" for stream, params in data.items(): log.debug("Sending data for %s" % stream) self.cosm.sendData(stream, str(params['value'])) def run(self): while True: data = self.read_sensors() self.send_data(data) def test_setup(self): """Check that the streams defined are available etc.""" for stream in self.streams: if not stream in self.cosm.cache: log.warn( "Stream '%s' not set up on Cosm, will be created" % ( stream))
# initialise serial port ser = serial.Serial(SERIAL_PORT, 57600) ser.flushInput() # Setup CosmSender dataStreamDefaults = { "min_value": "0.0", "unit": { "type": "derivedSI", "label": "watt", "symbol": "W" } } c = CosmSender(API_KEY, FEED, dataStreamDefaults, cacheSize=10) # continually pull data from current cost, write to file, print to stout and send to Cosm while True: # Get data from Current Cost Envi sensor, watts = pullFromCurrentCost() # open data file try: datafile = open(FILENAME, 'a+') except Exception, e: sys.stderr.write("""ERROR: Failed to open data file " + FILENAME + Has it been configured correctly in config.xml?""") raise # Print data to the file