Advertisement
Guest User

DHT11 Library

a guest
Jan 18th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.02 KB | None | 0 0
  1. import time
  2. import RPi
  3.  
  4.  
  5. class DHT11Result:
  6.     'DHT11 sensor result returned by DHT11.read() method'
  7.  
  8.     ERR_NO_ERROR = 0
  9.     ERR_MISSING_DATA = 1
  10.     ERR_CRC = 2
  11.  
  12.     error_code = ERR_NO_ERROR
  13.     temperature = -1
  14.     humidity = -1
  15.  
  16.     def __init__(self, error_code, temperature, humidity):
  17.         self.error_code = error_code
  18.         self.temperature = temperature
  19.         self.humidity = humidity
  20.  
  21.     def is_valid(self):
  22.         return self.error_code == DHT11Result.ERR_NO_ERROR
  23.  
  24.  
  25. class DHT11:
  26.     'DHT11 sensor reader class for Raspberry'
  27.  
  28.     __pin = 0
  29.  
  30.     def __init__(self, pin):
  31.         self.__pin = pin
  32.  
  33.     def read(self):
  34.         RPi.GPIO.setup(self.__pin, RPi.GPIO.OUT)
  35.  
  36.         # send initial high
  37.         self.__send_and_sleep(RPi.GPIO.HIGH, 0.05)
  38.  
  39.         # pull down to low
  40.         self.__send_and_sleep(RPi.GPIO.LOW, 0.02)
  41.  
  42.         # change to input using pull up
  43.         RPi.GPIO.setup(self.__pin, RPi.GPIO.IN, RPi.GPIO.PUD_UP)
  44.  
  45.         # collect data into an array
  46.         data = self.__collect_input()
  47.  
  48.         # parse lengths of all data pull up periods
  49.         pull_up_lengths = self.__parse_data_pull_up_lengths(data)
  50.  
  51.         # if bit count mismatch, return error (4 byte data + 1 byte checksum)
  52.         if len(pull_up_lengths) != 40:
  53.             return DHT11Result(DHT11Result.ERR_MISSING_DATA, 0, 0)
  54.  
  55.         # calculate bits from lengths of the pull up periods
  56.         bits = self.__calculate_bits(pull_up_lengths)
  57.  
  58.         # we have the bits, calculate bytes
  59.         the_bytes = self.__bits_to_bytes(bits)
  60.  
  61.         # calculate checksum and check
  62.         checksum = self.__calculate_checksum(the_bytes)
  63.         if the_bytes[4] != checksum:
  64.             return DHT11Result(DHT11Result.ERR_CRC, 0, 0)
  65.  
  66.         # ok, we have valid data, return it
  67.         return DHT11Result(DHT11Result.ERR_NO_ERROR, the_bytes[2], the_bytes[0])
  68.  
  69.     def __send_and_sleep(self, output, sleep):
  70.         RPi.GPIO.output(self.__pin, output)
  71.         time.sleep(sleep)
  72.  
  73.     def __collect_input(self):
  74.         # collect the data while unchanged found
  75.         unchanged_count = 0
  76.  
  77.         # this is used to determine where is the end of the data
  78.         max_unchanged_count = 100
  79.  
  80.         last = -1
  81.         data = []
  82.         while True:
  83.             current = RPi.GPIO.input(self.__pin)
  84.             data.append(current)
  85.             if last != current:
  86.                 unchanged_count = 0
  87.                 last = current
  88.             else:
  89.                 unchanged_count += 1
  90.                 if unchanged_count > max_unchanged_count:
  91.                     break
  92.  
  93.         return data
  94.  
  95.     def __parse_data_pull_up_lengths(self, data):
  96.         STATE_INIT_PULL_DOWN = 1
  97.         STATE_INIT_PULL_UP = 2
  98.         STATE_DATA_FIRST_PULL_DOWN = 3
  99.         STATE_DATA_PULL_UP = 4
  100.         STATE_DATA_PULL_DOWN = 5
  101.  
  102.         state = STATE_INIT_PULL_DOWN
  103.  
  104.         lengths = [] # will contain the lengths of data pull up periods
  105.         current_length = 0 # will contain the length of the previous period
  106.  
  107.         for i in range(len(data)):
  108.  
  109.             current = data[i]
  110.             current_length += 1
  111.  
  112.             if state == STATE_INIT_PULL_DOWN:
  113.                 if current == RPi.GPIO.LOW:
  114.                     # ok, we got the initial pull down
  115.                     state = STATE_INIT_PULL_UP
  116.                     continue
  117.                 else:
  118.                     continue
  119.             if state == STATE_INIT_PULL_UP:
  120.                 if current == RPi.GPIO.HIGH:
  121.                     # ok, we got the initial pull up
  122.                     state = STATE_DATA_FIRST_PULL_DOWN
  123.                     continue
  124.                 else:
  125.                     continue
  126.             if state == STATE_DATA_FIRST_PULL_DOWN:
  127.                 if current == RPi.GPIO.LOW:
  128.                     # we have the initial pull down, the next will be the data pull up
  129.                     state = STATE_DATA_PULL_UP
  130.                     continue
  131.                 else:
  132.                     continue
  133.             if state == STATE_DATA_PULL_UP:
  134.                 if current == RPi.GPIO.HIGH:
  135.                     # data pulled up, the length of this pull up will determine whether it is 0 or 1
  136.                     current_length = 0
  137.                     state = STATE_DATA_PULL_DOWN
  138.                     continue
  139.                 else:
  140.                     continue
  141.             if state == STATE_DATA_PULL_DOWN:
  142.                 if current == RPi.GPIO.LOW:
  143.                     # pulled down, we store the length of the previous pull up period
  144.                     lengths.append(current_length)
  145.                     state = STATE_DATA_PULL_UP
  146.                     continue
  147.                 else:
  148.                     continue
  149.  
  150.         return lengths
  151.  
  152.     def __calculate_bits(self, pull_up_lengths):
  153.         # find shortest and longest period
  154.         shortest_pull_up = 1000
  155.         longest_pull_up = 0
  156.  
  157.         for i in range(0, len(pull_up_lengths)):
  158.             length = pull_up_lengths[i]
  159.             if length < shortest_pull_up:
  160.                 shortest_pull_up = length
  161.             if length > longest_pull_up:
  162.                 longest_pull_up = length
  163.  
  164.         # use the halfway to determine whether the period it is long or short
  165.         halfway = shortest_pull_up + (longest_pull_up - shortest_pull_up) / 2
  166.         bits = []
  167.  
  168.         for i in range(0, len(pull_up_lengths)):
  169.             bit = False
  170.             if pull_up_lengths[i] > halfway:
  171.                 bit = True
  172.             bits.append(bit)
  173.  
  174.         return bits
  175.  
  176.     def __bits_to_bytes(self, bits):
  177.         the_bytes = []
  178.         byte = 0
  179.  
  180.         for i in range(0, len(bits)):
  181.             byte = byte << 1
  182.             if (bits[i]):
  183.                 byte = byte | 1
  184.             else:
  185.                 byte = byte | 0
  186.             if ((i + 1) % 8 == 0):
  187.                 the_bytes.append(byte)
  188.                 byte = 0
  189.  
  190.         return the_bytes
  191.  
  192.     def __calculate_checksum(self, the_bytes):
  193.         return the_bytes[0] + the_bytes[1] + the_bytes[2] + the_bytes[3] & 255
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement