Python GPIO code for DHT 11 temperature sensor crashes in PI 2

I ran into problems related to the DHT 11 temperature sensor in PI 2 with Python2.7 GPIO 0.5.11. I mean the http://www.uugear.com/portfolio/dht11-humidity-temperature-sensor-module/ sample code.

The same code works fine on PI 1 B +. In PI 2, I get the message "ERR_RANGE" as an Error. I tried to debug the problem, it looks like the data read by @GPIO pin 4 needs to be increased.

After increasing the value of reading data to 2000, the return value of temperature and humidity is 255. Has anyone encountered a problem, help me on how to solve it.

Thanks Bharadvaj

+6
source share
3 answers

You can also check out the following small library. It depends only on the GPIO module:

https://github.com/szazo/DHT11_Python

Example:

import RPi.GPIO as GPIO import dht11 # initialize GPIO GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.cleanup() # read data using pin 14 instance = dht11.DHT11(pin = 14) result = instance.read() if result.is_valid(): print("Temperature: %d C" % result.temperature) print("Humidity: %d %%" % result.humidity) else: print("Error: %d" % result.error_code) 
+11
source

Additional information may help solve your problem. I have the same sensor as you.

I followed this guide: https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/software-install-updated

 git clone https://github.com/adafruit/Adafruit_Python_DHT.git cd Adafruit_Python_DHT sudo apt-get update sudo apt-get install build-essential python-dev sudo python setup.py install 

And this is my test python script:

 #!/usr/bin/python import sys import Adafruit_DHT humidity, temperature = Adafruit_DHT.read_retry(11, 4) if humidity is not None and temperature is not None: print 'Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity) else: print 'Failed to get reading. Try again!' 

Save it, for example, dht_test.py, Chmod: sudo chmod a + x dht_test.py and run it as sudo: sudo./dht_test.py Perhaps this will help you.

+2
source

The following code works on the Pi 2 Model B:

https://github.com/netikras/r-pi_DHT11/blob/master/dht11.py

+1
source

Source: https://habr.com/ru/post/983463/


All Articles