MKR1500 loses modem communication, must hard reset

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

For reference, I have been running this modem (R410M) with an Arduino Mega continuously since the beginning of October. I send out data every 10 mins. The mega has had to hard reset the modem 35 times in the time period. I don't use the Arduino libraries to talk to the modem, but my sketch does hard reset the modem after a certain number of response timeouts. I have very poor signal at the installed location.

Thanks for the reply. In a 24 hour period the MKR1500 has had to reset the modem 9 times. I am checking every minute though so I would expect about 10x as many resets compared to your case. So given that it's a similar rate.

Did you solve this problem? I have problem where MODEM didnt response for couple of minutes... i must disconnect it from power source for while...

Did you edit src/Modem.cpp and src/NB.cpp to hard reset for >10 s if communication fails? This code works for me, and I haven't noticed any bricked modems from repeated hard resets (which I've read some people complain about). The reason I posted this was because I was concerned that I shouldn't be hard resetting - the library says it should only be done in an emergency, but I guess total loss of communication counts as an emergency.

I'm running into the same behavior as @emololftw. The only way to get my modem back is to pull the power, even with your (@jhochs ) modifications to src/Modem.cpp and src/NB.cpp.

.hardReset(), .reset(), .end(), .begin(), none of it brings the modem back after it goes offline, and it going offline seems pretty random.

MODEM.noop() reports 0 so sending any AT commands to the modem for resetting/restarting is not an option (verified by using SARASerialPassThrough to try to send commands to the modem).

Frustrating to say the least, and I'm pretty close to just abandoning the MKR/SARA combination and trying something else.

The only other thing you could try is to update the SARA's firmware to 05.12. See Firmware upgrade for UBLOX SARA-R410M-02B on the MKR NB 1500 #2

Honestly, if I were you I would abandon this board and use something else. The code has bugs, no documentation, and both U-Blox and Arduino support is useless. I've spent over a year wrestling with these boards and knowing what I know now, I wouldn't use them again.

I also spent months fighting the obstacles of modem issues...

You have to upgrade to the latest firmware.

What else? If you need the mkr formfactor and if you want a compact hardware design the only alternative i know is the Telit Charlie board. Who will write the libs? Zbelding? The 7000 modem i tried has also lacks in the libs. Do you really know if another modem is the remedy? At the end these modems are qualcom basebands. I guess after a long way you will find similar issues..

Arduino can't help, even if they are willing to, it's not their product. U-blox is not capable to provide "end user" support, be real. To be fair, there is plenty of documentation.

IMHO the things to keep in mind:

  • The modem is a complete computer, with os and everything. The only way to communicate is over the AT console. Thats what the arduino libs try to do. Feel free to follow zbelding and communicate directly with the modem.
  • My experience is : Timing is everthing. AT command to the modem -> Wait for the answer -> Next command. And so on. In this order.
  • Don't try to establish data sockets with weak signal strenght. Fighting the modem and the socket problems are too much (for me)
  • The main problem with the mkr1500 is that the NB / LTE-M band is not really established, so even when your code is stable, you need coverage from your carrier (Has nothing to do with arduino/modem)
  • A new mkr1600 with a full LTE modem and wired I2S is needed to lead the mkr series into the future of 4G/5G. I don't understand why Arduino doesn't take the MKR1400 and replace the modem with a new one. Very very low development effort. Just connect +/-/RX/TX/RESET/I2S . I think the new U-blox modems are also pin compatible...

I’ve updated to the 5.12 firmware, so that’s as much as can be done there.

These ‘plug-pull required’ issues seem to be insurmountable, however. Commands that work without issue over multiple iterations will suddenly receive no response, and I haven’t been able to find a triggering event or circumstance that lets me get ahead of it.

Just learned there's another update to the firmware. The documentation doesn't seem to indicate anything that will help with the shutdowns, but one never knows.

Hi,

I was having the same issue. Here is Australia using Telstra NB-IOT sim. I used jhochs idea and adjusted the library’s. This has seemed to work. But as stated in other comments is this the correct way. What’s causing this issue to start with?
Will continue using the MKR1500 this way until fix arises.

Thanks