Skip to content

Instantly share code, notes, and snippets.

@luanlmd
Created October 18, 2012 18:51
Show Gist options
  • Save luanlmd/3914070 to your computer and use it in GitHub Desktop.
Save luanlmd/3914070 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# based on http://www.netfluvia.org/layer8/?p=175
import mechanize
import json
import os
import serial
class Cosm:
_url_base = "http://api.cosm.com/v2/feeds/"
_feed_id = None
_version = None
## the substance of our update - list of dictionaries with keys 'id' and 'current_value'
_data = None
## the actual object we'll JSONify and send to the API endpoint
_payload = None
_opener = None
def __init__(self, feed_id, apikey):
self._version = "1.0.0"
self._feed_id = feed_id
self._opener = mechanize.build_opener()
self._opener.addheaders = [('X-PachubeApiKey',apikey)]
self._data = []
self._payload = {}
def addDatapoint(self,dp_id,dp_value):
self._data.append({'id':dp_id, 'current_value':dp_value})
def buildUpdate(self):
self._payload['version'] = self._version
self._payload['id'] = self._feed_id
self._payload['datastreams'] = self._data
def sendUpdate(self):
self.buildUpdate()
url = self._url_base + self._feed_id + "?_method=put"
try:
self._opener.open(url,json.dumps(self._payload))
except Exception as e:
print "An error occurred: %s " % e
ser = serial.Serial('/dev/ttyACM0', 9600)
ser.readline()
while 1:
data = ser.readline().split(',')
print data
c = Cosm('74872','yU_49zzUMfCNrCpdWNCqZJVds1SSAKxLWW9YUGNJL1dVMD0g')
c.addDatapoint(data[0], data[1])
c.sendUpdate()
@tonykamillo
Copy link

Cheiro de Java esses métodos em camel case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment