http-get.ino

Arduino MKR GSM 1400 · File : http-get.ino · Language : Arduino · Use case : Récupérer une page ou API HTTP via la data GPRS

HTTP client over the MKR GSM 1400 GPRS data link: join the network, attach GPRS, then issue a GET request to an open weather API. The raw response prints on the serial port, ready to parse for a standalone dashboard.

Hardware

An Arduino MKR GSM 1400 (SAMD21 microcontroller, 2G/GPRS cellular modem, external antenna, powered by a LiPo battery on the J6 connector or over USB). A SIM with a data option (2G IoT plan), the 2G antenna, and Internet access to reach the remote API.

Wiring

No external wiring; just check the operator APN in the code, it differs between IoT carriers.

How it works

attachGPRS negotiates a data bearer and IP address. GSMClient opens a TCP socket on port 80; the HTTP header is written by hand, then the stream is read until disconnect or a 15 s timeout.

Running it

In the Arduino IDE, install the Arduino SAMD Boards package, select the MKR GSM 1400 board, then upload. Network examples assume an active data SIM is inserted, the antenna is screwed on, and 2G coverage still exists in your area.

Variants

Switch to HTTPS with GSMClientSecure and setCertificate, publish to your own server, or point the sketch at any 2G-friendly HTTP endpoint.

http-get.ino

// Requête HTTP GET via GPRS (MKR GSM 1400, bibliothèque MKRGSM).
#include <MKRGSM.h>

const char PIN_SIM[] = "0000";
const char APN[] = "sl2g"; // APN data 2G de votre opérateur

GSM gsm;
GSMGPRS gprs;
GSMClient client;

const char HOTE[] = "api.open-meteo.com";
const char CHEMIN[] = "/v1/forecast?latitude=45.0&longitude=2.0&current=temperature_2m";

void setup() {
  Serial.begin(9600);
  while (!Serial);
  if (gsm.begin(PIN_SIM) != GSM_READY) while (true);
  while (gsm.waitNetworkReady(30) != ON_NET);
  if (gprs.attachGPRS(APN, GPRS_AUTOMATIC_BEARER, 1) != GPRS_READY) {
    Serial.println(F("GPRS KO"));
    while (true);
  }
  if (client.connect(HOTE, 80)) {
    client.print(String("GET ") + CHEMIN + " HTTP/1.1\r\n");
    client.print("Host: " + String(HOTE) + "\r\n");
    client.print("Connection: close\r\n\r\n");
    unsigned long debut = millis();
    while (client.connected() && millis() - debut < 15000) {
      while (client.available()) Serial.write(client.read());
    }
    client.stop();
  } else Serial.println(F("Connexion TCP KO"));
}

void loop() {}

← Back to the board