Raspberry Pi advanced GPIO library, wiringpi2 for python using notes (4) practice DHT11 decoding, gpiodht11

Source: Internet
Author: User

Raspberry Pi advanced GPIO library, wiringpi2 for python using notes (4) practice DHT11 decoding, gpiodht11

DHT11 is a temperature and humidity sensor with calibrated digital signal output. Precision humidity +-5% RH, temperature +-2 ℃, range humidity 20-90% RH, temperature 0 ~ 50 deg C.

The encapsulated modules I bought contain the pull-up resistor. After checking the Raspberry Pi, the gray, purple, and blue modules represent data, 3.3 V, and 0 V respectively, received 3, 1, and 10 of Raspberry Pi, corresponding to PIN8, 3.3 V, 0 V respectively.

Import wiringpi2 as gpioowpin = 8 # 8th feet are 1-wire feet tl = [] # Time for storing each data bit gpio. wiringPiSetup () # initialize the wiringpi library gpio. pinMode (owpin, 1) # Set the pins to output gpio. digitalWrite (owpin, 1) # output high-level gpio. delay (1) ### sending the start command, requiring DHT11 to transmit the data gpio. digitalWrite (owpin, 0) # lower by 25 ms to start the command gpio. delay (25) gpio. digitalWrite (owpin, 1) # output high level, start the command to end gpio. pinMode (owpin, 0) # Set the pin to the input status ### start sending the command, set the pin to a high level, and wait for DHT11 to pull down the pin. While (gpio. digitalRead (owpin) = 1): pass # If the PIN is always 1, keep waiting. ### If it is pulled low, it indicates that the transmission starts. The response signal + 40-Bit Data + the ending sign contains 42 BITs ### the following cycle for 45 times, deliberately cyclically view the results several times. For I in range (45): # test the time of each data cycle (including 40-bit data plus a sending start sign tc = gpio. micros () # Write down the current number of us (from the beginning of initialization, re-initialization if necessary) ''' a data cycle, including a low level, a high level, starting from the first low signal line of DHT11 to the low level end of the last 50us sent by DHT11 (then pulled high and maintained high, so the final completion mark is always high, over 500 ms) '''while (gpio. digitalRead (owpin) = 0): pass # a bit of data is composed of a low-level while (gpio. digitalRead (owpin) = 1): # Add a high level to form if gpio. micros ()-tc> 500: # if the cycle is over us, the break # will be pulled to a high level by the pull-up resistor after the transmission is over to prevent entering the dead cycle tl. append (gpio. micros ()-tc) # record the number of us in each cycle time, save to the tl list print (tl) # print the result

If there is a detailed explanation in the program, I will not go into details. Here I will post the execution result here:

[116, 68, 74, 67, 124, 64, 120, 120, 76, 71, 73, 73, 73, 73, 73, 73, 74, 67, 73, 73, 123, 120, 70, 72, 73, 79, 71, 73, 73, 74, 73, 74, 73, 70, 73, 122, 74, 117, 120, 119, 70, 508, 506, 512, 512]

Now let's analyze the result:

The first 116us is the response signal. According to the instructions, it should be 160us. It is estimated that the DHT11 in my hand is not very standard. The next 40 (from 68us to the last 70us) is 40 bits of data, because no data is received at the end, it exceeds US (more than us will jump out of the loop to prevent endless loops)

Regardless of the first 116us, if the value of 60-80 us is about 1 in 0,120, the received data is:

Humidity integer humidity decimal temperature integer temperature decimal check
0001 0110  0000 0000  0001 1000  0000 0000  0010 1110
16+4+2=22  0          16+8=24    0          32+8+4+2=46

The read result is humidity 22%, temperature 24 degrees, checksum of 22 + 24 = 46, read successful.

I added some data processing and additional code for failed read. The final code is as follows:

Import wiringpi2 as gpioowpin = 8 #1-wire pin def getval (owpin): tl = [] # Time for storing each data bit tb = [] # Time for storing the data bit gpio. wiringPiSetup () # initialize the wiringpi library gpio. pinMode (owpin, 1) # Set the pins to output gpio. digitalWrite (owpin, 1) # output high-level gpio. delay (1) gpio. digitalWrite (owpin, 0) # The command gpio starts to be pulled down for 20 ms. delay (25) gpio. digitalWrite (owpin, 1) # Raise 20-40us gpio. delayMicroseconds (20) gpio. pinMode (owpin, 0) # Set the pin to the input state while (gpio. digitalRead (owpin) = 1): pass # Wait for DHT11 to lower the pin for I in range (45): # test the time of each data cycle (including 40-bit data plus a sending start sign tc = gpio. micros () # Write down the current number of us (from the beginning of initialization, re-initialization if necessary) ''' a data cycle, including a low level, a high level, starting from the first low signal line of DHT11 to the low level end of the last 50us sent by DHT11 (then pulled high and maintained high, so the final completion mark is always high, over 500 ms) '''while (gpio. digitalRead (owpin) = 0): pass while (gpio. digitalRead (owpin) = 1): if gpio. micros ()-tc> 500: # if it exceeds ms, the break if gpio is terminated. micros ()-tc> 500: # Jump out of the entire cycle break tl. append (gpio. micros () -Tc) # record the number of us in each cycle time and save it to the tl list # print (tl) # The time list can be printed after reverse annotation tl = tl [1:] # Remove the first item, the remaining 40 data bits for I in tl: if I> 100: # if the data bit is 1, the time is 50us LOW LEVEL + 70us High Level = 120us tb. append (1) else: tb. append (0) # If the data bit is 0, the time is 50us low level + 25us High Level = 75us # Here, if the value is greater than 100us, it is 1 # print (tb) # reverse comments: return tbdef GetResult (owpin): for I in range (10): SH = 0; SL = 0; TH = 0; TL = 0; C = 0 result = getval (owpin) # print (len (result) if len (result) = 40: for I in range (8 ): # Calculate the shape of each bit State. Each word is 8 bits. This is a humidity integer, a humidity fraction, a temperature integer, a temperature fraction, and a checksum SH * = 2; SH + = result [I] SL * = 2; SL + = result [I + 8] TH * = 2; TH + = result [I + 16] TL * = 2; TL + = result [I + 24] C * = 2; C + = result [I + 32] if (SH + SL + TH + TL) % 256) = C and C! = 0: break else: print ("Read Sucess, But checksum error! Retrying ") else: print (" Read failer! Retrying ") gpio. delay (200) return SH, SL, TH, TLSH, SL, TH, TL = GetResult (owpin) print ("humidity:", SH, SL, "temperature :", TH, TL)

The running result is as follows:

Humidity: 20 0 temperature: 24 0

Related Article

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.