Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timeouts to GSMClient #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dzindra
Copy link
Contributor

@dzindra dzindra commented Mar 7, 2019

Timeouts on connect and write prevents sketch lockups due to poorly powered modem.

@Rocketct
Copy link
Contributor

Tested by setting, in example GSMWebClient:

  • client.setSocketTimeout(1); and client.setConnectTimeout(1); timeout reached and request not done;
  • client.setSocketTimeout(10000); and client.setConnectTimeout(10000); timeout not reached and request done and showed on the serial monitor;
  • client.setSocketTimeout(100); and client.setConnectTimeout(100); timeout reached and request not done.

The PR works, after the previous tests i have also introduced a little delay higher then the timeout, after the start = millis() where used in the APIs , to force it and also in this case the changes had works;
The sketch used for the test is the following:

/*
  Web client

 This sketch connects to a website through a MKR GSM 1400 board. Specifically,
 this example downloads the URL "http://www.example.org/" and
 prints it to the Serial monitor.

 Circuit:
 * MKR GSM 1400 board
 * Antenna
 * SIM card with a data plan

 created 8 Mar 2012
 by Tom Igoe
*/

// libraries
#include <MKRGSM.h>

#include "arduino_secrets.h" 
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
// PIN Number
const char PINNUMBER[]     = SECRET_PINNUMBER;
// APN data
const char GPRS_APN[]      = SECRET_GPRS_APN;
const char GPRS_LOGIN[]    = SECRET_GPRS_LOGIN;
const char GPRS_PASSWORD[] = SECRET_GPRS_PASSWORD;

// initialize the library instance
GSMClient client;
GPRS gprs;
GSM gsmAccess(true);

// 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
  }
  client.setSocketTimeout(100);
  client.setConnectTimeout(100);
  Serial.println("Starting Arduino web client.");
  // connection state
  bool connected = false;

  // After starting the modem with GSM.begin()
  // attach the shield to the GPRS network with the APN, login and password
  while (!connected) {
    if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &&
        (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
      connected = true;
    } else {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  Serial.println("connecting...");
  // 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");
  }
}

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

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

    // do nothing forevermore:
    for (;;)
      ;
  }
}

@sandeepmistry
Copy link
Contributor

@cmaglie @facchinm any thoughts on this one?

EthernetClient now has a setConnectionTimeout(timeout) API, should we port to other libraries?

https://github.com/arduino-libraries/Ethernet/blob/7b5ee58ba50ae28341111e4641d35076080b8597/src/Ethernet.h#L241

@facchinm
Copy link
Contributor

I think we could adopt setConnectionTimeout() API broadly, maybe even adding it as a (non pure virtual) function to Client base class

@dzindra
Copy link
Contributor Author

dzindra commented Jun 30, 2019

Any progress with this PR? Should I change function name to setConnectionTimeout() to make sure it is compatible with EthernetClient?

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Jindrich Dolezy seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants