0
MQTT lib(https://pypi.python.org/pypi/paho-mqtt/1.1)に問題があります。MQTTとFlaskを使用してWeb上でレスポンスを作成する方法は?
複数のリクエストに対して非同期および同期タスクを実行したいが、on_messageがコールバック関数であるため、実際に購読されたメッセージを受信したときにリクエストされたurl/mqttに対してWebでレスポンスを発行することはできないようだ。 GETメッセージは、あなたがclient.disconnect
サンプル・コードを使用することができたときにloop_forever停止するには
@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))