我担心这是一个非常基本的问题,在这里我找不到正确的搜索词,因此任何帮助都将不胜感激
我想经常监控一个覆盆子的GPIO(24)引脚。在最简单的示例中,我将其连接到一个按钮,并希望显示dash站点上的按下次数。我用dash_core_components.Intervalshowhere成功地做到了这一点。但是,这也会每隔一段时间刷新页面。我想不断监测GPIO引脚,只显示网站刷新当前的计数值
import RPi.GPIO as GPIO
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
globalCounter = 0
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.IN) #connected to 3.3V Pin of the pi, separated by a button.
app = dash.Dash()
app.layout = html.Div(
html.Div([
html.H1(id="live-update-text"),
dcc.Interval(
id='interval-component',
interval=1*1000, # in milliseconds
n_intervals=0
)
])
)
@app.callback(Output('live-update-text', 'children'),
Input('interval-component', 'n_intervals'))
def update_metrics(n):
global globalCounter
if GPIO.input(24) == 1:
globalCounter += 1
return "Number of presses: "+str(globalCounter)
if __name__ == '__main__':
app.run_server(debug=True, host='0.0.0.0', port=8080)
为此,我想我需要将app.layout链接到一个函数(如documentation中的2.示例),但是我如何仍然持续监视按钮是否被按下
找到了一个线程解决方案。GPIO在线程中的while循环中进行监控,仪表板布局链接到函数,如文档中所述
相关问题 更多 >
编程相关推荐