Skip to content

Commit c724eb1

Browse files
committed
MCU8MASS-2370 Add keep alive argument to AWS and Azure for low power applications
1 parent 94db611 commit c724eb1

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

examples/mqtt_low_power/mqtt_low_power.ino

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <mqtt_client.h>
1414
#include <veml3328.h>
1515

16-
#define USE_PSM false
16+
#define USE_PSM true
1717

1818
static char mqtt_pub_topic[128];
1919

@@ -56,7 +56,7 @@ void setup() {
5656
#if USE_PSM
5757
LowPower.configurePeriodicPowerSave(
5858
PowerSaveModePeriodMultiplier::ONE_MINUTE,
59-
1);
59+
3);
6060
#else
6161
LowPower.configurePowerDown();
6262
#endif
@@ -65,8 +65,11 @@ void setup() {
6565

6666
// If we're using PSM, we only need to connect to the MQTT broker at the
6767
// beginning, since the connection will remain active
68+
//
69+
// Here we also set the keep alive to 3 minutes to match the sleep period
70+
// for PSM
6871
#if USE_PSM
69-
MqttClient.beginAWS();
72+
MqttClient.beginAWS(180);
7073
#endif
7174

7275
Mcp9808.begin();

src/mqtt_client.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ static bool generateSigningCommand(char* data, char* command_buffer) {
261261
return true;
262262
}
263263

264-
bool MqttClientClass::beginAWS() {
264+
bool MqttClientClass::beginAWS(const uint16_t keep_alive) {
265265

266266
ATCA_STATUS status = ECC608.begin();
267267

@@ -312,13 +312,13 @@ bool MqttClientClass::beginAWS() {
312312
(char*)(endpoint),
313313
8883,
314314
true,
315-
60,
315+
keep_alive,
316316
true,
317317
"",
318318
"");
319319
}
320320

321-
bool MqttClientClass::beginAzure() {
321+
bool MqttClientClass::beginAzure(const uint16_t keep_alive) {
322322

323323
ATCA_STATUS status = ECC608.begin();
324324

@@ -379,14 +379,21 @@ bool MqttClientClass::beginAzure() {
379379
hostname,
380380
device_id);
381381

382-
return this->begin(device_id, hostname, 8883, true, 60, true, username, "");
382+
return this->begin(device_id,
383+
hostname,
384+
8883,
385+
true,
386+
keep_alive,
387+
true,
388+
username,
389+
"");
383390
}
384391

385392
bool MqttClientClass::begin(const char* client_id,
386393
const char* host,
387394
const uint16_t port,
388395
const bool use_tls,
389-
const size_t keep_alive,
396+
const uint16_t keep_alive,
390397
const bool use_ecc,
391398
const char* username,
392399
const char* password,

src/mqtt_client.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class MqttClientClass {
5757
const char* host,
5858
const uint16_t port,
5959
const bool use_tls,
60-
const size_t keep_alive = 60,
60+
const uint16_t keep_alive = 1200,
6161
const bool use_ecc = true,
6262
const char* username = "",
6363
const char* password = "",
@@ -67,12 +67,12 @@ class MqttClientClass {
6767
/**
6868
* @brief Will configure and connect to the provisioned AWS broker.
6969
*/
70-
bool beginAWS();
70+
bool beginAWS(const uint16_t keep_alive = 1200);
7171

7272
/**
7373
* @brief Will configure and connect to the provisioned Azure broker.
7474
*/
75-
bool beginAzure();
75+
bool beginAzure(const uint16_t keep_alive = 1200);
7676

7777
/**
7878
* @brief Disconnects from the broker and resets the state in the MQTT

0 commit comments

Comments
 (0)