title: sending MQTT messages from a webpage written in python with Flask
body: <p>I am trying to send a MQTT message from a web page built using Flask.</p>
  
  <p>I have established the connection in the main loop and I am able to send a message before starting Flask </p>
  
  <p>When i call:</p>
  
  <pre><code>client.publish('all/camera/'+path, 'all') 
  </code></pre>
  
  <p>nothing happens. No error but no message is sent. My best guess is that this is a scope problem. ie. that the object 'client' is not visible to the function. I have tried to initiate the client outside of the main function and i have also tried to declare 'client' as global inside the capture() function</p>
  
  <p>the code for the flask app is below:</p>
  
  <pre><code>#!/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)
  </code></pre>