0

I connected a DHT-11 to the GPIO04 pin and wrote this code down.

import Adafruit_DHT

sensor = Adafruit_DHT.DHT22
pin = 7

while True:
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
    print(temperature, humidity)

It just prints None None and does nothing else. Thanks for all the help!~

Edit: I ran this code

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

and it came out as process ended as with exit code 0

5
  • 1
    Does this answer your question? Reliable temperature/humidity logging with Python and a DHT11
    – Milliways
    Mar 26, 2023 at 21:09
  • are you certain that the pin name is correct?
    – jsotola
    Mar 27, 2023 at 1:32
  • jsotola yes im sure
    – yesn't
    Mar 27, 2023 at 16:24
  • @Milliways Process ended with exit code 0. comes out when i run the code
    – yesn't
    Mar 27, 2023 at 16:36
  • "Process ended with exit code 0" - what process? WHAT did you actually do? NOTE edit your question DO NOT put detail in Comments. The last time I used Adafruit code it did work, but was unreliable. NOTE it is unlikely to work if you connect to GPIO4 but tell the code you are using GPIO7.
    – Milliways
    Mar 28, 2023 at 0:45

0

Your Answer

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

Browse other questions tagged or ask your own question.