Arduino Forum

Products => MKR Boards => MKRFOX1200 => Topic started by: kickouille on Jul 31, 2019, 04:30 pm

Title: MKRFOX power saving issue
Post by: kickouille on Jul 31, 2019, 04:30 pm
Hello,

I have an issue using the MKRFOX board with power saving feature.
I am using the latest libraries and boards.

Simple way to reproduce:
Take EventTrigger example sketch, and change this:

LowPower.sleep();

to this:

LowPower.sleep(60000);

Result: I get two frames at 60 seconds interval, but never the third, a reset is necessary, and same behaviour continues.

After further troubleshooting I have identified that following part of code never returns after sending the second frame (second frame which is actually received by the backend).

I have also noticed that I only encounter this with timed wakeups, not event wakeups.

I also added this to be sure, but that didn't help:
LowPower.attachInterruptWakeup(RTC_ALARM_WAKEUP, alarmEvent0, CHANGE);

While troubelshooting, I added this in the setup:
SigFox.debug();

And this time I got consistent results.

I then read in documentation (https://www.arduino.cc/en/Reference/SigFoxDebug) that Enabling the debugging all the power saving features are disabled.

That doesn't impact SAMD power saving features.

So the WeatherMonitor example sketch which comes with Sigfox library simply doesn't work.

Is anybody able to reproduce or tell what is going wrong?

Yanik
Title: Re: MKRFOX power saving issue
Post by: pert on Jul 31, 2019, 05:09 pm
For anyone else looking at this, I think this is the EventTrigger example sketch kickouille mentioned:
https://www.arduino.cc/en/Tutorial/SigFoxEventTrigger (https://www.arduino.cc/en/Tutorial/SigFoxEventTrigger)

I know there is a problem with sleep and Serial, but I don't think it affects Serial1. Did you add any use of Serial to the sketch?
https://github.com/arduino-libraries/ArduinoLowPower/issues/7 (https://github.com/arduino-libraries/ArduinoLowPower/issues/7)
Title: Re: MKRFOX power saving issue
Post by: kickouille on Aug 01, 2019, 02:51 pm
Hi Pert,

Thank you for your answer.

Also as reference, this sketch provided by Sigfox library, using timed wakeups doesn't, I get the first frame, then the second frame after 15 minutes, and that stops here:

https://github.com/arduino-libraries/SigFox/tree/master/examples/WeatherMonitor (https://github.com/arduino-libraries/SigFox/tree/master/examples/WeatherMonitor)

Reading again my first post, I realized I didn't mention where that stops in the code, this call never returns if any timed sleep has occured before:
int ret = SigFox.endPacket();

This call also uses timed wakeups for managing the timeout... So I'm wondering if that could not be the problem, calling a sleep elsewhere in the code (in another class for instace), while primary code just woke up. Not sure exactly why. And this call in the setup disables power saving features from Sigfox class, making it all working as expected:

SigFox.debug();

Concerning your remark about the Serial or Serial1, the code is not using it at all.

Title: Re: MKRFOX power saving issue
Post by: riccalisci on Aug 09, 2019, 11:25 am
Hi everyone,
I have the exact same problem with you as a kickouille.
Did you manage to fix it?

thanks
Title: Re: MKRFOX power saving issue
Post by: kickouille on Aug 10, 2019, 07:02 pm
Hi,

I've not been able to fix it.
However I did some troubleshooting, including doing a simple blink program using sleep instead of delay, and within this program make a call to a method of another class which also runs a low power sleep, and that doesn't seem to be a problem.

That definitely looks to be a combination of the two timed wakeups with Sigfox, not sure where exactly.

Now I have to debug Sigfox class and put some markers to exactly identify where it blocks.
Title: Re: MKRFOX power saving issue
Post by: kickouille on Aug 11, 2019, 11:51 pm
Hello,

Ok I've found where that blocks.

In SigFox.cpp, function:
int SIGFOXClass::send(unsigned char mess[], int len, bool rx)

There is following block:

Code: [Select]
  if (!debugging) {
#ifdef SIGFOX_SPI
    LowPower.attachInterruptWakeup(interrupt_pin, NULL, FALLING);
//LowPower.attachInterruptWakeup(RTC_ALARM_WAKEUP, alarmEvent0, CHANGE);
    LowPower.sleep(timeout);
digitalWrite(LED_BUILTIN, HIGH);
#endif
    if (digitalRead(interrupt_pin) == 0) {
  status();
      ret = statusCode(SIGFOX);
    }
    goto exit;
  }


You can see a call to status() block:

Code: [Select]
void SIGFOXClass::status()
{
  digitalWrite(chip_select_pin, LOW);
  spi_port->beginTransaction(SPICONFIG);
  spi_port->transfer(0x0A);
  spi_port->transfer(0);
  ssm = spi_port->transfer(0);
  atm = spi_port->transfer(0);
  sig = spi_port->transfer(0);
  sig2 = spi_port->transfer(0);
  spi_port->endTransaction();
  digitalWrite(chip_select_pin, HIGH);
  delay(1);
}


It appears that program stops working right after:
  spi_port->transfer(0x0A);

However I have noticed that the problem exists even if there is no LowPower feature used in the main program.

At this point, my feeling is that SPI is not recovering as it should after SAMD is woken up for the second time.

I confirm that if Sigfox library is being put in debug mode, power saving features are disabled (as shown in the code above), and all is working good in this case.

So I believe there is not problem with Sigfox library.

To be tested: check if the MKR is able to talk to an SPI device after the first sleep, and after the second sleep.
Title: Re: MKRFOX power saving issue
Post by: kickouille on Aug 14, 2019, 02:01 pm
Hello,

I found a workaround, I still have to check if there is maybe a problem with the ArduinoLowPower library and another SPI device in a timed sleep.

I added SPI related lines in following portions of code in SigFox.cpp:

Code: [Select]
#ifdef SIGFOX_SPI
    spi_port->end();
    LowPower.attachInterruptWakeup(interrupt_pin, NULL, FALLING);
    LowPower.sleep(timeout);
    spi_port->begin();
    spi_port->setDataMode(SPI_MODE0);
    spi_port->setBitOrder(MSBFIRST);
#endif


And I also ended SPI properly in this portion of code:

Code: [Select]
void SIGFOXClass::end()
{
  pinMode(poweron_pin, LOW);
  delay(1);
  digitalWrite(chip_select_pin, LOW);
  delay(1);
  spi_port->beginTransaction(SPICONFIG);
  spi_port->transfer(0x05);
  spi_port->endTransaction();
  delay(1);
  digitalWrite(chip_select_pin, HIGH);
  delay(1);
  spi_port->end();
}


Then everything seems to work as expected!

Yanik
Title: Re: MKRFOX power saving issue
Post by: pert on Aug 14, 2019, 04:20 pm
Thanks for taking the time to share your findings kickouille!
Title: Re: MKRFOX power saving issue
Post by: riccalisci on Sep 02, 2019, 03:47 pm
thank you so much kickouille!!! everything seems to work also in my code.
Title: Re: MKRFOX power saving issue
Post by: marind on Sep 21, 2019, 09:54 pm
Dear kickouille,
I have same problems. I tried your solution and I followed your instructions but now I can not send any messages!? probably did something wrong but I can not see where.

I added SPI related lines on 2 places in Sigfox.cpp file (line 170 and 234) and changed completely inside of void SIGFOXClass::end().

Could you check and advise where I made mistake?
Any chance of providing complete Sigfox.cpp file?

Br

Marin
 
Title: Re: MKRFOX power saving issue
Post by: pacxxi on Oct 01, 2019, 05:39 pm
Thaks, kickouille,  :) 

Works great your solution in my code, I was crazy trying to find one solution for deepsleep.



Title: Re: MKRFOX power saving issue
Post by: BigMike81 on Nov 12, 2019, 01:29 pm
Hello,
can this change in Sigfox.cpp file the reason why is green LED on even on 2xAA batteries? AFAIK, LEDs should be off while on battery power, or not?

Thank you.