Test release Code

Source: Internet
Author: User

<code><pre>

Import Wiringpi2 as Gpio
Owpin=8 #第8脚为1-wire Feet
def getval (Owpin):
Tl=[] #存放每个数据位的时间
Tb=[] #存放数据位
Gpio.wiringpisetup () #初始化wiringpi库
Gpio.pinmode (owpin,1) #设置针脚为输出状态
Gpio.digitalwrite (owpin,1) #输出高电平
Gpio.delay (1)
Gpio.digitalwrite (owpin,0) #拉低20ms开始指令
Gpio.delay (25)
Gpio.digitalwrite (owpin,1) #抬高20 -40us
Gpio.delaymicroseconds (20)
Gpio.pinmode (owpin,0) #设针脚为输入状态
while (Gpio.digitalread (Owpin) ==1): Pass #等待DHT11拉低管脚

For I in range: #测试每个数据周期的时间 (including 40bit data plus one send start flag
Tc=gpio.micros () #记下当前us数 (reinitialization starting from initialization, if necessary)
‘‘‘
A data cycle, including a low level, a high level, starting from the DHT11 first pull low signal line
To DHT11 send the last 50us of the low end (and then be pulled high, maintaining high levels, so
The final finish sign is always high, over 500ms)
‘‘‘
while (Gpio.digitalread (owpin) ==0):p
while (Gpio.digitalread (Owpin) ==1):
If Gpio.micros ()-tc>500: #如果超过500ms就结束了
Break
If Gpio.micros ()-tc>500: #跳出整个循环
Break
Tl.append (Gpio.micros ()-TC) #记录每个周期时间的us数, save to TL this list

# print (TL) #反注释后可打印时间列表
Tl=tl[1:] #去掉第一项, 40 data bits left
For I in TL:
If i>100: #若数据位为1, Time is 50US low +70us high level =120US
Tb.append (1)
Else
Tb.append (0) #若数据位为0, time is 50US low +25us high level =75us
#这里取大于100us就为1
# print (TB) #反注释可查看每一位状态
Return TB

def 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):
#计算每一位的状态, each word 8 bits, with this as humidity integer, humidity fractional, temperature integer, temperature decimal, 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,TL

Sh,sl,th,tl=getresult (Owpin)
Print ("Humidity:", SH,SL, "Temperature:", TH,TL)

</pre></code>

Test release Code

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.