Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Timed Intervals for MQTT Messages #37

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions AquaMQTT/include/config/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,24 @@ constexpr bool DEBUG_RAW_SERIAL_MESSAGES = false;
constexpr uint32_t MQTT_FULL_UPDATE_MS = 1000 * 60 * 30;

/**
* Change the fixed time interval where the attributes published to the stats topic are updated.
* Change the time interval where the attributes published to the 'energy' topic are updated.
*/
constexpr uint16_t MQTT_STATS_UPDATE_MS = 5000;
constexpr uint32_t MQTT_ENERGY_UPDATE_MS = 1000 * 60;

/**
* Change the time interval where the attributes published to the 'hmi' topic are updated.
*/
constexpr uint32_t MQTT_HMI_UPDATE_MS = 1000 * 60;

/**
* Change the time interval where the attributes published to the 'main' topic are updated.
*/
constexpr uint32_t MQTT_MAIN_UPDATE_MS = 1000 * 60;

/**
* Change the time interval where the attributes published to the 'stats' topic are updated.
*/
constexpr uint32_t MQTT_STATS_UPDATE_MS = 1000 * 60 * 5;

/**
* Self explanatory internal settings: most probably you don't want to change them.
Expand Down
3 changes: 3 additions & 0 deletions AquaMQTT/include/task/MQTTTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class MQTTTask
uint8_t mTransferBuffer[message::HEATPUMP_MAX_FRAME_LENGTH];
uint8_t mTopicBuffer[config::MQTT_MAX_TOPIC_SIZE];
uint8_t mPayloadBuffer[config::MQTT_MAX_PAYLOAD_SIZE];
unsigned long mLastEnergyUpdate;
unsigned long mLastHmiUpdate;
unsigned long mLastMainUpdate;
unsigned long mLastStatsUpdate;
unsigned long mLastFullUpdate;
WiFiClient mWiFiClient;
Expand Down
21 changes: 18 additions & 3 deletions AquaMQTT/src/task/MQTTTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ using namespace message;

MQTTTask::MQTTTask()
: mLastStatsUpdate(0)
, mLastEnergyUpdate(0)
, mLastHmiUpdate(0)
, mLastMainUpdate(0)
, mLastFullUpdate(0)
, mMQTTClient(256)
, mTransferBuffer{ 0 }
Expand Down Expand Up @@ -305,9 +308,15 @@ void MQTTTask::loop()

bool statsUpdate = (millis() - mLastStatsUpdate) >= config::MQTT_STATS_UPDATE_MS;

bool energyUpdate = (millis() - mLastEnergyUpdate) >= config::MQTT_ENERGY_UPDATE_MS;

bool hmiUpdate = (millis() - mLastHmiUpdate) >= config::MQTT_HMI_UPDATE_MS;

bool mainUpdate = (millis() - mLastMainUpdate) >= config::MQTT_MAIN_UPDATE_MS;

auto notify = ulTaskNotifyTake(pdTRUE, mqttCycle);

if ((notify & 1 << 8) != 0 || fullUpdate)
if (hmiUpdate || fullUpdate)
{
if (HMIStateProxy::getInstance().copyFrame(aquamqtt::message::HMI_MESSAGE_IDENTIFIER, mTransferBuffer))
{
Expand All @@ -318,10 +327,12 @@ void MQTTTask::loop()
mLastProcessedHMIMessage = new uint8_t[aquamqtt::message::HMI_MESSAGE_LENGTH];
}
memcpy(mLastProcessedHMIMessage, mTransferBuffer, aquamqtt::message::HMI_MESSAGE_LENGTH);

mLastHmiUpdate = millis();
}
}

if ((notify & 1 << 7) != 0 || fullUpdate)
if (mainUpdate || fullUpdate)
{
if (MainStateProxy::getInstance().copyFrame(aquamqtt::message::MAIN_MESSAGE_IDENTIFIER, mTransferBuffer))
{
Expand All @@ -332,10 +343,12 @@ void MQTTTask::loop()
mLastProcessedMainMessage = new uint8_t[aquamqtt::message::MAIN_MESSAGE_LENGTH];
}
memcpy(mLastProcessedMainMessage, mTransferBuffer, aquamqtt::message::MAIN_MESSAGE_LENGTH);

mLastMainUpdate = millis();
}
}

if ((notify & 1 << 6) != 0 || fullUpdate)
if (energyUpdate || fullUpdate)
{
if (MainStateProxy::getInstance().copyFrame(aquamqtt::message::ENERGY_MESSAGE_IDENTIFIER, mTransferBuffer))
{
Expand All @@ -346,6 +359,8 @@ void MQTTTask::loop()
mLastProcessedEnergyMessage = new uint8_t[aquamqtt::message::ENERGY_MESSAGE_LENGTH];
}
memcpy(mLastProcessedEnergyMessage, mTransferBuffer, aquamqtt::message::ENERGY_MESSAGE_LENGTH);

mLastEnergyUpdate = millis();
}
}

Expand Down