Embed simple MQTT client in the application

master
esensar 2018-04-25 15:51:28 +02:00
parent 4f95b44205
commit 3b812d49a9
3 changed files with 26 additions and 1 deletions

3
.gitignore vendored
View File

@ -28,3 +28,6 @@ pip-log.txt
venv venv
*.pyc *.pyc
build build
# Instance dir
instance/

5
config.py 100644
View File

@ -0,0 +1,5 @@
MQTT_BROKER_URL = 'mybroker.com'
MQTT_BROKER_PORT = 1883
MQTT_USERNAME = 'user'
MQTT_PASSWORD = 'secret'
MQTT_REFRESH_TIME = 1.0 # refresh time in seconds

View File

@ -1,6 +1,23 @@
from flask import Flask from flask import Flask
app = Flask(__name__) from flask_mqtt import Mqtt
app = Flask(__name__, instance_relative_config=True)
app.config.from_object('config')
app.config.from_pyfile('config.py')
mqtt = Mqtt(app)
@app.route("/") @app.route("/")
def hello(): def hello():
return "Hello World!" return "Hello World!"
@mqtt.on_connect()
def handle_connect(client, userdata, flags, rc):
mqtt.subscribe('topic/state')
@mqtt.on_message()
def handle_mqtt_message(client, userdata, message):
data = dict(
topic=message.topic,
payload=message.payload.decode()
)
print(message.payload.decode())