🛠 Envoyer et recevoir des SMS
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
- Arduino MKR GSM 1400 board + deployed U.FL antenna
- Micro-SIM card with a plan including SMS, PIN code disabled
- LiPo battery ≥ 1500 mAh connected
- A phone for round-trip testing
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
- Phone number in national format (06...): use the international format "+33...".
- Full SMS inbox on the SIM:
availablereads block or loop; clear the SIM memory (often 5 to 10 messages) or delete them from a phone. - Long SMS (> 160 characters) or accents: Unicode encoding reduces the useful length; stick to ASCII for commands.
- Empty or incorrect sender number: depending on the carrier, the sender's number may be missing — do not rely on it to authorize a sender without verification.
- Simultaneous SMS + data attempt: the module is in circuit-switched mode during a call/SMS; design clear priorities between SMS and GPRS.
- Current spike during transmission: again, a LiPo battery is mandatory to avoid silent restarts.
Other tutorials for this board
Code examples for this board
gsm-connexion.inoArduinoenvoi-sms.inoArduinohttp-get.inoArduinohttp-post-capteur.inoArduinoheure-gsm.inoArduinoappels.inoArduino