Waking up on timer AND interrupt



  • For battery operated node is it possible to set node to wake up on timer to send battery status AND wake up on DIN3 interrupt to report digital input status? I see example how to wake up on timer for battery status reporting and other example on waking up on digital input interrupt, but not sure how to combine them together.


  • Mod



  • I have not tested it myself, but I think getSleepRemaining() is what you are looking for. It returns the time in ms that is remaining in the current sleep cycle if the device woke up by an interrupt. This seems to be implemented for AVR (this includes Arduinos) only.

    Something similar to this should work:

    #define INT_PIN 3
    #define SLEEP_TIME 9000000 // 15 min
    uint32_t sleepTime = SLEEP_TIME;
    int8_t wakeupReason = 0;
    void loop()
    {
    	if (wakeupReason == digitalPinToInterrupt(INT_PIN)) 
    	{
    		sleepTime = getSleepRemaining();
    		// Report sensor status here
    	}
    	else if (wakeupReason == MY_WAKE_UP_BY_TIMER)
    	{
    		sleepTime = SLEEP_TIME;
    		// Report battery level here
    	}
    	wakeupReason = sleep(digitalPinToInterrupt(INT_PIN), CHANGE, sleepTime);
    }
    

    Since sleep() returns the wake up reason, we can check whether it was caused by the external interrupt on D3 or because the timer ran out and set the sleep time for the next period accordingly.

    So, a node with a sleep time of 15 minutes, which is interrupted after 10 minutes, should send the sensor status and go back to sleep for another 5 minutes, report the battery status and reset the sleep timer to the full duration of 15 minutes.



  • @BearWithBeard
    Wow. Thanks. So many times, I have wanted a function like this, but I did not know it existed. I'm going to have to go back to the documentation and see what other good tricks I have missed.


  • Mod

    Great clear example @BearWithBeard

    I took the liberty to add that example to the documentation.



  • @mfalkvidd Thank you, works great!



  • @BearWithBeard Thank you for sharing this great tip! - It works great and maybe finally I get my lightning detector working!

    Thanks @mfalkvidd For putting this in with the docs where it will hopefully help others. We need more examples like this in a knowledge base to save time reading through lots of posts that are many years old.


Log in to reply
 

Suggested Topics

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts