DHT11 sensor reading on Raspberry PI 2
I recently bought a Raspberry PI 2 and started playing with it.
At first I experimented with DHT11 digital temperature and humidity sensor using Python. I tried to read data only by reading the user manual of the sensor. I had timing issues (which I accounted for that the Raspberian is not real time operating system and secondly that I used interpreted Python), but I finally create a small library that uses bare Python (with GPIO library) and works most of the time:
https://github.com/szazo/DHT11_Python
Using e.g. ThingSpeak the data can be sent continously to the cloud and can be visualized as graph:
import RPi.GPIO as GPIO
import dht11
import time
import datetime
import urllib2
# initialize GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()
# read data using pin 14
instance = dht11.DHT11(pin = 14)
api_key = "TODO: ---ThingSpeak API KEY---"
base_url = "http://api.thingspeak.com/update?api_key=%s" % api_key
while True:
result = instance.read()
if result.is_valid():
print("Last valid input: " + str(datetime.datetime.now()))
print("Temperature: %d C" % result.temperature)
print("Humidity: %d %%" % result.humidity)
try:
url = base_url + "&field1=%s&field2=%s" % (result.temperature, result.humidity)
print(url)
f = urllib2.urlopen(url)
print f.read()
f.close()
except:
print("error")
pass
time.sleep(600)