Advertisement
Wolvenspud

DHT-11

Aug 7th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.89 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import RPi.GPIO as GPIO
  4. import time
  5. import sys
  6.  
  7. def bin2dec(string_num):
  8.     return str(int(string_num, 2))
  9.    
  10. data = []
  11. effectiveData = []
  12. bits_min=999;
  13. bits_max=0;
  14. HumidityBit = ""
  15. TemperatureBit = ""
  16. crc = ""
  17. crc_OK = False;
  18. Humidity = 0
  19. Temperature = 0
  20.  
  21.  
  22. pin=7
  23.  
  24.  
  25. GPIO.setmode(GPIO.BOARD)
  26.  
  27.  
  28. def pullData():
  29. #{{{ Pull data from GPIO
  30.     global data
  31.     global effectiveData
  32.     global pin
  33.    
  34.     data = []
  35.     effectiveData = []
  36.    
  37.     GPIO.setup(pin,GPIO.OUT)
  38.     GPIO.output(pin,GPIO.HIGH)
  39.     time.sleep(0.025)
  40.     GPIO.output(pin,GPIO.LOW)
  41.     time.sleep(0.14)
  42.    
  43.     GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  44.    
  45.     for i in range(0,1000):
  46.         data.append(GPIO.input(pin))
  47.  
  48.     """  
  49.    for i in range(0,len(data)):
  50.        print "%d" % data[i],
  51.    print
  52.    """
  53.    
  54. #}}}
  55.  
  56.  
  57. def analyzeData():
  58. #{{{ Analyze data
  59.  
  60. #{{{ Add HI (2x8)x3 bits to array
  61.    
  62.     seek=0;
  63.     bits_min=9999;
  64.     bits_max=0;
  65.  
  66.     global HumidityBit
  67.     global TemperatureBit
  68.     global crc
  69.     global Humidity
  70.     global Temperature
  71.  
  72.     HumidityBit = ""
  73.     TemperatureBit = ""
  74.     crc = ""
  75.    
  76.     """
  77.    Snip off the first bit - it simply says "Hello, I got your request, will send you temperature and humidity information along with checksum shortly"
  78.    """
  79.     while(seek < len(data) and data[seek] == 0):
  80.         seek+=1;
  81.    
  82.     while(seek < len(data) and data[seek] == 1):
  83.          seek+=1;
  84.    
  85.     """
  86.    Extract all HIGH bits' blocks. Add each block as separate item in data[]
  87.    """
  88.     for i in range(0, 40):
  89.        
  90.         buffer = "";
  91.        
  92.         while(seek < len(data) and data[seek] == 0):
  93.             seek+=1;
  94.        
  95.        
  96.         while(seek < len(data) and data[seek] == 1):
  97.             seek+=1;
  98.             buffer += "1";
  99.        
  100.         """
  101.        Find the longest and the shortest block of HIGHs. Average of those two will distinct whether block represents '0' (shorter than avg) or '1' (longer than avg)
  102.        """
  103.        
  104.         if (len(buffer) < bits_min):
  105.             bits_min = len(buffer)
  106.    
  107.         if (len(buffer) > bits_max):
  108.             bits_max = len(buffer)
  109.        
  110.         effectiveData.append(buffer);
  111.         #print "%s " % buffer
  112.            
  113. #}}}
  114.  
  115.  
  116.  
  117. #{{{ Make effectiveData smaller
  118.    
  119.     """
  120.    Replace blocks of HIs with either '1' or '0' depending on block length
  121.    """
  122.     for i in range(0, len(effectiveData)):
  123.         if (len(effectiveData[i]) < ((bits_max + bits_min)/2)):
  124.             effectiveData[i] = "0";
  125.         else:
  126.             effectiveData[i] = "1";
  127.    
  128.         #print "%s " % effectiveData[i],
  129.    # print
  130.    
  131.    
  132. #}}}
  133.  
  134.  
  135. #{{{ Extract Humidity and Temperature values
  136.  
  137.     for i in range(0, 8):
  138.         HumidityBit += str(effectiveData[i]);
  139.    
  140.     for i in range(16, 24):
  141.         TemperatureBit += str(effectiveData[i]);
  142.    
  143.    
  144.     for i in range(32, 40):
  145.         crc += str(effectiveData[i]);
  146.    
  147.     Humidity = bin2dec(HumidityBit)
  148.     Temperature = bin2dec(TemperatureBit)
  149.  
  150.     #print "HumidityBit=%s, TemperatureBit=%s, crc=%s" % (HumidityBit, TemperatureBit, crc)
  151.  
  152. #}}}
  153.  
  154. #}}}
  155.  
  156.  
  157. #{{{ Check CRC
  158. def isDataValid():
  159.    
  160.     global Humidity
  161.     global Temperature
  162.     global crc
  163.    
  164.     #print "isDataValid(): H=%d, T=%d, crc=%d"% (int(Humidity), int(Temperature), int(bin2dec(crc)))
  165.     if int(Humidity) + int(Temperature) == int(bin2dec(crc)):
  166.         return True;
  167.     else:
  168.         return False;
  169. #}}}
  170.  
  171.  
  172. #{{{ Print data
  173. def printData():
  174.    global Humidity
  175.    global Temperature
  176.    
  177.    print "H: "+Humidity
  178.    print "T: "+Temperature
  179. #}}}
  180.  
  181.  
  182.  
  183. #{{{ Main loop
  184.  
  185. while (not crc_OK):
  186.     pullData();
  187.     analyzeData();
  188.     if (isDataValid()):
  189.         crc_OK=True;
  190.         print "\r",
  191.         printData();
  192.     else:
  193.         sys.stderr.write(".")
  194.         time.sleep(2);
  195. #}}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement