Skip to content

Instantly share code, notes, and snippets.

@starstuck
Created October 30, 2015 12:14
Show Gist options
  • Save starstuck/347dc83bf0d99ee2cf78 to your computer and use it in GitHub Desktop.
Save starstuck/347dc83bf0d99ee2cf78 to your computer and use it in GitHub Desktop.
Read humidity and temperature from DHT11 sensor connected to RaspbberyPi
#!/usr/bin/env python
# -*- coding: utf8 -*-
#
# Read humidity and temperature from DHT11 sensor connected to RaspberryPi.
#
# DHT11 protocol depends on exact timing. Sometimes linux kernel may evict your
# process, or GC can cause program to miss some of a bits from transmission. In
# that case following application will raise an error and you can try to run it
# again
#
#
# The MIT License (MIT)
# ---------------------
#
# Copyright (c) 2015 Dataloop.IO
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import RPi.GPIO as GPIO
import time
CHANNEL = 4 # BMC pin number
bits = [1] * 41
def read_bit():
while GPIO.input(CHANNEL) == 0: pass
start = time.time()
delta = 0.0
while GPIO.input(CHANNEL) == 1:
delta = time.time() - start
if delta > 0.000200:
raise Exception("Timeout on reciving bit of data")
return (delta >= 0.000040) and 1 or 0
def bits_to_int(bits):
binary_string = ("").join([str(x) for x in bits])
return int(binary_string, 2)
# Wake up DHT11 and initialize transmission
GPIO.setmode(GPIO.BCM)
GPIO.setup(CHANNEL, GPIO.OUT)
GPIO.output(CHANNEL, GPIO.HIGH)
time.sleep(0.025)
GPIO.output(CHANNEL, GPIO.LOW)
time.sleep(0.02)
# Read 41 bits, with one extra
GPIO.setup(CHANNEL, GPIO.IN, pull_up_down=GPIO.PUD_UP)
for i in range(0, len(bits)):
bits[i] = read_bit()
# First bit should be always, just a sigh DHT has started
if bits[0] != 1:
raise Exception("Invalid transmission beginning")
bits = bits[1:]
decoded = [bits_to_int(bits[(i * 8):((i + 1)* 8)]) for i in range(0, len(bits)/8)]
cksum = 0
for i in range(0, 4):
cksum += decoded[i]
if decoded[4] != cksum % 256:
raise Exception("Invalid check sum")
print "Relative Humidity: %d%% (±5%%)" % (decoded[0])
print "Temperature: %d°C (±2°C)" % (decoded[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment