freeze with +UUPSDD: 0 return

Hello,

Here am I again with a new error message after a freeze.

I include the whole self-contained sketch below.

Please note that iyou should use MKRGSM libray as modified by francmunoz (30 seconds timeout on the AT+CREG? initial connection loop to avoid freeze if no network)

The board sends data from temp and humidity sensor DHT22 every 30 seconds to soracom Havest IOT cloud. (It is only working with a soracom simcard, but you can adapt easily to send any dummy information to a server of your choice)

There is an option to use a powersaving library ArduinoLowPower.h, but it is not working so I did not activate it and at the end of main loop there is a simple delay(30000).

It worked fine for 16 hours, that is approximately 2000 request , then it froze and the last debug on the console was :

`OK
AT+CREG?+CREG: 0,5

OK
AT+UCALLSTAT=1OK
GSM OK
Attaching to GPRS with your APN...AT+CGATT=1OK
AT+UPSD=0,1,"soracom.io"OK
AT+UPSD=0,2,"sora"OK
AT+UPSD=0,3,"sora"OK
AT+UPSD=0,7,"0.0.0.0"OK
AT+UPSDA=0,3OK
AT+UPSND=0,8+UPSND: 0,8,1

OK
ok
Humidity: 47.40 %
Temperature: 26.40
Connecting and sending POST request to harvest.soracom.io...AT+USOCR=6+USOCR: 0

OK
AT+USOCO=0,"harvest.soracom.io",80OK
AT+USOWR=0,15,"504F5354202F20485454502F312E31"+USOWR: 0,15

OK
AT+USOWR=0,2,"0D0A"+USOWR: 0,2

OK
AT+USOWR=0,24,"486F73743A20686172766573742E736F7261636F6D2E696F"+USOWR: 0,24

OK
AT+USOWR=0,2,"0D0A"+USOWR: 0,2

OK

+UUSOCL: 0

+UUPSDD: 0`

If I understand well the documentation,
+UUSOCL: 0 means : socket closed
+UUPSDD: 0 means that the network has closed the connection.

///////////////////////////////////////////////////////////////Libraries definition//////////////////////////////////////////////////////
// test connection répétée à soracom Harvest en envoyant données du cpateur temp et humidité
// marche avec carte soracom uniquement 
// possibilité de tester ArduinoLowPower (au 24/08/2018, ne marche pas, utilisation d'un simple délaià la fin de la boucle principale)


#include <ArduinoLowPower.h>
#include <MKRGSM.h>
#include <ArduinoHttpClient.h>
#include <ArduinoJson.h>
#include <DHT.h> 

// PIN Number
const char PINNUMBER[] = ""; //blank if no pin
// APN data: check settings on mobile for sim provider or contact carrier for network settings
const char GPRS_APN[] = "soracom.io";
const char GPRS_LOGIN[] = "sora";
const char GPRS_PASSWORD[] = "sora";

// DHT Sensor settings
#define DHTPIN            0        // Pin which is connected to the DHT sensor.
// Uncomment the type of sensor in use:
//#define DHTTYPE           DHT11     // DHT 11
#define DHTTYPE           DHT22     // DHT 22 (AM2302)
//#define DHTTYPE           DHT21     // DHT 21 (AM2301)
//Set to 1 if you would like temperature in Fahrenheit
#define FAHRENHEIT 0
// Initialise DHT Library
DHT dht(DHTPIN, DHTTYPE);

GSMClient client;
GPRS gprsAccess;
GSM gsmAccess(true);

// https://github.com/alexissusset/soracom-mkrgsm1400
// Setting URL to http://harvest.soracom.io
// it also works with TCP but as we'd like to push two sensor values at the same time,
// we'll use HTTP POST method, Harvest detects number in payloads for you could post a simple
// "number" message or otherwise (as we do in this example) a JSON payload with names and values
char url[] = "harvest.soracom.io";
char path[] = "/";

// String response;
String response = "";
String okmsg = "ok";
String errormsg = "error";

void setup() {
  delay(4000);
  Serial.println("Init ...");
  pinMode(LED_BUILTIN, OUTPUT);
  LowPower.attachInterruptWakeup(RTC_ALARM_WAKEUP, dummy, CHANGE);
  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
}
void loop() {

  digitalWrite(LED_BUILTIN, HIGH);

  Serial.println("Connecting GSM ...");

  if (gsmAccess.begin(PINNUMBER) != GSM_READY) {
    Serial.println("GSM Error");
  }

  else {
    Serial.println("GSM OK");
    
    // Attach GPRS and check if it works
    if (Serial) Serial.print("Attaching to GPRS with your APN...");
    if (gprsAccess.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) != GPRS_READY) {
      if (Serial) Serial.println("GPRS connexion Error");
      gsmAccess.shutdown();

    } else {
      // GPRS successfully attached
      if (Serial) Serial.println(okmsg);
      
      
      // https://github.com/alexissusset/soracom-mkrgsm1400
      // Read sensor data and send to soracom Harvest
      // Reading temperature or humidity takes about 250 milliseconds!
      // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
      float h = dht.readHumidity();
      float t;
      if(FAHRENHEIT){
        t = dht.readTemperature(true);
      }else{
        t = dht.readTemperature();
      }

      // If Serial is connected, display current readings
      if(Serial) Serial.print("Humidity: ");
      if(Serial) Serial.print(h);
      if(Serial) Serial.println(" %\t");
      if(Serial) Serial.print("Temperature: ");
      if(Serial) Serial.println(t);

      // Prepare post data
      String PostData = "{\"Temperature\":"+ String(t) +",\"Humidity\":"+ String(h) +"}";

      // Connect to Soracom harvest
      if(Serial) Serial.print("Connecting and sending POST request to harvest.soracom.io...");
      int res_connect;

      res_connect = client.connect(url, 80);

      // If connection is successful, POST JSON data
      if (res_connect) {
        client.println("POST / HTTP/1.1");
        client.println("Host: harvest.soracom.io");
        client.println("User-Agent: Arduino/1.0");
        client.println("Connection: close");
        client.print("Content-Length: ");
        client.println(PostData.length());
        client.println();
        client.println(PostData);

        if(Serial) Serial.println(okmsg);

        // Read abd print the response from the server
        if(Serial) Serial.print("Receiving response...");
        boolean test = true;
        while (test) {
          // if there are incoming bytes available, print them
          if (client.available()) {
            char c = client.read();
            if(Serial) Serial.print(c);
          }

          // if the server's disconnected, stop the client:
          if (!client.connected()) {
            client.stop();
            Serial.println("Shutting down GSM connection: disconnected (client.stop())");
            test = false;
          }
        }
      } else {
        // if we didn't get a connection to the server
        if(Serial) Serial.println(errormsg);
      }
    }
  }
  digitalWrite(LED_BUILTIN, LOW);
  // delay(300000);
//  LowPower.sleep(15 * 60000);
//  LowPower.sleep(30000);
  Serial.println("delay ...");
  delay(30000);

}

// Called at wakeUp form LowPower.sleep
void dummy() {
  volatile int aaa = 0;
}