Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

thingspeak upload

  • Loading branch information...
commit e657f50e225522a9cec38616a38d95c6696459be 1 parent 5382f8b
@ericbarch authored
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,6 +57,9 @@ 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)
@@ -45,6 +67,10 @@ def main():
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()
Please sign in to comment.
Something went wrong with that request. Please try again.