Skip to content

Instantly share code, notes, and snippets.

@mmajewsk
Created September 9, 2015 12:06
Show Gist options
  • Save mmajewsk/feb02c00aaf411323674 to your computer and use it in GitHub Desktop.
Save mmajewsk/feb02c00aaf411323674 to your computer and use it in GitHub Desktop.
Part of the DHT11 python driver based on http://www.uugear.com/portfolio/dht11-humidity-temperature-sensor-module/# It has problem with proper response time, and few others, better use adafruit driver
import RPi.GPIO as GPIO
import time
from datetime import datetime
def bin2dec(string_num): # define a function to convert a binary number to a decimal.
return str(int(string_num, 2)) # return the string representing the integer value of the string passed to this function in base 2 (binary)
class Sensor(object):
def __init__(self):
self._reset_data()
def _reset_data(self):
self.data = []
self.total_bits = 0
self.count = 0
self.humidity_bit = ""
self.temperature_bit = ""
self.crc = ""
def _initiate_sensor_readout(self):
GPIO.setmode(GPIO.BCM) # use the Broadcom numbers instead of the WiringPi numbers
GPIO.setup(4,GPIO.OUT) # Set it as an output so that we can:
GPIO.output(4,GPIO.HIGH) # write a 1
time.sleep(0.025) # for 25 ms
GPIO.output(4,GPIO.LOW) # then write a 0
time.sleep(0.02) # for 20 ms.
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Change the pin to read mode, with a pullup resistor
def _check_crc(self):
try:
for i in range(0, 8): # do this 9 times
self.total_bits = 0 # reset the bit self.counter
while self.data[self.count] == 0: # as long as a 0 was read
self.count = self.count + 1 # move on to the next bit
while self.data[self.count] == 1: # as long as a 1 was read
self.total_bits = self.total_bits + 1 # self.count how many 1s
self.count = self.count + 1 # move on to the next bit
if self.total_bits > 3: # if there were at least 3 * 1-bits
self.crc = self.crc + "1" # add a 1 to the self.crc (Cyclic redundancy check) bitstring
else: # if there were less than 3* 1-bits
self.crc = self.crc + "0" # add a 0 to the self.crc bitstring
except IndexError:
raise BitDataRangeError
else:
self.Humidity = bin2dec(self.humidity_bit) # convert the binary bitstring to a decimal variable for humidity
self.Temperature = bin2dec(self.temperature_bit)
if int(self.Humidity) + int(self.Temperature) - int(bin2dec(self.crc)) != 0:
raise BitDataCrcError
def _pull_data(self):
for i in range(0,500): # 501 times
self.data.append(GPIO.input(4)) # read a bit from the GPIO, as fast as possible (no wait)
#with open('dump.txt','w') as f:
# f.write(str(self.data)) 36 22
def _process_readout(self):
try:
while self.data[self.count] == 1: # as long as you read a 1
self.count = self.count + 1 # self.count how many 1s have been read
for i in range(0, 32): # do this 33 times
self.total_bits = 0 # reset the bit self.count each time
while self.data[self.count] == 0: # as long as a 0 is read
self.count = self.count + 1 # move on to the next bit
# opnening [1, 0, 0, 0, 0, (1,) 0, 0, 0, 0...
while self.data[self.count] == 1: # as long as a 1 is read
self.total_bits = self.total_bits + 1 # self.count how many 1s in a row
self.count = self.count + 1 # move on to the next bit
if self.total_bits > 3: # if there were mote than 3 * 1-bits in a row
if i>=0 and i<8: # if we're in the first byte
self.humidity_bit = self.humidity_bit + "1" # append a 1 to the humidity bitstring
if i>=16 and i<24: # if we're in the 3rd byte
self.temperature_bit = self.temperature_bit + "1" # add a 1 to the temperature bitstring
else: # if there weren't at least 3 * 1-bits
if i>=0 and i<8: # if we're in the first byte
self.humidity_bit = self.humidity_bit + "0" # append a 0 to the humidity bitstring
if i>=16 and i<24: # if we're in the 3rd byte
self.temperature_bit = self.temperature_bit + "0" # append a 0 to the temperature bitstring
except IndexError:
raise BitDataRangeError
def read(self):
self._reset_data
self._initiate_sensor_readout()
try:
self._pull_data()
self._process_readout()
self._check_crc()
except (BitDataCrcError, BitDataRangeError):
self.Humidity = None
self.Temperature = None
self.readout_time = datetime.now()
return self.Humidity, self.Temperature, self.readout_time
class BitDataRangeError(Exception):
pass
class BitDataCrcError(Exception):
pass
# convert the binary bitstring to a decimal variable for temperature
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment