Embed simple MQTT client in the application
parent
4f95b44205
commit
3b812d49a9
|
@ -28,3 +28,6 @@ pip-log.txt
|
||||||
venv
|
venv
|
||||||
*.pyc
|
*.pyc
|
||||||
build
|
build
|
||||||
|
|
||||||
|
# Instance dir
|
||||||
|
instance/
|
||||||
|
|
|
@ -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
|
19
hello.py
19
hello.py
|
@ -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())
|
||||||
|
|
Loading…
Reference in New Issue