Issue:
Given a simple program that sends a MQTT message every minute and puts the board into a deep sleep in between, the program runs as expected for a short while before suddenly losing communication with the modem, and thus being stuck in a loop unable to connect. Calling hardReset() in the modem class* is the only thing I have found that will revive modem comm, but this is recommended for emergency use only. How can I avoid losing connection to the modem in the first place?
*Note: in src/Modem.cpp I had to change line 134:
delay(1000); // Datasheet says nothing, so guess we wait one second
to:
delay(11000);
since the datasheet does specify at least 10 s (see p.32).
Application:
I'm using MKR1500s for remote sensing so they must be very reliable over many months. I've previously been using MKR1400 GSM boards for >1 year with no issues, but am trying to change over because 3G is being depreciated in the US.
Hardware:
MKR1500 powered via the Li-Po battery connector, with T-Mobile SIM. I have a handful of these boards and have found the same problem across all of them. I have updated the SARA firmware to v5.12 for all of them.
Code:
#include <ArduinoBearSSL.h>
#include <ArduinoECCX08.h>
#include <ArduinoLowPower.h>
#include <ArduinoMqttClient.h>
#include <MKRNB.h>
#include "certificate.h"
// NB and MQTT settings:
#define BROKER "XXX.amazonaws.com"
#define TOPIC "dummy/topic"
#define NB_TIMEOUT 60 * 1000UL
#define MQTT_TIMEOUT 30 * 1000UL
#define PINNUMBER ""
#define SLEEP_PERIOD 1 * 60000UL // 1 minute
// Create objects:
GPRS gprs;
NB nbAccess;
NBClient client;
BearSSLClient sslClient(client); // Used for SSL/TLS connection, integrates with ECC508
MqttClient mqttClient(sslClient);
//=============================================================================================
void setup() {
Serial.begin(500000);
delay(5000);
Serial.println("------------------------------------------------------------------");
// Set timeouts:
nbAccess.setTimeout(NB_TIMEOUT);
gprs.setTimeout(NB_TIMEOUT);
mqttClient.setConnectionTimeout(MQTT_TIMEOUT);
// Configure SSL and MQTT:
ArduinoBearSSL.onGetTime(nbAccess.getTime);
sslClient.setEccSlot(0, CERT);
}
//=============================================================================================
void loop() {
// Connect to NB and MQTT:
verifyConnection();
// Send MQTT message:
mqttClient.beginMessage(TOPIC);
mqttClient.print("{\"state\" : {\"reported\": {\"sleeping\": 1}}}");
mqttClient.endMessage();
Serial.println("Shadow updated");
// Disconnect and sleep:
Serial.println("Disconnecting from MQTT...");
mqttClient.stop();
Serial.println("Disconnecting from NB...");
nbAccess.shutdown();
Serial.println("NB disconnected");
Serial.end();
LowPower.deepSleep(SLEEP_PERIOD);
Serial.begin(500000);
delay(10000);
}
//=============================================================================================
/*
If connection is not established, attempt to connect both NB and MQTT until
successful.
*/
void verifyConnection() {
while (!mqttClient.connected()) {
// First attempt:
connectNB();
delay(1000);
// Retry if failed:
int attemptCount = 0;
while (nbAccess.status() != NB_READY || gprs.status() != GPRS_READY) {
if (attemptCount > 4) {
// Soft reset modem
Serial.println("Soft resetting modem");
SerialSARA.begin(115200);
delay(10);
SerialSARA.println("AT+CFUN=15");
delay(2000);
attemptCount = 0;
} else {
attemptCount++;
}
connectNB();
}
// Connect to MQTT:
delay(2000);
connectMQTT();
}
}
//=============================================================================================
/*
Connects to narrowband (LTE) cellular network.
*/
void connectNB() {
Serial.println(F("Attempting to connect to cellular network"));
if ((nbAccess.begin(PINNUMBER) == NB_READY) && (gprs.attachGPRS() == GPRS_READY)) {
Serial.println(F("NB success"));
} else {
Serial.println(F("NB connection failed"));
delay(500);
}
}
//=============================================================================================
/*
Connects to MQTT broker (AWS in this case) to which measurements are streamed
and shadow is held.
*/
void connectMQTT() {
Serial.println(F("Attempting to connect to MQTT broker"));
if (mqttClient.connect(BROKER, 8883)) {
Serial.println(F("MQTT connected"));
} else {
Serial.print(F("MQTT failed to connect, error: "));
Serial.println(mqttClient.connectError());
}
}
With the following edit to src/NB.cpp at line 80:
if (!MODEM.begin(restart)) {
_state = ERROR;
Serial.println("Error starting modem, hard resetting...");
MODEM.hardReset();
Serial.println("Modem was hard reset");
} else {
Thanks in advance for any insights