sending MQTT messages from a webpage written in python with Flask -
i trying send mqtt message web page built using flask.
i have established connection in main loop , able send message before starting flask
when call:
client.publish('all/camera/'+path, 'all')
nothing happens. no error no message sent. best guess scope problem. ie. object 'client' not visible function. have tried initiate client outside of main function , have tried declare 'client' global inside capture() function
the code flask app below:
#!/usr/bin/env python flask import flask, render_template, request import paho.mqtt.client mqtt datetime import datetime app = flask(__name__) @app.route('/', methods = ['post', 'get']) def capture(): timestring=datetime.now().strftime("%y%m%d-%h%m%s") if 'sessionname' in request.form: sessionname = request.form['sessionname'] path = sessionname + "_" + timestring return render_template ('capture.html', path=path, timestring=timestring, sessionname=sessionname) client.publish('all/camera/'+path, 'all') else: return render_template ('capture.html',timestring=timestring) # paho callbacks def on_connect(client, userdata, flags, rc): #sub here re subscribe on reconnection client.subscribe("+/camera/#") client.subscribe("+/led") # main code if __name__=='__main__': client = mqtt.client() client.on_connect = on_connect client.connect("scanserver.local",1883,60) client.loop_start() client.publish('debug', 'server running') app.run(host = '0.0.0.0' , debug = true, port=5001)
as rmn said. had put return in wrong place. working now. thanks
Comments
Post a Comment