LowPower.sleep(120000) does not work

Hello,

"LowPower.sleep(120000)" doesn't work.

Thanks for your help...

/*
  SigFox Event Trigger tutorial

  This sketch demonstrates the usage of a MKRFox1200
  to build a battery-powered alarm sensor with email notifications

  A couple of sensors (normally open) should we wired between pins 1 and 2 and GND.

  This example code is in the public domain.
*/

#include <SigFox.h>
#include <ArduinoLowPower.h>

// Set debug to false to enable continuous mode
// and disable serial prints
int debug = true;

volatile int alarm_source = 0;

void setup() {

  if (debug == true) {

    // We are using Serial1 instead than Serial because we are going in standby
    // and the USB port could get confused during wakeup. To read the debug prints,
    // connect pins 13-14 (TX-RX) to a 3.3V USB-to-serial converter

    Serial1.begin(115200);
    while (!Serial1) {}
  }

  if (!SigFox.begin()) {
    //something is really wrong, try rebooting
    reboot();
  }

  //Send module to standby until we need to send a message
  SigFox.end();

  if (debug == true) {
    // Enable debug prints and LED indication if we are testing
    SigFox.debug();
  }

  // attach pin 0 and 1 to a switch and enable the interrupt on voltage falling event
  pinMode(0, INPUT_PULLUP);
  LowPower.attachInterruptWakeup(0, alarmEvent1, FALLING);

  pinMode(1, INPUT_PULLUP);
  LowPower.attachInterruptWakeup(1, alarmEvent2, FALLING);
}

void loop()
{
  // Sleep until an event is recognized
  LowPower.sleep(120000);
  Serial1.println("BEGIN");

  // if we get here it means that an event was received

  SigFox.begin();

  if (debug == true) {
    Serial1.println("Alarm event on sensor " + String(alarm_source));
  }
  delay(100);

  // 3 bytes (ALM) + 8 bytes (ID as String) + 1 byte (source) < 12 bytes
  String to_be_sent = "ALM" + SigFox.ID() +  String(alarm_source);

  SigFox.beginPacket();
  SigFox.print(to_be_sent);
  int ret = SigFox.endPacket();

  // shut down module, back to standby
  SigFox.end();

  if (debug == true) {
    if (ret > 0) {
      Serial1.println("No transmission");
    } else {
      Serial1.println("Transmission ok");
    }

    Serial1.println(SigFox.status(SIGFOX));
    Serial1.println(SigFox.status(ATMEL));

    // Loop forever if we are testing for a single event
    //while (1) {};
    
  }
}

void alarmEvent1() {
  alarm_source = 1;
}

void alarmEvent2() {
  alarm_source = 2;
}

void reboot() {
  NVIC_SystemReset();
  while (1);
}

Please explain exactly what you mean by "doesn't work".

When your code requires a library that's not included with the Arduino IDE please always post a link (using the chain link icon on the toolbar to make it clickable) to where you downloaded that library from or if you installed it using Library Manger(Sketch > Include Library > Manage Libraries) then say so and state the full name of the library.

ArduinoLowPower repository:

It means : The MKRFOX never goes out of sleep mode. Normally, it should go out of sleep mode after 120000 ms (2 mins).

Try to add this line in your setup() function :

LowPower.attachInterruptWakeup(RTC_ALARM_WAKEUP, alarmEvent0, CHANGE);

And create a dummy function :

void alarmEvent0() {
  alarm_source = 0;
}

It works ! Thanks :grin:

With this interrupt, When the arduino will wake up, it'll start all the sketch from beginning ? or just the loop ?

astroman35:
Try to add this line in your setup() function :

LowPower.attachInterruptWakeup(RTC_ALARM_WAKEUP, alarmEvent0, CHANGE);

And create a dummy function :

void alarmEvent0() {

alarm_source = 0;
}

Thanks astroman35,
I was looking a lot for a solution to this problem and you saved me finally.

Astroman, you saved my day!
I don´t really understand your solution, but it worked
Thanks a lot.

Hiya!

I am trying to use low power to shut down my arduino mkrfox1200 , but every time I use low power the board powers down indefinitely.
I have looked at this thread and used the dummy function but can't seem to wake up the board then. I also then can't find the board on the IDE to select as a port... so cannot upload anything else to the board. I have tried using the reset button whilst uploading but to no avail.

Any solutions would be greatly appreciated!

Try double-pressing reset button. Orange led should start blinking and then you can select COM port and upload new sketch.

Good Luck!