From 061a084f1d18e707858754dc43d7abad40a55156 Mon Sep 17 00:00:00 2001 From: damage Date: Sat, 17 May 2025 15:06:48 +0200 Subject: [PATCH] keepalive mqtt message by loops, not by time faster during runtime, calculation not needed --- master/master.ino | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/master/master.ino b/master/master.ino index 89214e8..a8d969c 100644 --- a/master/master.ino +++ b/master/master.ino @@ -19,7 +19,7 @@ #define MQTT_SIM_LOG_TOPIC "/xplane/meta/log" #define MQTT_SIM_VALUE_TOPIC "/xplane/rref/#" -#define MQTT_KEEPALIVE_INTERVAL_MS 15000 +#define MQTT_KEEPALIVE_INTERVAL_LOOPS 60000 struct device { byte address; // I2C address of device @@ -47,7 +47,7 @@ const char topic[] = MQTT_SIM_VALUE_TOPIC; EthernetClient client; char localIP[16]; MqttClient mqttClient(client); -unsigned long mqttLastKeepAlive = millis(); +int mqttLastKeepAlive = 0; void onMqttMessage(int messageSize) { // // we received a message, print out the topic and contents @@ -174,10 +174,11 @@ void loop() { } } - // transmit MQTT keepalive message if MQTT_KEEPALIVE_INTERVAL_MS is reached - // or millis() is wrapping to 0 - if (mqttLastKeepAlive + MQTT_KEEPALIVE_INTERVAL_MS < millis() || mqttLastKeepAlive > millis()) { + // transmit MQTT keepalive message every MQTT_KEEPALIVE_INTERVAL_LOOPS times + // running the loop + mqttLastKeepAlive++; + if (mqttLastKeepAlive >= MQTT_KEEPALIVE_INTERVAL_LOOPS) { sendMqttMessage(MQTT_SIM_DEVICE_TOPIC, 1, localIP); - mqttLastKeepAlive = millis(); + mqttLastKeepAlive = 0; } }