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
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
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 ??
-
11) use different software, 2) wire the DHT11 correctly.joan– joan2020年09月26日 08:27:52 +00:00Commented Sep 26, 2020 at 8:27
-
1The Adafruit code is notoriously unreliable. See raspberrypi.stackexchange.com/a/105549/8697Milliways– Milliways2020年09月26日 08:52:39 +00:00Commented Sep 26, 2020 at 8:52
-
Does this answer your question? Reliable temperature/humidity logging with Python and a DHT11Dougie– Dougie2020年09月26日 14:24:32 +00:00Commented 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...Vikram Suriyanarayanan– Vikram Suriyanarayanan2020年09月27日 18:36:53 +00:00Commented 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...Vikram Suriyanarayanan– Vikram Suriyanarayanan2020年09月27日 18:45:37 +00:00Commented Sep 27, 2020 at 18:45
2 Answers 2
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
-
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– Vikram Suriyanarayanan2020年09月29日 02:11:59 +00:00Commented 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.Milliways– Milliways2020年09月29日 03:05:20 +00:00Commented 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.pyMilliways– Milliways2020年09月29日 03:07:45 +00:00Commented Sep 29, 2020 at 3:07
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