Skip to content

Commit f76ec7f

Browse files
committed
Merge branch 'develop' into MCU8MASS-512-513-changelog-version
2 parents fdff7b5 + 4455966 commit f76ec7f

File tree

10 files changed

+285
-223
lines changed

10 files changed

+285
-223
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@
4747
* Split HTTP example into a HTTP and HTTPS example
4848
* Add a log message when HTTPS security profile is not set up
4949
* Update library.properties to pass lint checks for PR with library-registry
50+
* Improve error messages for MQTT
51+
52+
## Changes
53+
54+
* Use Arduino interrupt system so that the library doesn't hijack interrupt service routines used by other libraries
55+
* Change sandbox application to use increased MQTT Quality of Service in order to not lose received messages
56+
* Remove MqttClient.disconnect() (full functionality given in MqttClient.end())
5057

5158
## Bugfixes
5259

examples/mqtt_polling/mqtt_polling.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ void loop() {
6969
String message = MqttClient.readMessage(MQTT_SUB_TOPIC);
7070

7171
// Read message will return an empty string if there were no new
72-
// messages, so anything other than that means that there were a new
72+
// messages, so anything other than that means that there was a new
7373
// message
7474
if (message != "") {
7575
Log.infof("Got new message: %s\r\n", message.c_str());
@@ -87,5 +87,5 @@ void loop() {
8787
Log.error("Failed to publish");
8888
}
8989

90-
delay(1000);
90+
delay(5000);
9191
}

examples/power_down/power_down.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
#define SW0 PIN_PD2
2020

21-
ISR(PORTD_PORT_vect) {
21+
void buttonPressedInterrupt(void) {
2222
if (PORTD.INTFLAGS & PIN2_bm) {
2323
// Reset the interupt flag so that we can process the next incoming
2424
// interrupt
@@ -35,6 +35,7 @@ void setup() {
3535
// Configure SW0 for interrupt so we can wake the device up from sleep by
3636
// pressing the button
3737
pinConfigure(SW0, PIN_DIR_INPUT | PIN_INT_FALL);
38+
attachInterrupt(SW0, buttonPressedInterrupt, FALLING);
3839

3940
// Now we configure the low power module for power down configuration, where
4041
// the LTE modem and the CPU will be powered down

examples/power_save/power_save.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#define SW0 PIN_PD2
2121

22-
ISR(PORTD_PORT_vect) {
22+
void buttonPressedInterrupt(void) {
2323
if (PORTD.INTFLAGS & PIN2_bm) {
2424
PORTD.INTFLAGS = PIN2_bm;
2525
}
@@ -34,6 +34,7 @@ void setup() {
3434
// Configure SW0 for interrupt so we can wake the device up from sleep by
3535
// pressing the button
3636
pinConfigure(SW0, PIN_DIR_INPUT | PIN_INT_FALL);
37+
attachInterrupt(SW0, buttonPressedInterrupt, FALLING);
3738

3839
// Now we configure the power save configuration. Note that this has to be
3940
// done before calling Lte.begin().

examples/sandbox/sandbox.ino

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
#define START_PUBLISHING_SENSOR_DATA_FLAG (1 << 6)
3535
#define SEND_SENSOR_DATA_FLAG (1 << 7)
3636

37+
// Allow 32 messsages in flight
38+
#define RECEIVE_MESSAGE_ID_BUFFER_SIZE 32
39+
#define RECEIVE_MESSAGE_ID_BUFFER_MASK (RECEIVE_MESSAGE_ID_BUFFER_SIZE - 1)
40+
3741
typedef enum {
3842
NOT_CONNECTED,
3943
CONNECTED_TO_NETWORK,
@@ -55,7 +59,12 @@ static volatile uint16_t target_seconds = 0;
5559
static volatile uint16_t data_frequency = 0;
5660
static unsigned long last_data_time = 0;
5761

58-
static volatile uint16_t awaiting_messages = 0;
62+
/**
63+
* @brief Circular buffer for the message identifiers of the received messages.
64+
*/
65+
static uint32_t received_message_identifiers[RECEIVE_MESSAGE_ID_BUFFER_SIZE];
66+
static volatile uint8_t received_message_identifiers_head = 0;
67+
static volatile uint8_t received_message_identifiers_tail = 0;
5968

6069
static const char heartbeat_message[] = "{\"type\": \"heartbeat\"}";
6170

@@ -71,15 +80,14 @@ ISR(TCA0_OVF_vect) {
7180
TCA0.SINGLE.INTFLAGS = TCA_SINGLE_OVF_bm;
7281
}
7382

74-
ISR(PORTD_PORT_vect) {
83+
void sendHeartbeatInterrupt(void) {
7584
if (PORTD.INTFLAGS & PIN2_bm) {
76-
7785
event_flags |= SEND_HEARTBEAT_FLAG;
7886
PORTD.INTFLAGS = PIN2_bm;
7987
}
8088
}
8189

82-
ISR(PORTF_PORT_vect) {
90+
void resetInterrupt(void) {
8391
if (PORTF.INTFLAGS & PIN6_bm) {
8492
asm("jmp 0");
8593
PORTF.INTFLAGS = PIN6_bm;
@@ -93,7 +101,12 @@ void disconnectedFromBroker(void) { event_flags |= BROKER_DISCONN_FLAG; }
93101
void receivedMessage(const char *topic,
94102
const uint16_t msg_length,
95103
const int32_t msg_id) {
96-
awaiting_messages++;
104+
105+
received_message_identifiers_head =
106+
(received_message_identifiers_head + 1) &
107+
RECEIVE_MESSAGE_ID_BUFFER_MASK;
108+
109+
received_message_identifiers[received_message_identifiers_head] = msg_id;
97110
}
98111

99112
void connectMqtt() {
@@ -272,9 +285,11 @@ void setup() {
272285

273286
// Set PD2 as input (button)
274287
pinConfigure(PIN_PD2, PIN_DIR_INPUT | PIN_INT_FALL);
288+
attachInterrupt(PIN_PD2, sendHeartbeatInterrupt, FALLING);
275289

276290
// Set PF6 as input (reset button)
277291
pinConfigure(PIN_PF6, PIN_DIR_INPUT | PIN_INT_FALL);
292+
attachInterrupt(PIN_PF6, resetInterrupt, FALLING);
278293

279294
sei();
280295

@@ -317,6 +332,7 @@ void setup() {
317332
delay(500);
318333
while (!SequansController.retryCommand(
319334
"AT+SQNSPCFG=1,2,\"0xC02B\",1,19,0,0,\"\",\"\",1,0,0")) {}
335+
320336
// - Patch end -
321337

322338
connectLTE();
@@ -372,7 +388,7 @@ void loop() {
372388

373389
Log.info("Connected to MQTT broker, subscribing to topics!\r\n");
374390

375-
MqttClient.subscribe(mqtt_sub_topic);
391+
MqttClient.subscribe(mqtt_sub_topic, AT_LEAST_ONCE);
376392

377393
break;
378394
default:
@@ -409,34 +425,34 @@ void loop() {
409425
}
410426

411427
event_flags &= ~BROKER_DISCONN_FLAG;
412-
} else if (awaiting_messages > 0) {
428+
} else if (received_message_identifiers_head !=
429+
received_message_identifiers_tail) {
413430

414431
switch (state) {
415432
case CONNECTED_TO_BROKER:
416433
case STREAMING_DATA: {
417434

418435
char message[400] = "";
419436

420-
const bool message_read_successfully = MqttClient.readMessage(
421-
mqtt_sub_topic, (uint8_t *)message, sizeof(message));
437+
cli();
438+
received_message_identifiers_tail =
439+
(received_message_identifiers_tail + 1) &
440+
RECEIVE_MESSAGE_ID_BUFFER_MASK;
441+
442+
const uint32_t message_id =
443+
received_message_identifiers[received_message_identifiers_tail];
444+
sei();
422445

423-
awaiting_messages--;
446+
const bool message_read_successfully =
447+
MqttClient.readMessage(mqtt_sub_topic,
448+
(uint8_t *)message,
449+
sizeof(message),
450+
message_id);
424451

425452
if (message_read_successfully) {
426453
decodeMessage(message);
427454
} else {
428455
Log.error("Failed to read message\r\n");
429-
430-
// In case we lost some messages, we clear them out from the
431-
// Sequans Modem such that the memory is freed
432-
if (awaiting_messages > 0) {
433-
Log.errorf("Lost %d message(s) due to messages coming in "
434-
"too quickly to process\r\n",
435-
awaiting_messages);
436-
437-
MqttClient.clearMessages(mqtt_sub_topic, awaiting_messages);
438-
awaiting_messages = 0;
439-
}
440456
}
441457

442458
} break;
@@ -513,7 +529,7 @@ void loop() {
513529
Veml3328.getRed());
514530

515531
if (!MqttClient.publish(mqtt_pub_topic, transmit_buffer)) {
516-
Log.errorf("Could not publish message: %s\r\n",
532+
Log.errorf("Failed to publish message: %s\r\n",
517533
transmit_buffer);
518534
}
519535

src/lte.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static void connectionStatus(char *buffer) {
7171

7272
// The modem does not give any notification of a MQTT disconnect.
7373
// This must be called directly following a connection loss
74-
MqttClient.disconnect(true);
74+
MqttClient.end();
7575

7676
if (disconnected_callback != NULL) {
7777
disconnected_callback();
@@ -98,8 +98,8 @@ bool LteClass::begin(const bool print_messages) {
9898
if (!SequansController.isInitialized()) {
9999
SequansController.begin();
100100

101-
// Allow 50ms for boot
102-
delay(50);
101+
// Allow 2s for boot, we have to wait until the SYSSTART URC arrives
102+
delay(2000);
103103
}
104104

105105
SequansController.clearReceiveBuffer();
@@ -119,16 +119,12 @@ bool LteClass::begin(const bool print_messages) {
119119
// Clear receive buffer before querying the SIM card
120120
SequansController.clearReceiveBuffer();
121121

122-
// We check that the SIM card is inserted and ready. Note that we can only
123-
// do this and get a meaningful response in CFUN=1 or CFUN=4.
124-
SequansController.retryCommand(AT_COMMAND_CHECK_SIM);
125-
126122
char response[32] = "";
127123

128-
ResponseResult result =
129-
SequansController.readResponse(response, sizeof(response));
130-
131-
if (result != ResponseResult::OK) {
124+
// We check that the SIM card is inserted and ready. Note that we can only
125+
// do this and get a meaningful response in CFUN=1 or CFUN=4.
126+
if (!SequansController.retryCommand(
127+
AT_COMMAND_CHECK_SIM, response, sizeof(response))) {
132128
Log.error("Checking SIM status failed, is the SIM card inserted?");
133129
SequansController.retryCommand(AT_COMMAND_DISCONNECT);
134130
return false;
@@ -175,13 +171,10 @@ bool LteClass::begin(const bool print_messages) {
175171
delay(500);
176172

177173
SequansController.clearReceiveBuffer();
178-
SequansController.retryCommand(AT_COMMAND_GET_CLOCK);
179-
180174
char clock_response[48] = "";
181-
result =
182-
SequansController.readResponse(clock_response, sizeof(clock_response));
183175

184-
if (result != ResponseResult::OK) {
176+
if (!SequansController.retryCommand(
177+
AT_COMMAND_GET_CLOCK, clock_response, sizeof(clock_response))) {
185178
Log.errorf("Retrieving LTE modem time failed");
186179
SequansController.retryCommand(AT_COMMAND_DISCONNECT);
187180
return false;

0 commit comments

Comments
 (0)