🛠 HTTP GET via GPRS

Arduino MKR GSM 1400 · intermediaire · Schéma : IMG-mkr-gsm-1400-intermediaire-3-mkrgsm-http-get.svg — Requête HTTP GET partant de la MKR GSM 1400 via l'antenne vers une antenne relais puis un serveur web, la réponse s'affichant sur la console USB.

Objectives

Retrieve a page or API over HTTP on the cellular network using GSMClient + GPRS: the MKRGSM library API intentionally mirrors the classes from the Ethernet/WiFi libraries, so standard Arduino network code ports almost as-is. We write the HTTP request by hand (no HTTP library) to understand the protocol, then note the HTTPS options.

Hardware

Full code

#include <MKRGSM.h>
#include "arduino_secrets.h"
const char PINNUMBER[]     = SECRET_PINNUMBER;
const char GPRS_APN[]      = SECRET_GPRS_APN;
const char GPRS_LOGIN[]    = SECRET_GPRS_LOGIN;   // "" si non requis
const char GPRS_PASSWORD[] = SECRET_GPRS_PASSWORD;

GSMClient client;   // clone de la classe Client (Ethernet/WiFi)
GPRS      gprs;
GSM       gsmAccess;

void setup {
  Serial.begin(9600);
  while (!Serial) {}
  bool connected = false;
  while (!connected) {
    connected = (gsmAccess.begin(PINNUMBER) == GSM_READY) &&
                (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY);
    if (!connected) { Serial.println("Reseau indisponible"); delay(2000); }
  }
  Serial.println("GPRS pret");

  if (client.connect("example.org", 80)) {
    Serial.println("Connecte au serveur");
    client.println("GET / HTTP/1.1");
    client.println("Host: example.org");
    client.println("User-Agent: MKRGSM1400");
    client.println("Connection: close");
    client.println;               // ligne vide = fin d'en-tetes
  } else {
    Serial.println("Connexion refusee");
  }
}

void loop {
  while (client.available) Serial.write(client.read);
  if (!client.connected && !client.available) client.stop;
  while (true) {}   // une seule requete
}

Expected verification

The console displays the HTTP header followed by the page body (the example <!doctype html> from example.org). The connection must be properly closed (Connection: close) so that available eventually returns false. For HTTPS: either use GSMSSLClient (module SSL profiles) or BearSSLClient on the SAMD21, leveraging the embedded ATECC508 crypto chip — note that the root certificate store in older versions of MKRGSM (last version 1.4.2, 2019) had to be updated over time due to CA rotations: update the library.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials