-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlog.py
32 lines (27 loc) · 1.02 KB
/
log.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Connect Arduino to network with RESTduino sketch
# https://github.com/jjg/RESTduino
import httplib, urllib
import json
from Modifiers import *
arduino = httplib.HTTPConnection("192.168.1.40")
# load your API key into a JSON file 'apikey.js'
# and specify the absolute path if running from Cron
f = open('/root/dev/RestduinoThingspeak/apikey.js', 'r')
jsonObj = json.loads(f.read())
apikey = jsonObj['apikey']
try:
arduino.request("GET","/a0")
response = arduino.getresponse()
print "Arduino response:" , response.status, response.reason
jsonObj = json.loads(response.read())
a0 = jsonObj['a0']
print "a0:", modify(a0)
params = urllib.urlencode({'field1': modify(a0),'key':apikey})
headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
conn = httplib.HTTPConnection("api.thingspeak.com:80")
conn.request("POST", "/update", params, headers)
ts_response = conn.getresponse()
print "Thingspeak Response:", ts_response.status, ts_response.reason
conn.close
except:
print "connection failed!"