带烧瓶的RPI dht22:无法将第4行设置为输入-等待PulseIn消息超时 [英] RPI dht22 with flask: Unable to set line 4 to input - Timed out waiting for PulseIn message
问题描述
我正在尝试制作一个可通过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
并享受. :-)
Short answer
Remove debug=True
and enjoy. :-)
这个问题使我发疯,但我想我找到了根本原因!就我而言,我将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屋!