#!/usr/bin/env python # /home/pi/python_carriots/arduinostream_carriots2.py # 18.7.2015 KLL engineering # CC BY SA # blog: http://kll.engineering-news.org/kllfusion01/articles.php?article_id=93 #import serial from urllib2 import urlopen, Request import json import time # Full Privileges Apikey APIKEY="a...f" #Id developer DEVICE="defaultDevice@username.username" class Client (object): api_url = "http://api.carriots.com/streams" def __init__ (self, api_key = None, client_type = 'json'): self.client_type = client_type self.api_key = api_key self.content_type = "application/vnd.carriots.api.v2+%s" % (self.client_type) self.headers = {'User-Agent': 'Raspberry-Carriots', 'Content-Type': self.content_type, 'Accept': self.content_type, 'Carriots.apikey': self.api_key} self.data=None self.response=None def send (self, data): self.data = json.dumps(data) request = Request(Client.api_url, self.data, self.headers) self.response = urlopen(request) return self.response def main(): client_carriots = Client (APIKEY) foldtime = time.time() # seconds as float fnewtime = time.time() fstore = 10.0 # sec ipv = 0; # read number later from arduino Ain will be 0 .. 1023, 32 bit int while True: fnewtime = time.time() fdtime = fnewtime-foldtime if fdtime >= fstore: # only store every "fstore" seconds foldtime = fnewtime ipv += 1 if ipv > 1023: ipv = 0 print ipv data = {"protocol":"v2","device":DEVICE,"at":"now","data":{"A0":ipv}} print data carriots_response=client_carriots.send(data) print carriots_response.read() if __name__=='__main__': main()