I have a flask server written in Python running on IBM BlueMix. I want this server to listen to a MQTT channel and publish a message when it receives a message. I have the following code but on_connect and on_message are never called.
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)
I've tried client.loop and client.loop_forever but it doesn't work.
EDIT: client.publish(topic2, "STARTING SERVER") is working, my credentials are removed.