# SARA Modem freezes when restarted

**URL:** <https://forum.arduino.cc/t/sara-modem-freezes-when-restarted/1190881>\
**Category:** MKR NB 1500\
**Created:** [November 19, 2023, 8:09am UTC](https://forum.arduino.cc/t/sara-modem-freezes-when-restarted/1190881 "2023-11-19T08:09:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgartner](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jgartner/32/290558_2.png) [@jgartner](https://forum.arduino.cc/u/jgartner)\
**Post date:** [November 19, 2023, 8:09am UTC](https://forum.arduino.cc/t/sara-modem-freezes-when-restarted/1190881/1 "2023-11-19T08:09:50Z")

</div>

When I power down the SARA Module with `AT+CPWROFF=1` it cannot be used again to send data after some data has already been sent.

Here is my AT Log from the Serial Monitor:

```arduino
Begin setup
init...
AT
OK
AT+CMEE=0
OK
AT+CFUN=0
OK
AT+CPIN?
+CPIN: READY

OK
AT+CMGF=1
OK
AT+UDCONF=1,1
OK
AT+CTZU=1
OK
AT+CGDCONT=1,"IP","hologram"
OK
AT+UAUTHREQ=1,0
OK
AT+CFUN=1
OK
AT+CEREG?
+CEREG: 0,0

OK
AT+CEREG?
+CEREG: 0,0

OK
AT+CEREG?
+CEREG: 0,0

OK
AT+CEREG?
+CEREG: 0,0

OK
AT+CEREG?
+CEREG: 0,2

OK
AT+CEREG?
+CEREG: 0,2

OK
AT+CEREG?
+CEREG: 0,5

OK
AT+CGATT=1
OK
AT+CGACT?
+CGACT: 1,1

OK
connected
AT+CCLK?
+CCLK: "23/11/19,07:56:48+04"

OK
Force shutting down modem
AT+CFUN=0
OK
AT+CGATT=0
OK
AT+CPWROFF
OK
New interation
New measurement
waiting between msmnts
New measurement
waiting between msmnts
AT
OK
init...
AT
OK
AT+CMEE=0
OK
AT+CFUN=0
OK
AT+CPIN?
+CPIN: READY

OK
AT+CMGF=1
OK
AT+UDCONF=1,1
OK
AT+CTZU=1
OK
AT+CGDCONT=1,"IP","hologram"
OK
AT+UAUTHREQ=1,0
OK
AT+CFUN=1
OK
AT+CEREG?
+CEREG: 0,0

OK
AT+CEREG?
+CEREG: 0,0

OK
AT+CEREG?
+CEREG: 0,0

OK
AT+CEREG?
+CEREG: 0,0

OK
AT+CEREG?
+CEREG: 0,2

OK
AT+CEREG?
+CEREG: 0,2

OK
AT+CEREG?
+CEREG: 0,2

OK
AT+CEREG?
+CEREG: 0,2

OK
AT+CEREG?
+CEREG: 0,5

OK
AT+CGATT=1
OK
AT+CGACT?
+CGACT: 1,1

OK
connected
AT+USOCR=17
+USOCR: 0

OK
AT+USOLI=0,5683
OK
connected udp
AT+USOST=0,"<my-domain>",5683,34,"040000000133339F4100005042B4B15965040000000166669E4100005042B9B15965"
+USOST: 0,34

OK
AT+USOCL=0,1
ERROR
Force shutting down modem
AT+CFUN=0
OK
AT+CGATT=0
OK
AT+CPWROFF
OK
New interation
New measurement
waiting between msmnts
New measurement
waiting between msmnts
AT
OK
init...
AT
OK
AT+CMEE=0
OK
AT+CFUN=0
OK
AT+CPIN?
+CPIN: READY

OK
// here it stops

```

This is my code:

```cpp
#include <MKRNB.h>
#include <RTCZero.h>

#include "StandByManager.h"
#include "DHTHandler.h"
#include "Config.h"

// initialize the library instance
NBClient client;
GPRS gprs;
NB nbAccess(true);
NBUDP udp;
NBScanner scanner;

RTCZero rtcZero;

DHT dht(Config::DHT_DATA_PIN, Config::DHT_TYPE);
Measurement measurements[Config::DHT_MEASUREMENT_CAPACITY];
DHTHandler dhtHandler(dht, measurements, rtcZero);

bool properWakeup(bool skip = false);

void connectToNetwork(bool initRequired = true)
{
    auto connected = false;

    // MODEM.begin();

    Serial.println(F("init..."));
    while (!connected)
    {

        if (
            nbAccess.begin(Config::GSM_APN_PIN, Config::GSM_APN, false, true) == NB_READY &&
            gprs.attachGPRS() == GPRS_READY)
        {
            connected = true;
            Serial.println(F("connected"));
        }
        else
        {
            Serial.println(F("Not connected, retying"));
            delay(5000);
        }
    }
}

void disconnectFromNetwork()
{
    Serial.println(F("Force shutting down modem"));
    delay(2000);
    MODEM.send("AT+CFUN=0");
    delay(2000);

    gprs.detachGPRS(true); // set to false for async

    nbAccess.shutdown();
    nbAccess.secureShutdown();
}

bool properWakeup(bool skip){

    // reset module?
    MODEM.begin(false);
    MODEM.hardReset();

    //digitalWrite(SARA_PWR_ON, LOW);
    //digitalWrite(SARA_RTS, LOW);
    //digitalWrite(SARA_CTS, HIGH);
    //digitalWrite(SARA_RESETN, LOW);
    //digitalWrite(SARA_DTR, HIGH);

    //MODEM.send("AT+CFUN=0");
    //MODEM.waitForResponse(5000);
    //MODEM.send("AT+CFUN=15");
    
    //MODEM.waitForResponse(5000);
    //MODEM.send("AT+CFUN=1");
    //MODEM.waitForResponse(5000);

    return true;
}

void setup()
{
    delay(5 * 1000);

    Serial.begin(115200);
    rtcZero.begin();
    Serial.println("Begin setup");

    // updateRadioAccessTechnology();

    connectToNetwork(false);

    rtcZero.setEpoch(nbAccess.getTime());

    disconnectFromNetwork();

    // endless event loop
    while (true)
    {
        Serial.println(F("New interation"));
        for (size_t i = 0; i < Config::DHT_MEASUREMENT_CAPACITY; i++)
        {
            Serial.println(F("New measurement"));
            auto result = dhtHandler.performMeasurement();

            if (result > 0)
            {
                break;
            }
            Serial.println(F("waiting between msmnts"));

            // rtc interferes iwth serial
            delayStandByManager.waitInStandBy(rtcZero, Config::DHT_MEASUREMENT_PERIOD_SEC);
        }

        properWakeup();
        connectToNetwork(false);

        if (udp.begin(Config::COAP_PORT))
        {
            Serial.println(F("connected udp"));
            delay(2000);
            udp.beginPacket(Config::COAP_HOSTNAME, Config::COAP_PORT);
            // todo send multiple packets if one packet exceeds 300bytes
            delay(2000);
            udp.write((uint8_t *)measurements, sizeof(measurements));
            delay(2000);
            udp.endPacket();
            delay(2000);
            udp.stop();
        }

        disconnectFromNetwork();
        // todo just cap powe
    }
}

void loop() {}

```

Do you have any idea how I can properly shut down the modem and power it on properly? And if that does not work? How can I completely power the Modem off and on again? Because if I cut the power and reconnect it again it works well once.

You can be assured that power supply is not an issue. I power it with 10.4V of batteries and have a generous amount of 5mF of capacitors in series. It even works for a few seconds only from the capacitors when I unplug it.

 ![WhatsApp Bild 2023-11-19 um 09.07.44_b08cdfd6](https://europe1.discourse-cdn.com/arduino/original/4X/a/e/8/ae85afd270303ecc5b188a97921eed7a179ad272.jpeg)

---

<div class="post-metadata">

**Author:** ![intstarep](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/intstarep/32/238798_2.png) [@intstarep](https://forum.arduino.cc/u/intstarep)\
**Post date:** [November 24, 2023, 5:42am UTC](https://forum.arduino.cc/t/sara-modem-freezes-when-restarted/1190881/2 "2023-11-24T05:42:32Z")

</div>

IMHO you working with the big hammer here with repower the modem just to make it work again. The modem is it's own computer and you have only small control over that AT console and practical no debug options (ERROR is all you get). If the modem reacts other than forseen by the NB lib (Needs more time, whatever state, just the AT responses in unexpected order ans so on) the system is stuck (Maybe not, you don't know really whats going on with the modem by just listening the AT port).  
After a long fight i found my solution with MQTT. I use the fantastic

> **[GitHub - 256dpi/arduino-mqtt: MQTT library for Arduino](https://github.com/256dpi/arduino-mqtt)**
>
> MQTT library for Arduino. Contribute to 256dpi/arduino-mqtt development by creating an account on GitHub.

lib with the regular Arduino mkknb and mkrgsm and just try to reconnect if the mobile network or the socket fails. Works for me.  
I also started a fork with a class extension to use the internal MQTT stack from the modem:

> **[GitHub - CptHolzschnauz/MKRNB: NBMQTT is a new class for the MKRNB library to...](https://github.com/CptHolzschnauz/MKRNB)**
>
> NBMQTT is a new class for the MKRNB library to use the internal MQTT functionality of the SARA R410 modem - GitHub - CptHolzschnauz/MKRNB: NBMQTT is a new class for the MKRNB library to use the int...

which can be also a way but it's not finished yet (Contributions welcome).  
So, i have no answer for you question but thats how i got out of the same misery you're in ;=)

---

<div class="post-metadata">

**Author:** ![jgartner](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jgartner/32/290558_2.png) [@jgartner](https://forum.arduino.cc/u/jgartner)\
**Post date:** [November 25, 2023, 10:56am UTC](https://forum.arduino.cc/t/sara-modem-freezes-when-restarted/1190881/3 "2023-11-25T10:56:44Z")

</div>

Thanks! I will definetly look into these libraries you posted. But I'm not too interested in using MQTT due to it's overhead compared to UDP. See this illustration. Green is the actual payload:

 ![grafik](https://europe1.discourse-cdn.com/arduino/original/4X/1/e/5/1e5683ffef50944488f40443661c3e5cd2cc3953.png)

If you use TLS on such constrained devices it gets even worse:

 ![grafik](https://europe1.discourse-cdn.com/arduino/original/4X/6/9/d/69d9b0bed288dc9ed3a396d0e418a3590a72e8e5.png)

Compare that to using pure UDP:

 ![grafik](https://europe1.discourse-cdn.com/arduino/original/4X/9/5/9/959b3b2be341b64197fbd8255bc1a05793ff10f5.png)

I have a server that accepts UDP connections and in my case the binary data gets converted to entries in a database. But the server that accepts these connections could easily transform them into UDP messages. Making it way more energy and data efficient.

---

<div class="post-metadata">

**Author:** ![intstarep](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/intstarep/32/238798_2.png) [@intstarep](https://forum.arduino.cc/u/intstarep)\
**Post date:** [November 25, 2023, 12:17pm UTC](https://forum.arduino.cc/t/sara-modem-freezes-when-restarted/1190881/4 "2023-11-25T12:17:18Z")

</div>

Ok. As i told you, i just described my way out of this hell. I lost nearly 4 month fighting the modem issues.

You are right, there is a data overhead but in times of 3G 4G 5G i don't care.

I see the pros of MQTT and IMHO there are a lot. I don't want to fight with the socket and the UDP stack.

Best wishes for your project!

---

<div class="post-metadata">

**Author:** ![RichardNordkvelde](https://avatars.discourse-cdn.com/v4/letter/r/439d5e/32.png) [@RichardNordkvelde](https://forum.arduino.cc/u/RichardNordkvelde)\
**Post date:** [December 27, 2023, 12:49pm UTC](https://forum.arduino.cc/t/sara-modem-freezes-when-restarted/1190881/5 "2023-12-27T12:49:42Z")

</div>

I am in the same boat when it comes to the SARA module getting in a bad state, and I've tried several ways on how to powering on and off in a proper manner. Regardless, all methods I've tested makes the module going into a bad state (i.e. the board doesn't respond to any AT commands).  
I can however assume that if the NB is not connected I could drop the "hammer" (hardReset), but regardless it won't get back into a proper state. @intstarep, I couldn't see in your library that you're doing anything special on this manner compared to the MKRNB library provided from Arduino, but you don't have any issues with the powering on and off? I have custom boards based on the NB 1500, so I have a newer firmware on the device (L0.0.00.00.05.12,A.02.19), and according to Arduino they don't support it (but I don't understand why).

---

<div class="post-metadata">

**Author:** ![intstarep](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/intstarep/32/238798_2.png) [@intstarep](https://forum.arduino.cc/u/intstarep)\
**Post date:** [December 27, 2023, 7:05pm UTC](https://forum.arduino.cc/t/sara-modem-freezes-when-restarted/1190881/6 "2023-12-27T19:05:01Z")

</div>

My addition is a unfinished MQTT class that let you control the built in MQTT stack of the modem. This was once an attempt to solve the mess.

But my way was then to only use MQTT with a proper lib as explained. I do all the other stuff (Send SMS, log, proceed the data etc.) on the server side with node.js.

So, i use the regular MKRNB connection method (Try, if failed do the loop and try again) send data to MQTT topics and subscribe to another topic to receive data.

With the new MKRNB release, Arduino merged an IMHO important detail i found in the datasheets (Cpt. Holzschnauz is my second me (in total about 23 personalities:=)) which allows to close tcp sockets async. That can solve some issues.

So i just open tcp sockets which allows me to use the same method for MKR 1010, 1400 and 1500.  
I dropped the 1500 because the is IMHO not enough support for LTE-M all over the world.  
With 2G and 3G i am satisfied to send my +- 5 MB data per day.

This setup is stable (One board run over a year before power recycle for a firmware update), the hammer method is IMHO a no go.

Since Sandeep Mistry is out of the MKRNB dev, Arduino is not able/willing to adapt the lib to the new firmware, i think the MKR1500 is long forgotten..with my method it's no problem because i just use the tcp part, not the modem stuff (+AT myAss etc.)

I really can't understand why Arduino is not picking a new full ublox LTE Modem and put it on a 1500 PCB as the new MKR1600, I think the new modems are pin compatible..

Again: Arduino, we would like a MKR1600 with full LTE and I2S please!!

Disclaimer: I had some glasses of red wine before writing this post...

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 24, 2024, 7:05pm UTC](https://forum.arduino.cc/t/sara-modem-freezes-when-restarted/1190881/7 "2024-06-24T19:05:43Z")

</div>

Closed on June 24, 2024, 7:05pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
