Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. Waking up on timer AND interrupt

Waking up on timer AND interrupt

Scheduled Pinned Locked Moved Development
7 Posts 5 Posters 120 Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    APL2017
    wrote on last edited by
    #1

    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.

    mfalkviddM 1 Reply Last reply
    0
    • A APL2017

      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.

      mfalkviddM Offline
      mfalkviddM Offline
      mfalkvidd
      Mod
      wrote on last edited by
      #2

      @APL2017 yes. See https://www.mysensors.org/download/sensor_api_20#sleeping for info on how to do it.

      A 1 Reply Last reply
      1
      • BearWithBeardB Offline
        BearWithBeardB Offline
        BearWithBeard
        wrote on last edited by
        #3

        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.

        nagelcN mfalkviddM 2 Replies Last reply
        1
        • BearWithBeardB BearWithBeard

          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.

          nagelcN Offline
          nagelcN Offline
          nagelc
          wrote on last edited by
          #4

          @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.

          1 Reply Last reply
          1
          • BearWithBeardB BearWithBeard

            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.

            mfalkviddM Offline
            mfalkviddM Offline
            mfalkvidd
            Mod
            wrote on last edited by mfalkvidd
            #5

            Great clear example @BearWithBeard

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

            1 Reply Last reply
            3
            • mfalkviddM mfalkvidd

              @APL2017 yes. See https://www.mysensors.org/download/sensor_api_20#sleeping for info on how to do it.

              A Offline
              A Offline
              APL2017
              wrote on last edited by
              #6

              @mfalkvidd Thank you, works great!

              1 Reply Last reply
              0
              • skywatchS Offline
                skywatchS Offline
                skywatch
                wrote on last edited by skywatch
                #7

                @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.

                1 Reply Last reply
                2
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                16

                Online

                11.7k

                Users

                11.2k

                Topics

                113.0k

                Posts


                Copyright 2019 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • MySensors
                • OpenHardware.io
                • Categories
                • Recent
                • Tags
                • Popular