2
我想從使用Flask構建的網頁發送MQTT消息。從用python編寫的網頁發送MQTT消息與Flask
我已經建立在主迴路的連接,我能夠開始瓶
當我打電話之前發送的消息:
client.publish('all/camera/'+path, 'all')
沒有任何反應。沒有錯誤,但沒有消息發送。我最好的猜測是這是一個範圍問題。即。該對象的'客戶'是不可見的功能。我曾試圖啓動客戶端的主要功能之外,我也試圖申報「客戶」作爲全球捕獲()函數
的燒瓶應用程序裏面的代碼是下面:
#!/usr/bin/env python
from flask import Flask, render_template, request
import paho.mqtt.client as mqtt
from 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 will 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)
你應該在返回之前調用client.publish –