Skip to content

Instantly share code, notes, and snippets.

@udovicic
Last active September 5, 2016 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save udovicic/1b3b051b20d237f025089ffa98c2b488 to your computer and use it in GitHub Desktop.
Save udovicic/1b3b051b20d237f025089ffa98c2b488 to your computer and use it in GitHub Desktop.
DHT22 sensors readings using Adafruit Python DHT Sensor Library
#!/usr/bin/python
# Requires Adafruit Python DHT Sensor Library
# https://github.com/adafruit/Adafruit_Python_DHT
# git@github.com:adafruit/Adafruit_Python_DHT.git
# Measures temperature 5 times, and sends average to thingspeak.
# First measurement is discarded.
# It is intended to be executed by cron in background
import Adafruit_DHT
import httplib, urllib
# Configuration
sensor = Adafruit_DHT.DHT22
pin = 22
# Measurement
humidity = 0.0
temperature = 0.0
for i in range(0,6):
h, t = Adafruit_DHT.read_retry(sensor, pin)
if i == 0:
continue
humidity = humidity + h
temperature = temperature + t
humidity = humidity/5
temperature = temperature/5
# Submit through API
params = urllib.urlencode({'field1': temperature, 'field2': humidity, 'key':'xxx'})
headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
conn = httplib.HTTPConnection("api.thingspeak.com:80")
try:
conn.request("POST", "/update", params, headers)
response = conn.getresponse()
data = response.read()
conn.close()
except:
print "connection failed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment