Go Down

Topic: Freeze during put request (Read 3201 times) previous topic - next topic

heligone

#15
Aug 24, 2018, 09:27 am Last Edit: Aug 24, 2018, 10:16 am by heligone
Here are my views concerning this infamous AT+CREG? freeze.

I have several battery packs LI-ION 18650 1S3P, 3.7V   7800mAh.

I check the battery voltage before use.

But I have noticed that even if the battery voltage is correct, which means >4V, if the battery has not been charged for sometimes, the freeze can occur.

This does not happen with a new battery or a just recharged battery.


I understand that the board needs to draw much current when establishing the connection, and that requires a plugged in battery : USB power or solar power is  not enough.

BUT what I dont understand is : If there is no battery connected, or if the battery cannot furnish enough current, the board should not freeze during the initial AT+CREG? loop.

Indeed, for a remote and autonomous IOT project, requiring periodic reconnexion to GSM network, it can always happen that the battery has become a bit weak.

In that case, the freeze will occur at GSM reconnexion and the whole board will freeze and need a manual or watchdog reset.

Maybe this can be adressed progammatically in the gsm.begin() or other parts pof the library

If it is a hardware limitation of SARA chip : time to look for another board ?


heligone

concerning AT+CREG? freeze at GSM  reconnection please check also :

https://forum.arduino.cc/index.php?topic=565177.0

Riccardo_Rizzo

#17
Sep 10, 2018, 05:05 pm Last Edit: Sep 11, 2018, 11:37 am by Riccardo_Rizzo
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?

Code: [Select]
/*
  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.

Go Up