带烧瓶的 RPI dht22:无法将第 4 行设置为输入 - 等待 PulseIn 消息超时 [英] RPI dht22 with flask: Unable to set line 4 to input - Timed out waiting for PulseIn message

查看:35
本文介绍了带烧瓶的 RPI dht22:无法将第 4 行设置为输入 - 等待 PulseIn 消息超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 DHT22 提供温度和湿度的 Raspberry Pi 3 REST API.完整代码:

I'm trying to make a Raspberry Pi 3 REST API that provides temperature and humidity with DHT22. The whole code:

from flask import Flask, jsonify, request
from sds011 import SDS011
from adafruit_dht import DHT22
import board
import os
import time

app = Flask(__name__)
dht = DHT22(board.D4)

def get_dht_data():
    while True:
        try:
            temperature, humidity = dht.temperature, dht.humidity
            print(temperature, humidity)
            if temperature is not None and humidity is not None:
                return temperature, humidity
            else:
                raise
        except:
            time.sleep(0.5)

@app.route('/', methods=['GET'])
def status():
    temperature, humidity = get_dht_data()

    return jsonify({
        'temperature': temperature,
        'humidity': humidity
    })

if __name__ == '__main__':
    app.run(debug=True)

我使用了 https://github.com/adafruit/Adafruit_CircuitPython_DHT

但是,当我启动服务器时,它显示消息

However, when I start server, it shows message

'无法将第 4 行设置为输入'

'Unable to set line 4 to input'

并且温度和湿度总是None.如果我不运行 Flask 应用程序而只运行 DHT 代码,它就可以工作.

and temperature and humidity is always None. If I don't run flask app but just DHT code, it works.

推荐答案

简短回答

删除 debug=True 并享受.:-)

这个问题让我抓狂,但我想我找到了根本原因!就我而言,我将 DHT22 对象封装在另一个对象中,如下所示:

This problem drove me nuts but I think I found the root cause! In my case, I was encapsulating the DHT22 object in another object, like so:

...
class DHT22Custom:
    def __init__(self):
        print("**** BUILDING by {0}!".format(threading.currentThread().getName()))
        self.dht_device = adafruit_dht.DHT22(board.D17)
...

我的 main.py 看起来像:

import RPi.GPIO as GPIO
from sensor.config.app_config import create_app

if __name__ == '__main__':
    app = create_app()         # builds DHT22Custom inside
    app.run(debug=True)
    print("Cleaning up GPIO before exiting...")
    GPIO.cleanup()

看看我得到了多么有趣的输出:

And look what an interesting output I got:

root@05600e5:/app# python -m sensor.main
**** BUILDING by MainThread!
 * Serving Flask app "FlaskWS" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
**** BUILDING by MainThread!
Unable to set line 17 to input

MainThread 对我的对象进行了两次初始化!怎么样?好吧,如果您查看 Flask 的 run() 文档,您将看到以下内容:

The MainThread was initializing my object twice! How was that? Well, if you look at the documentation of Flask's run(), you'll see the following:

If the :attr:`debug` flag is set the server will automatically reload
 for code changes and show a debugger in case an exception happened.

所以,Flask 似乎只是重新启动了应用程序或类似的东西.老实说,我不清楚.但是好吧,如果你只是删除 debug,你会看到类似:

So, it seems that Flask just relaunches the application or something like this. Not clear to me, to be honest. But well, if you just remove debug, you'll see something like:

root@05600e5:/app# python -m sensor.main
**** BUILDING by MainThread!
 * Serving Flask app "FlaskWS" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

我希望这会有所帮助.快乐编码!

I hope this helps. Happy coding!

这篇关于带烧瓶的 RPI dht22:无法将第 4 行设置为输入 - 等待 PulseIn 消息超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