Description

attachGPRS(const char* apn, const char* user_name, const char* password, bool synchronous = true);
hostByName(const String &hostname, IPAddress& result);

Asynchronous operation of the GPRS class methods allows to run the task of attaching to the internet concurrently to the main job of your application. This means that your application does not block while waiting for the modem to answer etc. The application starts attachGPRS() and calls the ready() function regularly which gives the GPRS class processing time to handle the connection-process in the background. Its return value tells the user application if the task is finished and if this was successful or not. See the code below as an example. The status() method returns the current operation step the gprs-object is working on (e.g. ERROR, IDLE, CONNECTING, GPRS_READY etc.)

Syntax

gprs.attachGPRS(“APN”, “NAME”, “PASS”, true);
gprs.ready();
gprs.status();

gprs.hostByName( s_hostName, result);
gprs.getIPAddress();

Parameters

attachGPRS:
“APN” stands for the character array holding the APN of the provider / SIM card e.g. “internet.telekom”
“NAME” stands for the character array holding the name of the APN e.g. “t-mobile”
“PASS” stands for the character array holding the password of the APN e.g. “tm”
true – the last parameter starts the attachGPRS() function in asynchronous mode. (false = synchronous (default if omitted) )
see also link

hostByName:
String s_hostName – String holding the hostname to be resolved
IPAddress result – the variable to store the resolved IP address into

Returns

attachGPRS() returns:
ERROR– in case of problems with the modem communication.
CONNECTING – OK, trying to connect to the internet.

ready() returns:
0 – the process is still running
1– OK. Process was successfully finished
2– ERROR. Process could not be completed because an error occurred.

status() returns:
IDLE – modem process is idle
ERROR – an error occurred during the latest modem process (e.g. begin() operation failed)
GPRS_READY – GPRS successfully attached to the internet
CONNECTING – modem is currently trying to attach to the internet

hostByName() returns:
int – 0: error / 1:OK – host could successfully be found

getIPAddress returns:
IPAddress – the resolved IP address of the host requested.

Example Code

This example shows how to start the CAT-M1 & GPRS connection process in an asynchronous way.

#include <DICENB.h>

GSM gsm;
GPRS gprs;

void setup()
{
  gsm.begin("", true, false); // begin in async mode
  while(gsm.ready() == 0) {
    // do other app things (async)
  };
  if(gsm.ready() > 1)
  {
    Serial.println("Error");
    while(1) { yield(); };
  }
  else
    Serial.println("GSM initialized");

  gprs.attachGPRS("internet.telekom", "t-mobile", "tm");
  while(gprs.ready() == 0) {
    // do other app things (async)
  };
  if(gprs.ready() > 1)
  {
    Serial.println("Error");
    while(1) { yield(); };
  }
  else
    Serial.println("GPRS initialized");

  Serial.print("Our IP address: ");
  Serial.println(gprs.getIPAddress());
  IPAddress t_remoteIP;
  gprs.hostByName(String("di-ce.de"), t_remoteIP);
  Serial.println("Resolve di-ce.de: " + String(t_remoteIP));
}

void loop()
 
{
  // once connected do something interesting
}   

Notes and Warnings

Note: the correct APN credentials for the SIM (provider) are important for proper operation. (particularly for LTE-CAT M1)
If the APN is missing or incorrect, network registration and connection appear to work, but no actual data is transmitted.

See also

Arduino – MKRattachGPRS
GSM begin() – synchronously
HTTP_Client Class – recommended Client classes for TCP/IP operations