sleep-gsm.ino

Arduino MKR GSM 1400 · File : sleep-gsm.ino · Language : Arduino · Use case : Réduire la consommation entre deux envois avec les alarmes SAMD

Battery station running in cycles: the MKR GSM 1400 wakes every five minutes via the SAMD21 RTC alarm, powers the modem down between readings and sleeps in deepSleep. The reference build for months of autonomy.

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). LiPo on J2, antenna, and patience: the first full modem wake takes a few seconds.

Wiring

No extra wiring; remove unused LEDs and sensors to cut quiescent current.

How it works

LowPower.attachInterruptWakeup hooks the callback to the RTC alarm; deepSleep stops CPU and peripherals. gsm.shutdown() cuts modem power, the main consumer (up to 2 A peaks).

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

Also wake on a sensor (external interrupt), tune the period to criticality, or keep the modem in PSM instead of powering it down if your operator supports it.

sleep-gsm.ino

// Sommeil profond entre deux releves : alarmes SAMD + modem eteint.
#include <MKRGSM.h>
#include <ArduinoLowPower.h>
#include <RTCZero.h>

const char PIN_SIM[] = "0000";
const char APN[] = "sl2g";

GSM gsm; GSMGPRS gprs; GSMClient client;
RTCZero rtc;

volatile bool reveille = false;

void onReveil() { reveille = true; }

void setup() {
  Serial.begin(9600);
  if (gsm.begin(PIN_SIM) != GSM_READY) while (true);
  rtc.begin();
  LowPower.attachInterruptWakeup(RTC_ALARM_WAKEUP, onReveil, RISING);
}

void loop() {
  reveille = false;
  mesuresEtEnvoi();          // reveil -> mesurer -> envoyer
  gsm.shutdown();            // coupe le modem (gros gain)
  LowPower.deepSleep();      // attend l'alarme RTC
  rtc.setAlarmEvery(5 * 60); // prochaine mesure dans 5 min
  rtc.enableAlarm(rtc.MATCH_SS);
}

void mesuresEtEnvoi() {
  Serial.println(F("Reveil : mesure + envoi"));
  // apres deepSleep, reattacher le reseau puis poster la mesure
  while (gsm.waitNetworkReady(30) != ON_NET);
  gprs.attachGPRS(APN, GPRS_AUTOMATIC_BEARER, 1);
  // ... client.connect / POST ...
}

← Back to the board