Freeze during put request

Hi @heligone, i have modified the sketch proposed before:

  • Now it is able to retry the connection when the antenna disconnect or loose signal; consider that my code actually (in the sketch) don't have timeout or counter for count the number of re-connection, it try to reconnect forever until when the modem return registered status, you could manage this simply adding in the loop() a counter and choose to do nothing or what you need;
  • When you disconnect the antenna it try to open a new socket, if not possible it return on the sketch, the time required for this action seems long but each time that i have tried to disconnect the antenna, it always was able to:
  1. follow to run and restart to communicate with the server if i reconnect the antenna;
  2. goes in the else condition of if (gsm.isAccessAlive()) in the main loop;

In the sketch that i propose following i have added some comments, if you look in the loop all the action is subordinated to if (gsm.isAccessAlive()) this check the CREG value and if it is different from a value that represent the registered status to the network it return false, in this way if +CREG=0 case happen when you are in the main loop it allow you to impose your condition in the main sketch, consider that the modem manage automatically the value of CREG, then could you try the code from your side and let me know if work?

/*
  Simple PUT client for ArduinoHttpClient library
  Connects to server once every five seconds, sends a PUT request
  and a request body

  created 14 Feb 2016
  by Tom Igoe

  update by M. Sibert May 2018

  this example is in the public domain
*/

#include <MKRGSM.h>
#include <ArduinoHttpClient.h>
#include "arduino_secrets.h"



const String serverName(("httpbin.org"));
const unsigned serverPort = 80;
const String servicePath(("/put"));

const String GPRS_APN((APN_NAME));
const String GPRS_LOGIN((APN_USERNAME));
const String GPRS_PASSWORD((APN_PASSWORD));

unsigned long initTime = millis();

GSM gsm(true);
GPRS gprs;
GSMClient gprsClient;

HttpClient client = HttpClient(gprsClient, serverName, serverPort);

const String contentType = "application/json";
const String putData = "{ \"name\": \"light\", \"age\": 46 }";
unsigned long int count = 0;

void setup() {
  Serial.begin(115200);

  MODEM.begin();
  boolean connected = false;
  while (!connected) {
    if ((gsm.begin(PIN_CODE) == GSM_READY) &&
        (gprs.attachGPRS(GPRS_APN.c_str(), GPRS_LOGIN.c_str(), GPRS_PASSWORD.c_str()) == GPRS_READY)) {
      connected = true;
    } else {
      Serial.println("Not connected");
      delay(1000);
    }
  }
  Serial.println("Connected");
}

void loop() {
  
  /* Here is checked if the connection is lost, simply check the CREG status
     there you can also count the tentative number until when you could stop to reconect and do other
     you could do somthing like this also in the setup() imposing some condition in the line:45 in the "while (!connected)"
  */
  if (gsm.isAccessAlive()) {

    Serial.println("Making PUT request");
    int ret = client.put(servicePath.c_str(), contentType, putData);
    if (ret < 0) {
      client.flush();
      client.stop();
      return;
    }
    unsigned long looptime = millis();
    /*
      changing the condition here the sketch don't freeze when the connection is lost and the antenna is detached 
    */
    while (client.available() <= 0 && millis() - looptime < 5000) {
    }

    // read the status code and body of the response
    const int statusCode = client.responseStatusCode();
    const String response = client.responseBody();
    if (statusCode != 200) {
      client.flush();
      client.stop();
    } else {
      Serial.print("Status code: ");
      Serial.println(statusCode);
      Serial.print("Response: ");
      Serial.println(response);
      Serial.print(count);
      count++;
    }

    unsigned long start = millis();
    Serial.print("Time ");
    Serial.println((millis() - initTime) / 1000);
    while (client.connected())
    {

      Serial.println("Connected!!!");
      delay(100);
      if (millis() - start > 5000)
      {
        client.flush();
        client.stop();
      }
    }
  } else {
    /*
      here you can implement all the actions that could be done if the connection is lost 
    */
    Serial.println("not connected, loop:sketch "); 
    delay(1000);
  }

}

in my actual test, it run from 4 days, i have disconnected random the antenna and each time that i have reconnected, it was able to establish again the connection with the server, all the problem was managed from sketch, be sure to have the last library version, the only condition that i have change is that in the MKRGSM library in file Modem.cpp in the last line i change the baudrate to 115200 in my test, soon i'll report the results of a long run test with the highest speed possible.