🛠 HTTP GET via GPRS
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
- Arduino MKR GSM 1400 board + U.FL antenna
- Data micro-SIM with a known APN, PIN code disabled
- LiPo battery ≥ 1500 mAh
- MKRGSM library; secrets in the IDE's Secret tab
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
- Reading the body before it's complete: loop on
availablewith an inactivity timeout rather than a single instantaneouswhileloop in a fast main loop. - Forgetting the
Host:header in HTTP/1.1: modern hosts return a 400 error. - Targeting an HTTPS site with plain
GSMClient: the connection fails or returns a 301 redirect; switch to SSL or use a test HTTP API. - Exhausted data SIM or restricted APN: the TCP connection is established but the transfer remains empty.
- Reusing the client without calling
stopbetween two requests: clear the buffer or recreate the connection. - 3G cut off: actual GPRS throughput is very low (fallback to 2G), plan for generous timeouts on both the server and sketch sides.
Other tutorials for this board
Code examples for this board
gsm-connexion.inoArduinoenvoi-sms.inoArduinohttp-get.inoArduinohttp-post-capteur.inoArduinoheure-gsm.inoArduinoappels.inoArduino