MQTT-函数以破折号显示输出

MQTT - Function display output in Dash(MQTT-函数以破折号显示输出)

本文介绍了MQTT-函数以破折号显示输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Ploly Dash上显示订阅特定主题的MQTT响应。不幸的是,订阅该主题的on_message函数没有显示任何内容。我是Python和Ploly Dash的新手,无法正常工作

import dash
import dash_html_components as html
import dash_core_components as dcc
import dash_bootstrap_components as dbc
import flask
from dash.dependencies import Input, Output, State
import json
import paho.mqtt.client as mqtt

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

server = flask.Flask(__name__)
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.config.suppress_callback_exceptions = True

app.layout = html.Div([
                                         html.Div([
                                                    html.Button('Register', id='post-val', n_clicks=0)
                                                    
                                                ],style = {'marginTop': 50, 'margin-left' : '600px'}),
                                        
                                        html.Br(),
                                        
                                        html.Div(
                                            
                                            id='textarea-state-example-output', style={'whiteSpace': 'pre-line'}
                                            ),
                                        html.Div(
                                            
                                            id='textarea-state-example-output1', style={'whiteSpace': 'pre-line'}
                                            ),


                                 ],style={'width':'100%', 'margin': 20})
 
MQTT_HOST = "172.17.0.2"
#MQTT_HOST = 'localhost'
MQTT_PORT = 1883
MQTT_KEEPALIVE_INTERVAL = 45
MQTT_TOPIC_Registration = "gw/register"
MQTT_TOPIC_Registration_Response = "gw/registerresponse"
mqttc = mqtt.Client()
mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)

@app.callback(
    Output('textarea-state-example-output1', 'children'),
    [Input('post-val', 'n_clicks')]
    )


def display_Output(clicked):
    
    MQTT_HOST = "172.17.0.2"
    #MQTT_HOST = "localhost"
    MQTT_PORT = 1883
    MQTT_KEEPALIVE_INTERVAL = 45
    MQTT_TOPIC_Registration_Response = "gw/D4351D552DA8/registerresponse"
    if clicked:
        def on_connect(client, userdata, flags, rc):
            client.subscribe(MQTT_TOPIC_Registration_Response)
            
            
        def on_message(client, userdata, msg):
            data = json.loads(str(msg.payload.decode("utf-8","ignore")))
            return str(data)
        
        mqttc = mqtt.Client()
        mqttc.on_connect = on_connect
        mqttc.on_message = on_message
        
        mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
        return 'Print' +str(mqttc.on_message)
        


@app.callback(
    Output('textarea-state-example-output', 'children'),
    [Input('post-val', 'n_clicks')])

def update_output(clicked):
    
    if clicked:

    MQTT_MSG=json.dumps({"gw_macid": "[32,74,2,255,255,144,255,203]",
                         "zigbee_mac_id": "60A423FFFE4292F1",
                         "gw_ip" : "192.168.0.105",
                         "gw_fw_version": "19.4.h-597",
                         "status_report_interval": 1,
                         "net_config": 2, 
                         "network_gateway_ip": 171986689,
                         "device_net_mask": 4294967040 });  
        
        mqttc.publish(MQTT_TOPIC_Registration, MQTT_MSG)
        
        res = mqttc.subscribe(MQTT_TOPIC_Registration_Response)
        if len(res)> 0:
            return "Gateway with MAC id" +gw_mac_id +"registered successfully"
        return "Gateway cannot be registered :("

if __name__ == '__main__':
    app.run_server(host='0.0.0.0',port=8050,debug=True)

运行应用程序时显示Print<function display_Output.<locals>.on_message at 0x7f3690143700>

display_Output函数的哪些更改将提取收到的响应?

提前感谢

mqtt

on_message函数不返回任何内容,它在新消息到达时在推荐答案客户端的网络线程上运行。

因此,尝试将其添加到页面的呈现方不会做任何有意义的事情

这篇关于MQTT-函数以破折号显示输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:MQTT-函数以破折号显示输出

基础教程推荐