Ich habe ein Problem mit MQTT lib (https://pypi.python.org/pypi/paho-mqtt/1.1).Wie kann man mit MQTT und Flask eine Antwort im Internet erstellen?
Ich möchte Async-und Sync-Aufgaben für mehrere Anfragen, aber es scheint, ich kann nicht umgehen ein Problem, eine Antwort auf Web, für angeforderte URL/MQTT, wenn tatsächlich abonnierte Nachricht empfangen wird, da on_message eine Rückruffunktion ist .
@app.route('/mqtt', methods=['POST', 'GET'])
def main():
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
test = on_message
client.username_pw_set('x', 'x')
client.connect("x", 1883, 60)
client.subscribe("/message/1")
client.loop_forever()
# how do I get the message from callback function on_message and then stop loop_forever?
#I thought about: return msg but it doesn't work.
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
#how to pass this msg to the main function?
print(msg.topic + " " + str(msg.payload))