How to stop routine when connection fails

Hi,

I'm reading data from some sensors and send the info to the server....after that the arduino enters in sleep mode.

if every thing goes ok, there is no problem, but the problem comes when an error happens. If there is no connection or client could not connect the arduino did not go to sleep, remains on

I'm sure is an stupid issue, but I inverted a lot of time and i could not fix

Thank's for your help

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  Serial.begin(115200);

  // Wait for 1mins to see if Serial is connected, otherwise run the Sktech without Serial debugging
  int i;
  for (i = 0; i < 60; i++) {
    if (Serial) break;
    delay(1000);
  }

  // start GSM shield
  if (Serial) Serial.print("Connecting GSM network...");
  if (gsmAccess.begin() != GSM_READY) {
    if (Serial) Serial.println(errormsg);
    LowPower.sleep(3 * 60 * 1000);
    while (true);
  } else {
    if (Serial) Serial.println(okmsg);

    // 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();
      LowPower.sleep(3 * 60 * 1000);
    } else {
      // GPRS successfully attached
      if (Serial) Serial.println(okmsg);


      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 ...");
      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");
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED off
  LowPower.sleep(3 * 60 * 1000);

}

The value you compute hereLowPower.sleep(3 * 60 * 1000);is not what you think it is (if you are on a UNO type of platform) use 3*60*1000[color=red][b]ul[/b][/color]

Hi
@J

J-M-L:
The value you compute hereLowPower.sleep(3 * 60 * 1000);is not what you think it is (if you are on a UNO type of platform) use 3*60*1000[color=red][b]ul[/b][/color]

I'm using MKR GSM 1400, and I don't understand your answer,

Thank's

Eduard

If all the values/variables in a calculation are integers, the result will be cast as an integer. In your case, 3, 60 and 1000 are all integers, but you multiply them together and you get 180,000 which is long. Without casting one of your values as long (hence the ul) your result will overflow.

Hi,

I just put the number without multiply but still having the same problem.

My question is, how to break an if condition.I'm using if X=true then else go to sleep and stop following indexed if conditions

Thank's

Hi

On the MKR platform int are on 32 bits so you don’t have the ul issue

Why do you do this is the GSM does not work

if (Serial) Serial.println(errormsg);
    LowPower.sleep(3 * 60 * 1000);
    while (true);

—> why sleep and then loop for infinity (watchdog will likely reboot)

For the rest we would need your full code...

HI

the idea is to go to sleep the mk when the connection or client connection did not work. How is now, when the connection fails the arduino remains open forever.

If the arduino could not connect, go to sleep and try to connect after "X" minutes, meanwhile to safe battery go to sleep.

Many many thanks

E

see the full code

// libraries
#include <ArduinoLowPower.h>
#include <MKRGSM.h>             // GSM library
#include <ArduinoHttpClient.h>

// PIN Number
const char PINNUMBER[]     = ""; //blank if no pin
const char GPRS_APN[]      = "**************************";
const char GPRS_LOGIN[]    = "";
const char GPRS_PASSWORD[] = "";


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

GSMClient client;
GPRS gprsAccess;
GSM gsmAccess;

// Thebluedots API parameters
char server[] = "****************";
char path[] = "*************";
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(5000);
  LowPower.attachInterruptWakeup(RTC_ALARM_WAKEUP, dummy, CHANGE);
  // initialize serial communications and wait for port to open:

}
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  Serial.begin(115200);

  // Wait for 1mins to see if Serial is connected, otherwise run the Sktech without Serial debugging
  int i;
  for (i = 0; i < 60; i++) {
    if (Serial) break;
    delay(1000);
  }

  // start GSM shield
  if (Serial) Serial.print("Connecting GSM network...");
  if (gsmAccess.begin() != GSM_READY) {
    if (Serial) Serial.println(errormsg);
    LowPower.sleep(3 * 60 * 1000);
    while (true);
  } else {
    if (Serial) Serial.println(okmsg);

    // 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();
      LowPower.sleep(3 * 60 * 1000);
    } else {
      // GPRS successfully attached
      if (Serial) Serial.println(okmsg);


      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 ...");
      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");
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED off
  LowPower.sleep(3 * 60 * 1000);

}

Where is the dummy() function?
Don’t use the String class esp for what you have to build
You might need some time for Serial to reconnect after sleep

