send config only once
This commit is contained in:
parent
e236841345
commit
c20fb17f03
@ -8,10 +8,12 @@
|
||||
|
||||
namespace Mqtt {
|
||||
|
||||
void publishConfig();
|
||||
void publishCommand();
|
||||
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 tPublishCommand(TASK_SECOND, TASK_FOREVER, publishCommand, &ts);
|
||||
|
||||
AsyncMqttClient client;
|
||||
|
||||
@ -53,6 +55,18 @@ namespace Mqtt {
|
||||
publishTempConfig();
|
||||
publishPressureConfig();
|
||||
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() {
|
||||
@ -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() {
|
||||
client.onConnect([](bool sessionPresent) {
|
||||
publishConfig();
|
||||
tPublish.enableDelayed();
|
||||
tPublishBmp.enable();
|
||||
tPublishConfig.enable();
|
||||
tPublishBmp.enableIfNot();
|
||||
tPublishCommand.enableDelayed();
|
||||
Serial.println("Connected to MQTT");
|
||||
});
|
||||
client.onDisconnect([](AsyncMqttClientDisconnectReason reason) {
|
||||
tPublish.disable();
|
||||
tPublishCommand.disable();
|
||||
tPublishBmp.disable();
|
||||
Serial.println("Disconnected from MQTT");
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user