Inconsistent behaviour using LowPower.sleep();

Hi all,

I'm building an 'emergency button' for my elderly parents. A press of the button sends a message over the Sigfox network and then a callback to my email address (or push message, still working that out).

I'm using a MKRFOX1200 board (link) with a Big Red Button (link).

What I like to achieve when the button is pressed:

  • light up the button LED for x seconds to signal a message was sent
  • consistent behaviour (system must work the same even after a 'standby' of weeks/months)
  • separate function to drive an external alarm sound or flashing light (not in the code yet but maybe you guys have a good solution which solves the problem and can address this :slight_smile: )

When I turn the board on and do the following, it works as expected:

  1. press button
  2. LED turns on and stays on for about 10 seconds
  3. message is sent over the Sigfox network. The Sigfox message sequence ID is increased by 1. This is normal as each message received increases that sequence number. Previous message was 59, this message ID is now 60.
  4. I receive an email (which is persistently pinned on my phones screen).
  5. LED turns off (after the 10 seconds)
  6. board goes into lowpower sleep mode waiting for interrupt on pin 1 (the button)

If I Push the button again within 2 or 3 mins, the same result and this is good.

Now, when I try it, say 30 mins later, then the following happens:

  1. press button
  2. LED turns on while the button is pressed and when the button is released the LED goes off immediately
  3. a message is sent over the Sigfox network. The message sequence ID is now increased by 2 (so it skips a number). Previous Id was 60, this message is now number 62
  4. I receive an email (which is persistently pinned on my phones screen).
  5. the board goes into lowpower sleep mode (I assume as I can't see it happening.

This is not what I expect and I can't figure out why this happens. Maybe something to do with the sleep mode and the board doesn't wake up properly.

This is the code (so far):

//#include <OneButton.h> // used to enable multi-click events from a single button
#include <SigFox.h>
#include <ArduinoLowPower.h>

#define PIN_INPUT 1
#define PIN_LED 3

// current LED state, staring with LOW (0)
//int ledState = LOW;

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

volatile int alarm_source = 0;

// Variables for battery SoC calculation
float voltage = 0;
int sensorValue = 0;
uint8_t battery_percentage = 0;

// setup code here, to run once:
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) {}
Serial.println("Debug is true, Starting setup");

}

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 1 to a switch and enable the interrupt on voltage rising event
pinMode(1, INPUT_PULLUP);
LowPower.attachInterruptWakeup(1, alarmEvent1, RISING);

// enable the LED output on the defined pin_led
pinMode(PIN_LED, OUTPUT); // sets the digital pin as output

// set the LED output to the value in ledState. Mainly used for the doubleclick action to reverse the LED state.
//digitalWrite(PIN_LED, ledState);

} // setup

void alarmEvent1() {
alarm_source = 1;

Serial.println("Click button");
digitalWrite(PIN_LED, HIGH);
// battery SoC calculation
analogReadResolution(10);
analogReference(AR_INTERNAL1V0);

sensorValue = analogRead(ADC_BATTERY);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 4.3V):
// if you're using a 3.7 V Lithium battery pack - adjust the 3 to 3.7 in the following formulas
voltage = sensorValue * (3 / 1023.0);

//battery percentage calculation
// 2.2 is the cutoff voltage so adjust it if you're using a 3.7 V battery pack

battery_percentage = ((voltage - 2.2) / (3 - 2.2)) * 100;

analogReference(AR_DEFAULT);
}

// main code here, to run repeatedly:
void loop()
{
// Sleep until an event is recognized
LowPower.sleep();

// 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
// 414c4d = ALM + 313030 = 100 + 1 or 2 for the alarm_source so example 414c4d3130301
String to_be_sent = "ALM" + String(battery_percentage) + String(alarm_source);

// sending the payload to Sigfox server
SigFox.beginPacket();
SigFox.print(to_be_sent);
int ret = SigFox.endPacket();

// shut down module, back to standby
digitalWrite(PIN_LED, LOW);
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

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

// End