Skip to main content

Example - HTTP Get Request

This document demonstrates a simple HTTP GET call to a webserver, specifically to get the current time as The amount of seconds passed since January 1st 1970unix time. It's assumed that the development enviroment has been correctly configured. The API worldtimeapi.org offers this service.

The full example can be found in the Arduino IDE under File -> Examples -> AVR-IoT-Cellular -> http_get_time or on the GitHub page.

Connecting to the cellular network

Before any HTTP requests can be made, the modem must be connected to the cellular network. This is usually done in the setup() function. In the code below we also start the LED controller and the logging module. This is just beneficial to get any error messages whilst connecting to the network and have the LEDs light up when we are connected.

// Start up the LEDs and display a start up animation
LedCtrl.begin();
LedCtrl.startupCycle();

// Start the logging system to get messages logged
Log.begin(115200);

// Start modem
Lte.begin();
If the modem doesn't connect

When starting the board for the first time, the modem needs to search for a compatible network. If the modem doesn't connect after five minutes, make sure of the following:

  • The SIM card
    • Is properly inserted into the board
    • Is activated
  • If the bundled Truphone SIM card is used: the board is located in a region with LTE-M coverage. See Truphone's website for a coverage list.
  • If an NB-IoT SIM card is used: make sure the modem firmware was been upgraded to the dual mode (LTE-M + NB-IoT) version. See instructions here.

HTTP Communication

Configuring the HTTP Client

To make the GET request to the worldtimeapi.org API, the HTTP Client must be configured to do so.

if (!HttpClient.configure("worldtimeapi.org", 80, false))
{
Log.errorf("Failed to configure the http client for the domain %s\r\n", "worldtimeapi.org");
return;
}

Sending the GET Request

When the client is configured, the GET request is performed with the HttpClient.get() call. Notice that only the Uniform Resource IdentifierURI is specified, as the domain was configured through the client. This example gets the current time for Oslo, Norway.

HttpResponse response;
response = HttpClient.get("/api/timezone/Europe/Oslo.txt");
if (response.status_code != HttpClient.STATUS_OK)
{
Log.errorf("Error when performing a GET request on %s/%s. Got status code = %d. Exiting...",
"worldtimeapi.org",
"/api/timezone/Europe/Oslo.txt\r\n",
response.status_code);
return;
}

Reading the Response

The response body from a GET request is read with a HttpClient.readBody() call.

String body = HttpClient.readBody(512);
if (body == "")
{
Log.error("The returned body from the GET request is empty. Something went wrong. Exiting...");
return;
}

Parsing the Response

Responses from a GET request vary depending on what resource is being requested. In this case, the parameters are seperated by a newline (\n). The request is parsed to find the unixtime variable.

long getTimeFromApiresp(String *resp)
{
int unixTimeIndex = resp->indexOf(String("unixtime: "));
int utx_datetimeIndex = resp->indexOf(String("utc_datetime"));

String substr = resp->substring(unixTimeIndex + 10, utx_datetimeIndex - 1);
return substr.toInt();
}

Log.infof("Got the time (unixtime) %lu\r\n", getTimeFromApiresp(&body));

Putting it All Together

Live Editor

tip

The contents of the live editor can be changed and compiled in the browser. Click compile at the bottom of the editor, and drag & drop the Provided hex-file onto the Curiosity Drive.

Live Editor
#include <Arduino.h>
#include <http_client.h>
#include <lte.h>
#include <sequans_controller.h>
#include <log.h>
#include <led_ctrl.h>
#define TIMEZONE_URL "worldtimeapi.org"
#define TIMEZONE_URI "/api/timezone/Europe/Oslo.txt"
long getTimeFromApiresp(String *resp)
{
int unixTimeIndex = resp->indexOf(String("unixtime: "));
int utx_datetimeIndex = resp->indexOf(String("utc_datetime"));
String substr = resp->substring(unixTimeIndex + 10, utx_datetimeIndex - 1);
return substr.toInt();
}
void setup()
{
// Start up the LEDs
LedCtrl.begin();
LedCtrl.startupCycle();
Log.begin(115200);
Log.info("Starting HTTP Get Time Example");
// Start modem and connect to the operator
Lte.begin();
if (!HttpClient.configure(TIMEZONE_URL, 80, false))
{
Log.errorf("Failed to configure the http client for the domain %s\r\n", TIMEZONE_URL);
return;
}
Log.info("Configured to HTTP");
HttpResponse response;
response = HttpClient.get(TIMEZONE_URI);
if (response.status_code != HttpClient.STATUS_OK)
{
Log.errorf("Error when performing a GET request on %s%s. Got status code = %d. Exiting...\r\n", TIMEZONE_URL, TIMEZONE_URI, response.status_code);
return;
}
Log.infof("Successfully performed GET request. Status Code = %d, Size = %d\r\n", response.status_code, response.data_size);
String body = HttpClient.readBody(512);
 
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
No action performed

Serial Terminal

Unsupported Browser
Your browser does not support the online serial terminal. Supported browsers are Google Chrome, Microsoft Edge and Opera.

Expected Output

If you do not know how to find these logs, see Reading Logs.

[INFO] Starting HTTP Get Time Example
[INFO] Connecting to operator... OK!
[INFO] Configured to HTTP
[INFO] Successfully performed GET request. Status Code = 200, Size = 359
[INFO] Got the time (unixtime) 1638458488