Arduino MKR 1500 + NB Client

Hi,

MKR 1500 + NB Library.

I am having issues in my code when a new connection is attempted to be established at client.connect(server, port);

Arduino gets frozen when a new connection is made without internet access. It simply never respond. There is no timeout.

Code:

// libraries
#include <MKRNB.h>

// initialize the library instance
NBClient client;
GPRS gprs;
NB nbAccess;

// URL, path and port (for example: example.org)
char server[] = "example.org";
char path[] = "/";
int port = 80; // port 80 is the default for HTTP

void setup() {
  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("Starting Arduino web client.");
  // connection state
  boolean connected = false;

/*
  while (!connected) {
    if ((nbAccess.begin() == NB_READY) && (gprs.attachGPRS() == GPRS_READY)) {
      connected = true;
    } else {
      Serial.println("Not connected");
      delay(1000);
    }
  }
*/

  Serial.println("connecting...");

  //if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    Serial.print((char)client.read());
  }

  // if you get a connection, report back via serial:
  if (client.connect(server, port)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.print("GET ");
    client.print(path);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Connection: close");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }

  //if the server's disconnected, stop the client:
  if (!client.available() && !client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  
}
}

void loop() {

}

As you can see, I have commented out the block of the code which connects to the internet on purpose. As I understand, the line client.connect(server, port) shouldn't hang forever, it should go to else condition and print a message saying "connection failed". This is a very common scenario where internet is not available 24x7. It seems a library bug.

Does anybody have ever faced this issue before? Is there a workaround?

Thank you!

As I understand, the line client.connect(server, port) shouldn't hang forever, it should go to else condition and print a message saying "connection failed".

Wrong, if you want that behavior you have to call the library correctly:

NBClient client(false);

Otherwise the NBClient.connect() method waits until the internet connection gets ready, if you explicitly prohibit this it will wait forever.

Thank you so much for the advice Pylon. I will test!.

Cheers.