Permalink
Browse files

thingspeak upload

  • Loading branch information...
1 parent 5382f8b commit e657f50e225522a9cec38616a38d95c6696459be @ericbarch committed Feb 9, 2014
Showing with 27 additions and 1 deletion.
  1. +27 −1 rad.py
View
28 rad.py
@@ -1,11 +1,31 @@
#!/usr/bin/env python
import time
+import httplib, urllib
import RPi.GPIO as GPIO
waitUntil = time.time() + 1
events = []
+def upload():
+ cpm = str(float(len(events))/float(5))
+ params = urllib.urlencode({'field1': cpm})
+ headers = {
+ "X-THINGSPEAKAPIKEY": "YOUR-CHANNEL-KEY",
+ "Content-Type": "application/x-www-form-urlencoded",
+ "Content-Length": str(len(params))
+ }
+ conn = httplib.HTTPConnection("api.thingspeak.com")
+ conn.request("POST", "/update", params, headers)
+ response = conn.getresponse()
+ if response.status == 200:
+ print 'upload'
+ else:
+ print 'upload fail'
+ data = response.read()
+ conn.close()
+
+
def radiationEventHandler (pin):
if time.time() >= waitUntil:
events.append(time.time())
@@ -20,7 +40,6 @@ def noiseEventHandler (pin):
except:
pass
-
# main function
def main():
# tell the GPIO module that we want to use
@@ -38,13 +57,20 @@ def main():
GPIO.add_event_detect(18, GPIO.FALLING)
GPIO.add_event_callback(18, noiseEventHandler)
+ # wait 5 mins before posting to thingspeak (data needs to stabilize)
+ loops = -270
+
# sleepy time
while True:
time.sleep(1)
for event in events:
if event < time.time() - 300:
events.remove(event)
print "cpm: " + str(float(len(events))/float(5))
+ loops = loops + 1
+ if (loops > 30):
+ loops = 0
+ upload()
# done
GPIO.cleanup()

0 comments on commit e657f50

Please sign in to comment.