send config only once
This commit is contained in:
parent
e236841345
commit
c20fb17f03
@ -8,10 +8,12 @@
|
|||||||
|
|
||||||
namespace Mqtt {
|
namespace Mqtt {
|
||||||
|
|
||||||
|
void publishConfig();
|
||||||
void publishCommand();
|
void publishCommand();
|
||||||
void publishBmp280();
|
void publishBmp280();
|
||||||
Task tPublish(TASK_SECOND, TASK_FOREVER, publishCommand, &ts);
|
Task tPublishConfig(TASK_IMMEDIATE, TASK_ONCE, publishConfig, &ts);
|
||||||
Task tPublishBmp(5 * TASK_MINUTE, TASK_FOREVER, publishBmp280, &ts);
|
Task tPublishBmp(5 * TASK_MINUTE, TASK_FOREVER, publishBmp280, &ts);
|
||||||
|
Task tPublishCommand(TASK_SECOND, TASK_FOREVER, publishCommand, &ts);
|
||||||
|
|
||||||
AsyncMqttClient client;
|
AsyncMqttClient client;
|
||||||
|
|
||||||
@ -53,6 +55,18 @@ namespace Mqtt {
|
|||||||
publishTempConfig();
|
publishTempConfig();
|
||||||
publishPressureConfig();
|
publishPressureConfig();
|
||||||
publishAltitudeConfig();
|
publishAltitudeConfig();
|
||||||
|
ts.deleteTask(tPublishConfig);
|
||||||
|
}
|
||||||
|
|
||||||
|
void publishBmp280() {
|
||||||
|
Bmp::data.readAll();
|
||||||
|
StaticJsonDocument<255> jsonDoc;
|
||||||
|
jsonDoc["temperature"] = Bmp::data.temp;
|
||||||
|
jsonDoc["pressure"] = Bmp::data.pressure;
|
||||||
|
jsonDoc["altitude"] = Bmp::data.altitude;
|
||||||
|
char message[255];
|
||||||
|
serializeJson(jsonDoc, message);
|
||||||
|
client.publish(bmpTopic, 0, true, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
void publishCommand() {
|
void publishCommand() {
|
||||||
@ -67,26 +81,15 @@ namespace Mqtt {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void publishBmp280() {
|
|
||||||
Bmp::data.readAll();
|
|
||||||
StaticJsonDocument<255> jsonDoc;
|
|
||||||
jsonDoc["temperature"] = Bmp::data.temp;
|
|
||||||
jsonDoc["pressure"] = Bmp::data.pressure;
|
|
||||||
jsonDoc["altitude"] = Bmp::data.altitude;
|
|
||||||
char message[255];
|
|
||||||
serializeJson(jsonDoc, message);
|
|
||||||
client.publish(bmpTopic, 0, true, message);
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
client.onConnect([](bool sessionPresent) {
|
client.onConnect([](bool sessionPresent) {
|
||||||
publishConfig();
|
tPublishConfig.enable();
|
||||||
tPublish.enableDelayed();
|
tPublishBmp.enableIfNot();
|
||||||
tPublishBmp.enable();
|
tPublishCommand.enableDelayed();
|
||||||
Serial.println("Connected to MQTT");
|
Serial.println("Connected to MQTT");
|
||||||
});
|
});
|
||||||
client.onDisconnect([](AsyncMqttClientDisconnectReason reason) {
|
client.onDisconnect([](AsyncMqttClientDisconnectReason reason) {
|
||||||
tPublish.disable();
|
tPublishCommand.disable();
|
||||||
tPublishBmp.disable();
|
tPublishBmp.disable();
|
||||||
Serial.println("Disconnected from MQTT");
|
Serial.println("Disconnected from MQTT");
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user