Я хочу периодически публиковать данные датчиков на сервере Mosquitto.Я использую планировщик Celery Beat для этого.Рабочий из сельдерея выполняет задачу, но сообщение не публикуется посреднику.
Код:
from flask import Flask
from celery import Celery
import paho.mqtt.client as mqtt
app = Flask(__name__)
app.debug = True
celery = Celery(app.name, broker= 'redis://localhost:6379/0')
app.config['CELERYBEAT_SCHEDULE'] = {
'update_temp': {
'task': 'app.update_temperature',
'schedule': 10.0,
}
}
app.config['CELERY_IMPORTS'] = ('app')
celery.conf.update(app.config)
@celery.task
def update_temperature():
client.publish("temperature/sensor01", payload = "temperature_data")
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("temperature/sensor01")
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
def on_publish(client, userdata, mid):
print('Message Published')
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.on_publish = on_publish
client.connect("localhost", 1883, 60)
client.loop_start()
@app.route('/')
def view():
return "Hello, Flask is up and running!"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080)
Журнал сельдерея:
[2019-04-26 14:48:11,955: INFO/MainProcess] Received task: app.update_temperature[53363e79-3dff-4275-81d7-f15007c69d8f]
[2019-04-26 14:48:11,964: INFO/ForkPoolWorker-4] Task app.update_temperature[53363e79-3dff-4275-81d7-f15007c69d8f] succeeded in 0.0023796100285835564s: None