login
|
Register
Forum
>
Beginners
Post
|
DHT22 Sensor Python Code for Orange PI Zero
看9252
|
回1
|
Favorite
phyesix
看全部
2017-3-11 16:49:42
Hi,
I bought the DHT22 sensor, but I could not get it to work in any way. What is wrong in this code block:
code:
#!/usr/bin/env python
from pyA20.gpio import gpio
from pyA20.gpio import port
from pyA20.gpio import connector
from time import sleep
def bin2dec(string_num):
return str(int(string_num, 2))
data = []
#GPIO.setmode(GPIO.BCM)
PIN = port.PA3
gpio.init()
gpio.setcfg(PIN, gpio.OUTPUT)
gpio.output(PIN,gpio.HIGH)
sleep(0.018)
gpio.output(PIN,gpio.LOW)
sleep(0.018)
#gpio.setup(PIN, gpio.IN, pull_up_down=gpio.PUD_UP)
gpio.setcfg(PIN, gpio.INPUT)
for i in range(0,500):
data.append(gpio.input(PIN))
bit_count = 0
tmp = 0
count = 0
HumidityBit = ""
TemperatureBit = ""
crc = ""
try:
while data[count] == 1:
tmp = 1
count = count + 1
for i in range(0, 32):
bit_count = 0
while data[count] == 0:
tmp = 1
count = count + 1
while data[count] == 1:
bit_count = bit_count + 1
count = count + 1
if bit_count > 3:
if i>=0 and i<8:
HumidityBit = HumidityBit + "1"
if i>=16 and i<24:
TemperatureBit = TemperatureBit + "1"
else:
if i>=0 and i<8:
HumidityBit = HumidityBit + "0"
if i>=16 and i<24:
TemperatureBit = TemperatureBit + "0"
except:
print "ERR_RANGE 1"
exit(0)
try:
for i in range(0, 8):
bit_count = 0
while data[count] == 0:
tmp = 1
count = count + 1
while data[count] == 1:
bit_count = bit_count + 1
count = count + 1
if bit_count > 3:
crc = crc + "1"
else:
crc = crc + "0"
except:
print "ERR_RANGE 2"
exit(0)
Humidity = bin2dec(HumidityBit)
Temperature = bin2dec(TemperatureBit)
if int(Humidity) + int(Temperature) - int(bin2dec(crc)) == 0:
print Humidity
print Temperature
else:
print "ERR_CRC 3"
nopnop2002
看全部
2017-3-12 05:41:45
There is DHT11-Python-library-Orange-PI
https://github.com/Vitalik-Samets/DHT11-Python-library-Orange-PI
OrangePi En
Powered by
Discuz!
X3.4
homepage
|
Simple edition
|
Touch edition
|
PC