arduino-libraries / MKRGSM

GNU Lesser General Public License v2.1
55 stars 51 forks source link

MKRgsm modem check loop forever #36

Closed eduardcabanas closed 6 years ago

eduardcabanas commented 6 years ago

Hi,

I'm using the following sketch to send some info to my server. I would like to put the arduino in sleep mode after sending data to save battery (not implemented in the current sketch) If everything goes ok, no problem but the problem comes when for example the antenna is broken (the unit is outside). When the GSM access(if (gsmAccess.begin() != GSM_READY) {) wants to connect due the antenna is broken there is no possibility to connect because the Arduino is trying and trying to connect forever.

// libraries

#include <MKRGSM.h>             // GSM library
#include <ArduinoHttpClient.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[]      = "internet.easym2m.eu";
const char GPRS_LOGIN[]    = "";
const char GPRS_PASSWORD[] = "";

// get this from the wia dashboard. it should start with `d_sk`
const char* device_secret_key = "*********************";

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

// Thebluedots API parameters
char server[] = "********.io";
char path[] = "/api/v2/gprs";
int port = 11880;

HttpClient httpClient = HttpClient(client, server, port);

// Various variables used in this Sketch
String response = "";
String okmsg = "ok";
String errormsg = "error";

void setup() {
delay(3000);
  // initialize serial communications and wait for port to open:
  Serial.begin(115200);
    int i;
    for (i = 0; i < 5; i++) {
  Serial.print("Connecting GSM network...");
  if (gsmAccess.begin() != GSM_READY) {
    Serial.println(errormsg);
    Serial.println(i);

    while (true);
  } else {
    Serial.println(okmsg);
    break;
  }
      delay(1000);
    }

}

void loop() {
  // start GSM shield
  // if your SIM has PIN, pass it as a parameter of begin() in quotes
 Serial.print("Connecting GSM network...");

  // Attach GPRS and check if it works
  if (Serial) Serial.println("Attaching to GPRS with your APN...");
  if (gprsAccess.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) != GPRS_READY) {
    if (Serial) Serial.println(errormsg);

    gsmAccess.shutdown();

  } else {
    // GPRS successfully attached
    if (Serial) Serial.println(okmsg);

    // 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 = 80.88;
    float t;
    t = 25.80;

    // 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 THEBLUEDOTS.io...");
    int res_connect;

    res_connect = client.connect(server, port);

    // If connection is successful, POST JSON data
    if (res_connect) {

      //                data.printTo(PostData);
      httpClient.beginRequest();
      httpClient.post(path);
      httpClient.sendHeader("Content-Type", "application/json");
      //                httpClient.sendHeader("Content-Length", data.measureLength());
      httpClient.sendHeader("Authorization", "Bearer "  + String(device_secret_key));
      httpClient.beginBody();
      httpClient.print(PostData);
      httpClient.endRequest();

      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()) {
          if (Serial) Serial.println("Shuttign down GSM connection: disconnected");
          client.stop();
          test = false;
        }
      }
    } else {
      // if we didn't get a connection to the server
      if (Serial) Serial.println(errormsg);
    }
  }

  // Switching off GSM Access
  gsmAccess.shutdown();
  Serial.println("sleeping");

  // Wait for 3mins until we send the next sensor reading
  delay(3 * 60000);
}
void dummy() {
  volatile int aaa = 0;
}

How could I stop the connection? this is the response

Connecting GSM network...⸮AT

OK
AT+IPR=921600

OK
AT

OK
AT+UPSV=3

OK
AT+CPIN?

ERROR
AT+CPIN?

ERROR
AT+CPIN?

ERROR
AT+CPIN?

+CPIN: READY

OK
AT+CMGF=1

OK
AT+UDCONF=1,1

OK
AT+CTZU=1

OK
AT+UDTMFD=1,2

OK
AT+CREG?

+CREG: 0,0

OK
AT+CREG?

+CREG: 0,0

OK
AT+CREG?

+CREG: 0,0

Thank you very much for your help I'm really stuck with this issue

eduardcabanas commented 6 years ago

HI, Any news from this issue? I have my units out of service due this problem thnk's

Rocketct commented 6 years ago

