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+
3741typedef enum {
3842 NOT_CONNECTED,
3943 CONNECTED_TO_NETWORK,
@@ -55,7 +59,12 @@ static volatile uint16_t target_seconds = 0;
5559static volatile uint16_t data_frequency = 0 ;
5660static 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
6069static 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; }
93101void 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
99112void 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
0 commit comments