我有一个用Python编写的flask服务器在IBM BlueMix上运行。我希望此服务器侦听MQTT通道,并在接收到消息时发布消息。我有下面的代码,但是on_connect和on_消息永远不会被调用。
app = Flask(__name__)
def on_connect(client):
client.publish(topic2, "CONNECTED")
def on_message(client, userdata, msg):
client.publish(topic2, "MESSAGE")
@app.route('/')
def hello_world():
return 'Hello World! I am running on port ' + str(port)
if __name__ == '__main__':
client = mqtt.Client()
client.username_pw_set(username, password)
client.on_connect = on_connect
client.on_message = on_message
client.connect('broker.example.com')
client.subscribe(topic)
client.publish(topic2, "STARTING SERVER")
app.run(host='0.0.0.0', port=port)
我一直在尝试client.loop和client.loop_,但它不起作用。
编辑:client.publish(topic2,“启动服务器”)正在工作,我的凭据被删除。
目前没有回答
相关问题 更多 >
编程相关推荐