Hi @eduardcabanas, i had try your sketch it works, i have removed the stop condition in the for in setup:(while(true)), i have also imposed in the in the loop() this condition: if (gsmAccess.begin() == GSM_READY && gprsAccess.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY) {

because if you call thegsmAccess.shutdown(); you have also to reconnect the module to the GSM network, for stop the connection you have to manage the problem from sketch, what you do in the loop() function, is repeated as if is in a while(true) loop, then you could simply impose if the module are not able to connect in 'n' tentative it stop to work and go in loop or imposing a condition that inhibit , the condition.

about the


+CREG: 0,0```

for the MKR GSM have you a battery pack connected to the board?

sandeepmistry commented 6 years ago

Maybe we need a timeout mechanism for the connection?

I'm not too keen to set a default timeout, as sometimes the modem can takes minutes to track a signal.

Maybe a new API like GSMAccess::setConnectionTimeout(long timeout), where timeout is a value in milliseconds, and -1 (which is the default) = no timeout.

FrancMunoz commented 6 years ago

Hi @sandeepmistry , I like the approach of GSMAccess::setConnectionTimeout(long timeout). Let me know if I could help.

Rocketct commented 6 years ago

@sandeepmistry yes i think that could be a good solution

sandeepmistry commented 6 years ago

@eduardcabanas @FrancMunoz @Rocketct there's a pull request for the set timeout API in https://github.com/arduino-libraries/MKRGSM/pull/53 - feedback welcome :)

Rocketct commented 6 years ago

@sandeepmistry tested the PR https://github.com/arduino-libraries/MKRGSM/pull/53, unplugging the antenna and setting the timeoput, the changes works fine, the software wait the timeout and after turn back on the loop function in the sketch, now i have to rewrite the sketch of @eduardcabanas and test with it

Rocketct commented 6 years ago

tested also with the @eduardcabanas sketch, i have add the setTimeout before the gsm begin and it works, the sketch is the following:

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

const char PINNUMBER[]     = "";

const char GPRS_APN[]      = "APN";
const char GPRS_LOGIN[]    = "";
const char GPRS_PASSWORD[] = "";

const char* device_secret_key = "*********************";

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

// Thebluedots API parameters
char server[] = "****";
char path[] = "***";
int port = 80;

HttpClient httpClient = HttpClient(client, server, port);

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

void setup() {
  delay(3000);

  Serial.begin(115200);
  int i;
  //setted a timeout of 180 s for the GPRS and GSM operation
  gprsAccess.setTimeout(180000);
  gsmAccess.setTimeout(180000);
  for (i = 0; i < 5; i++) {
    Serial.print("Connecting GSM network...");
    if ((gsmAccess.begin() != GSM_READY) &&
    (gprsAccess.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) != GPRS_READY)) {

    Serial.println(errormsg);
      Serial.println(i);
    } else {
      Serial.println(okmsg);
      break;
    }
    delay(1000);

    if (gsmAccess.status() != GSM_READY) {
    while (true);
    }
  }
}

void loop() {

  Serial.print("Connecting GSM network...");

  if (Serial) Serial.println("Attaching to GPRS with your APN...");
  if ((gsmAccess.begin() != GSM_READY) &&
            (gprsAccess.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) != GPRS_READY)) {

    if (Serial) {
      Serial.println(errormsg);
    }

    gsmAccess.shutdown();

  } else {

    if (Serial) {
      Serial.println(okmsg);
    }

    float h = 80.88;
    float t;
    t = 25.80;

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

    if (Serial) Serial.print("Connecting and sending POST request to THEBLUEDOTS.io...");
    int res_connect;

    res_connect = client.connect(server, port);

    if (res_connect) {

      httpClient.beginRequest();
      httpClient.post(path);
      httpClient.sendHeader("Content-Type", "application/json");

      httpClient.sendHeader("Authorization", "Bearer "  + String(device_secret_key));
      httpClient.beginBody();
      httpClient.print(PostData);
      httpClient.endRequest();

      if (Serial) Serial.println(okmsg);

      if (Serial) Serial.print("Receiving response...");
      boolean test = true;
      while (test) {

        if (client.available()) {
          char c = client.read();
          if (Serial) Serial.print(c);
        }

        if (!client.connected()) {
          if (Serial) Serial.println("Shuttign down GSM connection: disconnected");
          client.stop();
          test = false;
        }
      }
    } else {

      if (Serial) Serial.println(errormsg);
    }
  }

  gsmAccess.shutdown();
  Serial.println("sleeping");

  delay(60000);
}

void dummy() {
  volatile int aaa = 0;
eduardcabanas commented 6 years ago

Nice!!!

Rocketct commented 6 years ago

Closing in favour of https://github.com/arduino-libraries/MKRGSM/pull/53

Rocketct commented 6 years ago

Closing in favour of https://github.com/arduino-libraries/MKRGSM/pull/53