Hi

I used more time in serial, but this is not the origin of the issue, I don't know if there is no gsm connection goes to sleep and wait for next time.

If forget to put into the message, sorry. Dummy is a dummy function that lowpower needs, this is not the problem.

Thank's

// libraries
#include <ArduinoLowPower.h>
#include <MKRGSM.h>             // GSM library
#include <ArduinoHttpClient.h>

// PIN Number
const char PINNUMBER[]     = ""; //blank if no pin
const char GPRS_APN[]      = "**************************";
const char GPRS_LOGIN[]    = "";
const char GPRS_PASSWORD[] = "";


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

GSMClient client;
GPRS gprsAccess;
GSM gsmAccess;

// Thebluedots API parameters
char server[] = "****************";
char path[] = "*************";
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(5000);
  LowPower.attachInterruptWakeup(RTC_ALARM_WAKEUP, dummy, CHANGE);
  // initialize serial communications and wait for port to open:

}
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  Serial.begin(115200);

  // Wait for 1mins to see if Serial is connected, otherwise run the Sktech without Serial debugging
  int i;
  for (i = 0; i < 60; i++) {
    if (Serial) break;
    delay(1000);
  }

  // start GSM shield
  if (Serial) Serial.print("Connecting GSM network...");
  if (gsmAccess.begin() != GSM_READY) {
    if (Serial) Serial.println(errormsg);
    LowPower.sleep(3 * 60 * 1000);
    while (true);
  } else {
    if (Serial) Serial.println(okmsg);

    // 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();
      LowPower.sleep(3 * 60 * 1000);
    } else {
      // GPRS successfully attached
      if (Serial) Serial.println(okmsg);


      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 ...");
      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");
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED off
  LowPower.sleep(3 * 60 * 1000);

}
void dummy() {
  volatile int aaa = 0;
}

You should not put the Serial.begin in the loop but in the setup

You should get rid of serial for the time being and connect a couple led, and have those led show status, like blinking or off etc.

(And I would not use the String class although at this stage this is not your problem)

Hi,

I would like to do a timer using millis. The routine has to connect to a gsm station, if it is not possible during these 10 seconds, mark task as not connection

I'm using this code but i'm not sure:

Thank's

void loop() {

  long previousMillis = 0;        
  long interval = 10000;           // interval 

  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis > interval) {
    
    previousMillis = currentMillis;
    // start GSM shield
    // if your SIM has PIN, pass it as a parameter of begin() in quotes
    Serial.print("Connecting GSM network...");
    if (gsmAccess.begin() != GSM_READY) {
      Serial.println(errormsg);
      follow = 0;
    } else {
      if (Serial) Serial.println(okmsg);
      follow=1;
    }
  }

Hello ecabanas

If there is no connection or client could not connect the arduino did not go to sleep, remains on

  if (gsmAccess.begin() != GSM_READY) {
    if (Serial) Serial.println(errormsg);
    LowPower.sleep(3 * 60 * 1000);
    while (true);
  } else {

Do you see "errormsg" ?
Could add a test print after while (true); ?

Could you disassemble your sketch and post the resultWindows, linux,...?
Which system do you use (Windows, linux,...) ?

Regards,
bidouilleelec

Hi

Th eproblem is simple, gsmAccess.begin() did not do nothing when tries to connect. To test it I disconnect the anthena, and run the sketch, only this part with some serials to check when the sketch fails.

So, the result is when the sketch arrives to gsmAccess.begin(), stops here and the micro remains open forever

to check I'm using these sketch:

// libraries
#include <MKRGSM.h>

// PIN Number
#define PINNUMBER ""

// initialize the library instance
GSM gsmAccess;     // include a 'true' parameter for debug enabled

void setup()
{delay(5000);
  // initialize serial communications
  Serial.begin(9600);

  // connection state
  boolean notConnected = true;
Serial.println(notConnected);
  // Start GSM shield
  // If your SIM has PIN, pass it as a parameter of begin() in quotes
  while(notConnected)
  {
    if(gsmAccess.begin()==GSM_READY){
      notConnected = false;
      Serial.println("Connected to network");
    }
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }
}

The solution is a counter, for example for int 4 times but the problems is when arrives to gsmaccess.(begin) the sketc stops to receive response from gsm network...that never will come due the anthenna is disconnected

thank's