简体   繁体   English

使用DHT11传感器在Raspberry Pi上读取温度

[英]Reading temperature on Raspberry Pi using DHT11 sensor

I am new to working with Raspberry Pi physical computing. 我是使用Raspberry Pi物理计算的新手。 I have a Raspberry Pi and am trying to write a simple Python program to print the temperature and humidity data to the log. 我有一个Raspberry Pi,正在尝试编写一个简单的Python程序以将温度和湿度数据打印到日志中。 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: 我有一个DHT11传感器插入3V3,接地,数据连接到GPIO引脚14。这是到目前为止的代码:

#!/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. 我不确定使用“ InputDevice”是否错误,或者从哪个方向真的错了。 I just want to be able to read the temp and humidity. 我只想能够读取温度和湿度。 Thank you for any advice. 感谢您的任何建议。

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 这是我的接线DHT11

Louis 路易

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM