🛠 Envoyer et recevoir des SMS

Arduino MKR GSM 1400 · intermediaire · Schéma : IMG-mkr-gsm-1400-intermediaire-2-mkrgsm-sms.svg — MKR GSM 1400 échangeant des SMS avec un téléphone mobile, ondes radio entre l'antenne et le téléphone, message « ALERTE » affiché sur les deux écrans.

Objectives

Use the GSM_SMS class from the MKRGSM library to send an alert SMS to an international number and listen for incoming SMS messages to control the board remotely (text "LED ON" / "LED OFF"). SMS remains the ideal backup channel when GPRS is down: it works even when data is unavailable, as long as 2G coverage exists.

Hardware

Complete code

#include <MKRGSM.h>
#include "arduino_secrets.h"
const char PINNUMBER[] = SECRET_PINNUMBER;
const char NUMERO_DEST[] = "+33612345678";  // format international

GSM     gsmAccess;
GSM_SMS sms;

void setup {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  while (!Serial) {}
  while (gsmAccess.begin(PINNUMBER) != GSM_READY) delay(1000);
  Serial.println("GSM pret, ecoute SMS...");

  // SMS de demarrage
  sms.beginSMS(NUMERO_DEST);
  sms.print("MKR GSM 1400 en ligne.");
  sms.endSMS;
}

void loop {
  if (sms.available) {
    String msg;
    char c;
    while ((c = sms.read) != -1) msg += c;
    msg.trim;
    Serial.println(msg);
    if (msg.equalsIgnoreCase("LED ON"))  digitalWrite(LED_BUILTIN, HIGH);
    if (msg.equalsIgnoreCase("LED OFF")) digitalWrite(LED_BUILTIN, LOW);

    sms.beginSMS(sms.remoteNumber);   // accuse de reception
    sms.print("Recu : " + msg);
    sms.endSMS;
  }
  delay(1000);
}

Expected verification

At startup, the phone receives "MKR GSM 1400 online.". Sending "LED ON" turns on LED D6 and returns an acknowledgment "Received: LED ON"; "LED OFF" turns it off. endSMS blocks until the message is sent or fails: the first send after registration may take several seconds.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials