Skip to content

Commit eae351a

Browse files
jmpmscorp256dpi
authored andcommitted
added setClockSource to enable a custom clock source for deep sleep applications
1 parent 75740e3 commit eae351a

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,16 @@ void setOptions(int keepAlive, bool cleanSession, int timeout);
146146

147147
- The `keepAlive` option controls the keep alive interval in seconds (default: 10).
148148
- The `cleanSession` option controls the session retention on the broker side (default: true).
149-
- The `timeout` option controls the default timeout for all commands in milliseconds (default: 1000).
149+
- The `timeout` option controls the default timeout for all commands in milliseconds (default: 1000).
150+
151+
Set a custom clock source "custom millis" callback to enable deep sleep applications:
152+
153+
```c++
154+
void setClockSource(MQTTClientClockSource);
155+
// Callback signature: uint32_t clockSource() {}
156+
```
157+
158+
- The specified callback is used by the internal timers to get a monotonic time in milliseconds. Since the clock source for the built-in `millis` is stopped when the the Arduino goes into deep sleep, you need to provide a custom callback that first syncs with a built-in or external Real Time Clock (RTC). You can pass `NULL` to reset to the default implementation.
150159
151160
Connect to broker using the supplied client id and an optional username and password:
152161

src/MQTTClient.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,23 @@ inline void lwmqtt_arduino_timer_set(void *ref, uint32_t timeout) {
55
auto t = (lwmqtt_arduino_timer_t *)ref;
66

77
// set future end time
8-
t->end = (uint32_t)(millis() + timeout);
8+
if (t->millis != nullptr) {
9+
t->end = (uint32_t)(t->millis() + timeout);
10+
} else {
11+
t->end = (uint32_t)(millis() + timeout);
12+
}
913
}
1014

1115
inline int32_t lwmqtt_arduino_timer_get(void *ref) {
1216
// cast timer reference
1317
auto t = (lwmqtt_arduino_timer_t *)ref;
1418

1519
// get difference to end time
16-
return (int32_t)t->end - (int32_t)millis();
20+
if (t->millis != nullptr) {
21+
return (int32_t)(t->end - t->millis());
22+
} else {
23+
return (int32_t)(t->end - millis());
24+
}
1725
}
1826

1927
inline lwmqtt_err_t lwmqtt_arduino_network_read(void *ref, uint8_t *buffer, size_t len, size_t *read,
@@ -144,6 +152,11 @@ void MQTTClient::onMessageAdvanced(MQTTClientCallbackAdvanced cb) {
144152
this->callback.advanced = cb;
145153
}
146154

155+
void MQTTClient::setClockSource(MQTTClientClockSource cb) {
156+
this->timer1.millis = cb;
157+
this->timer2.millis = cb;
158+
}
159+
147160
void MQTTClient::setHost(const char hostname[], int port) {
148161
// free hostname if set
149162
if (this->hostname != nullptr) {

src/MQTTClient.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ extern "C" {
99
#include "lwmqtt/lwmqtt.h"
1010
};
1111

12+
typedef uint32_t (*MQTTClientClockSource)();
13+
1214
typedef struct {
1315
uint32_t end;
16+
MQTTClientClockSource millis;
1417
} lwmqtt_arduino_timer_t;
1518

1619
typedef struct {
@@ -45,8 +48,8 @@ class MQTTClient {
4548
MQTTClientCallback callback;
4649

4750
lwmqtt_arduino_network_t network = {nullptr};
48-
lwmqtt_arduino_timer_t timer1 = {0};
49-
lwmqtt_arduino_timer_t timer2 = {0};
51+
lwmqtt_arduino_timer_t timer1 = {0, nullptr};
52+
lwmqtt_arduino_timer_t timer2 = {0, nullptr};
5053
lwmqtt_client_t client = {0};
5154

5255
bool _connected = false;
@@ -64,6 +67,8 @@ class MQTTClient {
6467
void onMessage(MQTTClientCallbackSimple cb);
6568
void onMessageAdvanced(MQTTClientCallbackAdvanced cb);
6669

70+
void setClockSource(MQTTClientClockSource cb);
71+
6772
void setHost(const char hostname[]) { this->setHost(hostname, 1883); }
6873
void setHost(const char hostname[], int port);
6974

0 commit comments

Comments
 (0)