0

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 ??

8
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-17T10: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
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) >>> – Vikram Suriyanarayanan Sep 29 '20 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. – Milliways Sep 29 '20 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 – Milliways Sep 29 '20 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.