0
votes

I am new to working with Raspberry Pi physical computing. I have a Raspberry Pi and am trying to write a simple Python program to print the temperature and humidity data to the log. I have a DHT11 sensor plugged into 3V3, ground, and the data connected to GPIO pin 14. Here is the code I have so far:

#!/usr/bin/env python

from gpiozero import InputDevice

print(InputDevice(14, False))

However, all this prints is:

<gpiozero.InputDevice object on pin GPIO14, pull_up=False, is_active=False>

I'm not sure if it was wrong to use 'InputDevice' or really which direction to go from here. I just want to be able to read the temp and humidity. Thank you for any advice.

1
did you read documentation ? Probably you need x = InputDevice(14, False) and later something like print( x.some_function() )furas
You printed the object itself. The examples mostly have while True loops reading values from devices gpiozero.readthedocs.io/en/v1.3.1/api_input.html#inputdeviceOneCricketeer
Yes I read the documentation, but I was not able to find out how to retrieve the data for a generic device, not a built-in one to gpiozero like DistanceSensor, since the temperature sensor does not have code built in to gpiozero.garson

1 Answers

1
votes

May by this help you. I use this code to read the sensor and write to a file.

    #!/usr/bin/python

csvfile = "/home/pi/My-logs/temp_181.txt"

import time
from datetime import datetime

import Adafruit_DHT

pin_dht11 = 25 # GPIO number-color brown

while True:
    date = datetime.now()
    timestamp = date.strftime("%d/%m/%Y %H:%M:%S")

    #Read the DHT11 device to get humidity and temperature
    hum_dht11, temp_dht11 = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11, pin_dht11)

    values_10 = timestamp,  round(temp_dht11, 1), round(hum_dht11, 1)

    with open(csvfile, "a") as f:
        f.write (str(values_10) + "\n")

    print(values_10)
    f.close()
    time.sleep(10)

This is my wiring DHT11

Louis