envoi-sms.ino

Arduino MKR GSM 1400 · File : envoi-sms.ino · Language : Arduino · Use case : Envoyer un SMS de notification vers un téléphone

SMS notification from the MKR GSM 1400: the modem texts a message to a phone number, here a heartbeat every five minutes. Handy for alerting without Internet infrastructure, with the target number in international format.

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 an SMS plan, the 2G antenna screwed on, and a phone to receive the messages.

Wiring

Nothing to wire: SIM and antenna in place, powered by USB or a LiPo battery on the J6 connector.

How it works

GSMSMS puts the modem in text mode: sendNumber sets the recipient, then beginMessage/print/endMessage compose and dispatch the SMS. The code uses millis() to space sends five minutes apart.

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

Trigger sends from a sensor alarm, or cap to a few SMS per day to protect your plan; PDU mode allows longer texts but is far more verbose.

envoi-sms.ino

// Envoi d'un SMS avec le MKR GSM 1400 (MKRGSM + classe GSM SMS).
#include <MKRGSM.h>

const char PIN_SIM[] = "0000";
const char NUMERO[] = "+33612345678"; // numero cible au format international

GSM gsm;
GSMSMS sms;

unsigned long dernierEnvoi = 0;
const unsigned long INTERVALLE = 300000; // 5 minutes

void setup() {
  Serial.begin(9600);
  while (!Serial);
  if (gsm.begin(PIN_SIM) != GSM_READY) {
    Serial.println(F("Modem KO"));
    while (true);
  }
  if (sms.begin() != GSM_READY) Serial.println(F("SMS KO"));
  envoyer("MKR GSM 1400 : systeme en ligne");
}

void loop() {
  if (millis() - dernierEnvoi >= INTERVALLE) {
    dernierEnvoi = millis();
    envoyer("Heartbeat OK - uptime " + String(millis() / 1000) + "s");
  }
}

void envoyer(const String &message) {
  sms.sendNumber(NUMERO);
  sms.beginMessage();
  sms.print(message);
  sms.endMessage();
  Serial.println(F("SMS envoye : ") + message);
}

← Back to the board