1

OK, This is a little frustrating as I am JUST trying to get the temperature/Humidity readings on my DHT 11 sensor with Raspberry pi 4 and I am unable to do so. Please see below

https://www.electronicwings.com/raspberry-pi/dht11-interfacing-with-raspberry-pi

I get an error asking me to run the same as sudo.

Then after I try to run the program as a sudo user I get the following error repeatedly:

"Failed to get the readin. Try Again"

I even tried changing the sensor to no avail....

There were some posts suggesting a different version as the Adafruit function does not seem support the latest hardware. However there does not seem to be a proper closure to any of the posts. enter image description here I also tried this

https://stackoverflow.com/questions/63232072/cannot-import-name-beaglebone-black-driver-from-adafruit-dht

Any help is appreciated.


Ok So like Dougie suggested I went to the post and downloaded DHT.PY

Reliable temperature/humidity logging with Python and a DHT11

After downloading DHT.Py , I ran the script mentioned in the link above and then when i ran I do not see any error message nor do i see any out put

enter image description here

I also ensured to keep the DHT.Py and the script that i am running in the same folder...(Double checked pins , nothing much to check anyways, there is ground , power and pin 4 (BCM)), SO it is not the circuit or the sensor , so I ma guessing it must be code. I also did sudo pigpiod before I ran all this..

Am i just being plain stupid or is this really an issue ??

Milliways
62.8k33 gold badges114 silver badges228 bronze badges
asked Sep 26, 2020 at 8:25
8
  • 1
    1) use different software, 2) wire the DHT11 correctly. Commented Sep 26, 2020 at 8:27
  • 1
    The Adafruit code is notoriously unreliable. See raspberrypi.stackexchange.com/a/105549/8697 Commented Sep 26, 2020 at 8:52
  • Does this answer your question? Reliable temperature/humidity logging with Python and a DHT11 Commented Sep 26, 2020 at 14:24
  • @Dougie thanks for this but looks like I am still struggling. Please see the edit for the problems that I am facing... Commented Sep 27, 2020 at 18:36
  • @Milliways , thanks this is the code that I am using now that even Dougie has suggested..No solution in sight... Commented Sep 27, 2020 at 18:45

2 Answers 2

0

The code I ACTUALLY use is raspberrypi.stackexchange.com/a/105549/8697
This doesn't actually output anything and only posts a single MQTT message.

The following simplified code prints a single reading.

#!/usr/bin/env python3
# 2020年09月27日
"""
 A Program to read the DHTXX temperature/humidity sensors.
 REQUIREMENTS
 DHT.py download "module" from http://abyz.me.uk/rpi/pigpio/code/DHT.py
 pigpiod running
"""
import sys
import pigpio
import DHT
import time
import datetime
# Sensor should be set to DHT.DHT11, DHT.DHTXX or DHT.DHTAUTO
sensor = DHT.DHT11
pin = 4 # Data - Pin 7 (BCM 4)
def output_data(timestamp, temperature, humidity):
 # Sample output Date: 2019年11月17日T10:55:08, Temperature: 25°C, Humidity: 72%
 date = datetime.datetime.fromtimestamp(timestamp).replace(microsecond=0).isoformat()
 print(u"Date: {:s}, Temperature: {:g}\u00b0C, Humidity: {:g}%".format(date, temperature, humidity))
pi = pigpio.pi()
if not pi.connected:
 exit()
s = DHT.sensor(pi, pin, model = sensor)
tries = 5 # try 5 times if error
while tries:
 try:
 timestamp, gpio, status, temperature, humidity = s.read() #read DHT device
 if(status == DHT.DHT_TIMEOUT): # no response from sensor
 exit()
 if(status == DHT.DHT_GOOD):
 output_data(timestamp, temperature, humidity)
 exit() # Exit after successful read
 time.sleep(2)
 tries -=1
 except KeyboardInterrupt:
 break
answered Sep 28, 2020 at 2:22
3
  • I am sorry I don't see any difference here .. I still do not see any out-put or message after I run this in thonny python ide All I see is this in the shell: Python 3.7.3 (/usr/bin/python3) >>> Commented Sep 29, 2020 at 2:11
  • Did you ACTUALLY try running it? There are MANY changes - not least uncommenting the print statement. If you ACTUALLY followed the original - which requires installing MQTT and modifying cron it works, BUT still doesn't output anything, and was never intended to do so, it was an EXAMPLE people could use to create their own code. Commented Sep 29, 2020 at 3:05
  • NOTE forget Thonny - this is a stand alone program, which will run (if you set execute permission) or run with python3 name.py Commented Sep 29, 2020 at 3:07
0
from pigpio_dht import DHT11, DHT22
gpio = 4 # BCM Numbering
sensor = DHT11(gpio)
#sensor = DHT22(gpio)
result = sensor.read()
print(result)

Even I had the same problem..This worked for me you can also try once. Before running the code enter the commands on the terminal

sudo pigpiod #Start daemon
pigs pud 4 u # Set internal pull up

If pigpio-dht is not installed enter pip3 install pigpio-dht and run the above program

answered Mar 11, 2021 at 4:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.