login|Register
Forum > Beginners
Post|
看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:

  1. #!/usr/bin/env python
  2. from pyA20.gpio import gpio
  3. from pyA20.gpio import port
  4. from pyA20.gpio import connector
  5. from time import sleep

  6. def bin2dec(string_num):
  7.     return str(int(string_num, 2))
  8.    
  9. data = []

  10. #GPIO.setmode(GPIO.BCM)
  11. PIN = port.PA3

  12. gpio.init()
  13. gpio.setcfg(PIN, gpio.OUTPUT)

  14. gpio.output(PIN,gpio.HIGH)
  15. sleep(0.018)
  16. gpio.output(PIN,gpio.LOW)
  17. sleep(0.018)

  18. #gpio.setup(PIN, gpio.IN, pull_up_down=gpio.PUD_UP)
  19. gpio.setcfg(PIN, gpio.INPUT)

  20. for i in range(0,500):
  21.     data.append(gpio.input(PIN))
  22.    
  23.    
  24. bit_count = 0
  25. tmp = 0
  26. count = 0
  27. HumidityBit = ""
  28. TemperatureBit = ""
  29. crc = ""


  30. try:
  31.    while data[count] == 1:
  32.       tmp = 1
  33.       count = count + 1
  34.         

  35.    for i in range(0, 32):
  36.       bit_count = 0
  37.       
  38.       while data[count] == 0:
  39.          tmp = 1
  40.          count = count + 1

  41.       while data[count] == 1:
  42.          bit_count = bit_count + 1
  43.          count = count + 1

  44.       if bit_count > 3:
  45.          if i>=0 and i<8:
  46.             HumidityBit = HumidityBit + "1"
  47.          if i>=16 and i<24:
  48.             TemperatureBit = TemperatureBit + "1"
  49.       else:
  50.          if i>=0 and i<8:
  51.             HumidityBit = HumidityBit + "0"
  52.          if i>=16 and i<24:
  53.             TemperatureBit = TemperatureBit + "0"
  54.             
  55. except:
  56.    print "ERR_RANGE 1"
  57.    exit(0)

  58.    
  59. try:
  60.    for i in range(0, 8):
  61.       bit_count = 0
  62.       
  63.       while data[count] == 0:
  64.          tmp = 1
  65.          count = count + 1

  66.       while data[count] == 1:
  67.          bit_count = bit_count + 1
  68.          count = count + 1

  69.       if bit_count > 3:
  70.          crc = crc + "1"
  71.       else:
  72.          crc = crc + "0"
  73. except:
  74.    print "ERR_RANGE 2"
  75.    exit(0)
  76.       
  77.       
  78. Humidity = bin2dec(HumidityBit)
  79. Temperature = bin2dec(TemperatureBit)

  80. if int(Humidity) + int(Temperature) - int(bin2dec(crc)) == 0:
  81.    print Humidity
  82.    print Temperature
  83. else:
  84.    print "ERR_CRC 3"



nopnop2002 看全部
2017-3-12 05:41:45

OrangePi En

Powered by Discuz! X3.4

homepage|Simple edition|Touch edition|PC