Raspberry Pie 3 B + read temperature and humidity sensor DHT11

Source: Internet
Author: User
# encoding=utf-8 # Power By:funco_ small Wind import rpi.gpio as GPIO import Time # delay function def delay (i): while i:i = 1 # Early init dht11 Connection pin # DHT11_PIN-DHT11 connection PIN number def init_dht11 (dht11_pin): # Output mode initial state to High level Gpio.setup (Dht11_pin, GPIO. Out) Gpio.output (Dht11_pin, 1) # used to get the pin number of the # DHT11_PIN-DHT11 connection # return two tuples [humidity, temperature] def get_dht11 (dht11_pin): buff=[0,0
	, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] GPIO.output (dht11_pin,0) Time.sleep (0.02) # pulls down 20ms gpio.output (dht11_pin,1) Gpio.setup (Dht11_pin,gpio. IN) # Here needs to be pulled high 20-40us, but changing mode requires 50US, so do not invoke delay while not gpio.input (Dht11_pin): # Detect return signal detects high level end of Revelation signal while GPIO.I Nput (Dht11_pin): # The High level that detects the revelation signal is cycled pass i=40 while I:start=time.time () *1000000 # time to start the strict timing cycle i-=1 while n OT Gpio.input (dht11_pin): pass-while-Gpio.input (dht11_pin): Pass Buff[i]=time.time () *1000000-start# in order to strictly sequence each measurement It is not immediately processed to store Gpio.setup first (Dht11_pin,gpio. Out) # Read end reset pin Gpio.output (dht11_pin,1) # print "Buff-", Buff # starts processing data for I in range (len (Buff)): # Converts time to 0 1 if buff[i]>100: # When the top test is the time of the whole bit # therefore is compared with 100 more than 100 for 1 (bit cycle low level 50US) buff[i]=1 else:buff[i]=0 # print "After-", Buff i=40 hum_int=0 whi Le i>32: # humidity Integer part i-=1 hum_int<<=1 hum_int+=buff[i] # print "Hum-", Hum_int tmp_int=0 i=24 while I &GT;16: # Temperature integral part i-=1 tmp_int<<=1 Tmp_int+=buff[i] # print "tmp-", Tmp_int return [Hum_int,tmp_int] GPI O.setmode (GPIO. BOARD) init_dht11 (7) Print get_dht11 (7) Gpio.cleanup () # Note # If continuous measurement can be without delay but continuous measurement, it is recommended to call GET_DHT11 (Dht11_pin) at intervals of more than 0.2s each time. Fetch data (raspberry instability) # DHT11 Although there are 40 digits of actual temperature and humidity, the number of decimal readings is 0 # If the program does not read correctly, can consider whether the initial part of the delay is inaccurate, you can refer to the annotation and the actual environment measurement results adjust the delay # Program test environment for $ raspberry Pie 3 Generation b+$ $ python2.7.9 $Raspbian Gnu/linux 8$

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.