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. My Project
  3. gw.sleep on battery powered magnet door switch

gw.sleep on battery powered magnet door switch

Scheduled Pinned Locked Moved My Project
42 Posts 13 Posters 13.2k Views 16 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.
  • sundberg84S sundberg84

    @Chakkie - Looks right, Do you see anything in the serial debug log when you try?

    ChakkieC Offline
    ChakkieC Offline
    Chakkie
    wrote on last edited by
    #4

    @sundberg84

    I have added some print to debugged. It seems like the code with sleep does not verify and send the high 1 or low 0 when I open and close the contact. See the serial debug log. The repeated sleep in the log is when i open and close the contact. Normally it will send 1 or 0 " send: 204-204-0-0 s=3,c=1,t=16,pt=2,l=2,sg=0,st=ok:0"

    void loop() 
    {
      debouncer.update();
      // Get the update value
      int value = debouncer.read();
    
      if (value != oldValue) {
         // Send in the new value
         Serial.println("Trigger");
         gw.send(msg.set(value==HIGH ? 1 : 0));
         oldValue = value;
      }     
      gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME);
      Serial.println("Sleep");
    
    send: 204-204-0-0 s=255,c=3,t=15,pt=2,l=2,sg=0,st=ok:0
    send: 204-204-0-0 s=255,c=0,t=17,pt=0,l=5,sg=0,st=ok:1.5.4
    send: 204-204-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=ok:0
    read: 0-0-204 s=255,c=3,t=6,pt=0,l=1,sg=0:M
    sensor started, id=204, parent=0, distance=1
    send: 204-204-0-0 s=3,c=0,t=0,pt=0,l=0,sg=0,st=ok:
    Trigger
    send: 204-204-0-0 s=3,c=1,t=16,pt=2,l=2,sg=0,st=ok:0
    Sleep
    Sleep
    Sleep
    Sleep
    Sleep
    Sleep
    Sleep
    Sleep
    

    Raspberry Pi 2
    Domoticz
    RFXCOM
    ZWAVE Aeon stick
    Coming soon Arduino mysensors GW

    AWIA 1 Reply Last reply
    0
    • mfalkviddM mfalkvidd

      The code looks ok to me (except that you might as well set SLEEP_TIME to 0 since the loop only will report on change, regardless of the sleep time).
      Maybe add a few Serial.println statements in different parts of the loop so you can debug what happens?

      ChakkieC Offline
      ChakkieC Offline
      Chakkie
      wrote on last edited by
      #5

      @mfalkvidd Hi thanks

      Changing the sleep time to 0 does not help. When I open and close the contact the status will not be sent. That's weird

      Raspberry Pi 2
      Domoticz
      RFXCOM
      ZWAVE Aeon stick
      Coming soon Arduino mysensors GW

      1 Reply Last reply
      0
      • sundberg84S Offline
        sundberg84S Offline
        sundberg84
        Hardware Contributor
        wrote on last edited by
        #6

        @Chakkie - add a debugline and see what value and oldvalue says.
        See if you can measure the input when you close the contact and when its open so its not a hardware failure.

        Controller: Proxmox VM - Home Assistant
        MySensors GW: Arduino Uno - W5100 Ethernet, Gw Shield Nrf24l01+ 2,4Ghz
        MySensors GW: Arduino Uno - Gw Shield RFM69, 433mhz
        RFLink GW - Arduino Mega + RFLink Shield, 433mhz

        ChakkieC 1 Reply Last reply
        1
        • ChakkieC Chakkie

          @sundberg84

          I have added some print to debugged. It seems like the code with sleep does not verify and send the high 1 or low 0 when I open and close the contact. See the serial debug log. The repeated sleep in the log is when i open and close the contact. Normally it will send 1 or 0 " send: 204-204-0-0 s=3,c=1,t=16,pt=2,l=2,sg=0,st=ok:0"

          void loop() 
          {
            debouncer.update();
            // Get the update value
            int value = debouncer.read();
          
            if (value != oldValue) {
               // Send in the new value
               Serial.println("Trigger");
               gw.send(msg.set(value==HIGH ? 1 : 0));
               oldValue = value;
            }     
            gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME);
            Serial.println("Sleep");
          
          send: 204-204-0-0 s=255,c=3,t=15,pt=2,l=2,sg=0,st=ok:0
          send: 204-204-0-0 s=255,c=0,t=17,pt=0,l=5,sg=0,st=ok:1.5.4
          send: 204-204-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=ok:0
          read: 0-0-204 s=255,c=3,t=6,pt=0,l=1,sg=0:M
          sensor started, id=204, parent=0, distance=1
          send: 204-204-0-0 s=3,c=0,t=0,pt=0,l=0,sg=0,st=ok:
          Trigger
          send: 204-204-0-0 s=3,c=1,t=16,pt=2,l=2,sg=0,st=ok:0
          Sleep
          Sleep
          Sleep
          Sleep
          Sleep
          Sleep
          Sleep
          Sleep
          
          AWIA Offline
          AWIA Offline
          AWI
          Hero Member
          wrote on last edited by
          #7

          @Chakkie debouncer does not function when you use the sleep function. Reason is that sleep puts the internal timer which is used by debouncer to rest. Remove the debouncer routine and debounce by including a gw.wait(40) before going to sleep. This avoids a retrigger by an eventual contact bounce.

          ChakkieC 1 Reply Last reply
          1
          • sundberg84S sundberg84

            @Chakkie - add a debugline and see what value and oldvalue says.
            See if you can measure the input when you close the contact and when its open so its not a hardware failure.

            ChakkieC Offline
            ChakkieC Offline
            Chakkie
            wrote on last edited by
            #8

            @sundberg84 I have debugged a several times. I am now sure it is not a hardware failure.

            Raspberry Pi 2
            Domoticz
            RFXCOM
            ZWAVE Aeon stick
            Coming soon Arduino mysensors GW

            1 Reply Last reply
            0
            • AWIA AWI

              @Chakkie debouncer does not function when you use the sleep function. Reason is that sleep puts the internal timer which is used by debouncer to rest. Remove the debouncer routine and debounce by including a gw.wait(40) before going to sleep. This avoids a retrigger by an eventual contact bounce.

              ChakkieC Offline
              ChakkieC Offline
              Chakkie
              wrote on last edited by
              #9

              @AWI Thank you so much. Yes I have came to the conclusion that the debouncer does not seems to work fine with sleep function. So I have used a method other than debouncer.

              Here the working script below:

              #include <MySensor.h>
              #include <SPI.h>
              
              
              #define CHILD_ID_Deur 11
              #define CHILD_ID_Post 12
              #define BUTTON_PIN_Deur  2  // Arduino Digital I/O pin for button/reed switch
              #define BUTTON_PIN_Post  3  // Arduino Digital I/O pin for button/reed switch
              
              MySensor gw;
              
              
              // Change to V_LIGHT if you use S_LIGHT in presentation below
              MyMessage msgDeur(CHILD_ID_Deur,V_TRIPPED);
              MyMessage msgPost(CHILD_ID_Post,V_TRIPPED);
              
              void setup()  
              {  
                gw.begin();
              gw.sendSketchInfo("Voordeur contacts", "1.0");
               // Setup the button
                pinMode(BUTTON_PIN_Deur,INPUT);
                pinMode(BUTTON_PIN_Post,INPUT);
                // Activate internal pull-up
                digitalWrite(BUTTON_PIN_Deur,HIGH);
                digitalWrite(BUTTON_PIN_Post,HIGH);
              
                // Register binary input sensor to gw (they will be created as child devices)
                // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
                // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
                gw.present(CHILD_ID_Deur, S_DOOR);
                gw.present(CHILD_ID_Post, S_DOOR);   
              }
              
              
              //  Check if digital input has changed and send in new value
              void loop() 
              {
                uint8_t value;
                static uint8_t sentValue = 2;
                static uint8_t sentValue2 = 2;
              
                // Short delay to allow buttons to properly settle
                gw.sleep(5);
              
                value = digitalRead(BUTTON_PIN_Deur);
              
                if (value != sentValue) {
                  // Value has changed from last transmission, send the updated value
                  gw.send(msgDeur.set(value == HIGH ? 1 : 0));
                  sentValue = value;
                }
              
                value = digitalRead(BUTTON_PIN_Post);
              
                if (value != sentValue2) {
                  // Value has changed from last transmission, send the updated value
                  gw.send(msgPost.set(value == HIGH ? 1 : 0));
                  sentValue2 = value;
                }
              
              
                // Sleep until something happens with the sensor
                gw.sleep(BUTTON_PIN_Deur - 2, CHANGE, BUTTON_PIN_Post - 2, CHANGE, 86400000);// changing Secondary button to correct pin (-3) does not work. So keep it on (-2)
                Serial.println("Sleep");
              
              }
              
              

              Notice that the script does not work properly when I set BUTTON_PIN_Post - 3. The button pin post will not trigger. But when I change it to -2, both switches will work

              gw.sleep(BUTTON_PIN_Deur - 2, CHANGE, BUTTON_PIN_Post - 2, CHANGE, 86400000);
              

              Raspberry Pi 2
              Domoticz
              RFXCOM
              ZWAVE Aeon stick
              Coming soon Arduino mysensors GW

              AWIA 1 Reply Last reply
              0
              • ChakkieC Chakkie

                @AWI Thank you so much. Yes I have came to the conclusion that the debouncer does not seems to work fine with sleep function. So I have used a method other than debouncer.

                Here the working script below:

                #include <MySensor.h>
                #include <SPI.h>
                
                
                #define CHILD_ID_Deur 11
                #define CHILD_ID_Post 12
                #define BUTTON_PIN_Deur  2  // Arduino Digital I/O pin for button/reed switch
                #define BUTTON_PIN_Post  3  // Arduino Digital I/O pin for button/reed switch
                
                MySensor gw;
                
                
                // Change to V_LIGHT if you use S_LIGHT in presentation below
                MyMessage msgDeur(CHILD_ID_Deur,V_TRIPPED);
                MyMessage msgPost(CHILD_ID_Post,V_TRIPPED);
                
                void setup()  
                {  
                  gw.begin();
                gw.sendSketchInfo("Voordeur contacts", "1.0");
                 // Setup the button
                  pinMode(BUTTON_PIN_Deur,INPUT);
                  pinMode(BUTTON_PIN_Post,INPUT);
                  // Activate internal pull-up
                  digitalWrite(BUTTON_PIN_Deur,HIGH);
                  digitalWrite(BUTTON_PIN_Post,HIGH);
                
                  // Register binary input sensor to gw (they will be created as child devices)
                  // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
                  // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
                  gw.present(CHILD_ID_Deur, S_DOOR);
                  gw.present(CHILD_ID_Post, S_DOOR);   
                }
                
                
                //  Check if digital input has changed and send in new value
                void loop() 
                {
                  uint8_t value;
                  static uint8_t sentValue = 2;
                  static uint8_t sentValue2 = 2;
                
                  // Short delay to allow buttons to properly settle
                  gw.sleep(5);
                
                  value = digitalRead(BUTTON_PIN_Deur);
                
                  if (value != sentValue) {
                    // Value has changed from last transmission, send the updated value
                    gw.send(msgDeur.set(value == HIGH ? 1 : 0));
                    sentValue = value;
                  }
                
                  value = digitalRead(BUTTON_PIN_Post);
                
                  if (value != sentValue2) {
                    // Value has changed from last transmission, send the updated value
                    gw.send(msgPost.set(value == HIGH ? 1 : 0));
                    sentValue2 = value;
                  }
                
                
                  // Sleep until something happens with the sensor
                  gw.sleep(BUTTON_PIN_Deur - 2, CHANGE, BUTTON_PIN_Post - 2, CHANGE, 86400000);// changing Secondary button to correct pin (-3) does not work. So keep it on (-2)
                  Serial.println("Sleep");
                
                }
                
                

                Notice that the script does not work properly when I set BUTTON_PIN_Post - 3. The button pin post will not trigger. But when I change it to -2, both switches will work

                gw.sleep(BUTTON_PIN_Deur - 2, CHANGE, BUTTON_PIN_Post - 2, CHANGE, 86400000);
                
                AWIA Offline
                AWIA Offline
                AWI
                Hero Member
                wrote on last edited by
                #10

                @Chakkie Good to hear. Using "-3" makes the interrupt number the same for _Deur and _Post. Using -2 activates both interrupt 0 and 1.
                You'r ready for the next project..

                ChakkieC 1 Reply Last reply
                0
                • mfalkviddM Offline
                  mfalkviddM Offline
                  mfalkvidd
                  Mod
                  wrote on last edited by
                  #11

                  We should really get rid of the confusing "- 2" stuff in the examples and start using DigtalPinToInterrupt instead.
                  I'm annoyed enough to see if I can create a pull request later this week.

                  ChakkieC 1 Reply Last reply
                  1
                  • AWIA AWI

                    @Chakkie Good to hear. Using "-3" makes the interrupt number the same for _Deur and _Post. Using -2 activates both interrupt 0 and 1.
                    You'r ready for the next project..

                    ChakkieC Offline
                    ChakkieC Offline
                    Chakkie
                    wrote on last edited by
                    #12

                    @AWI Thanks. By the way would you care to explain the interrupt index number? or may be a link for explanation?

                    Raspberry Pi 2
                    Domoticz
                    RFXCOM
                    ZWAVE Aeon stick
                    Coming soon Arduino mysensors GW

                    AWIA 1 Reply Last reply
                    0
                    • ChakkieC Chakkie

                      @AWI Thanks. By the way would you care to explain the interrupt index number? or may be a link for explanation?

                      AWIA Offline
                      AWIA Offline
                      AWI
                      Hero Member
                      wrote on last edited by AWI
                      #13

                      @Chakkie In a few words.. The ATMEGA has two external interrupts 0 and 1 which are connected to pins 2 and 3. ("-2" translates the pin number to the interrupt number, which is rather confusing as @mfalkvidd meant)

                      ChakkieC 1 Reply Last reply
                      0
                      • mfalkviddM mfalkvidd

                        We should really get rid of the confusing "- 2" stuff in the examples and start using DigtalPinToInterrupt instead.
                        I'm annoyed enough to see if I can create a pull request later this week.

                        ChakkieC Offline
                        ChakkieC Offline
                        Chakkie
                        wrote on last edited by
                        #14

                        @mfalkvidd Thanks for the info. Does DigitalPinTolnterrupt works better?

                        Raspberry Pi 2
                        Domoticz
                        RFXCOM
                        ZWAVE Aeon stick
                        Coming soon Arduino mysensors GW

                        1 Reply Last reply
                        0
                        • AWIA AWI

                          @Chakkie In a few words.. The ATMEGA has two external interrupts 0 and 1 which are connected to pins 2 and 3. ("-2" translates the pin number to the interrupt number, which is rather confusing as @mfalkvidd meant)

                          ChakkieC Offline
                          ChakkieC Offline
                          Chakkie
                          wrote on last edited by
                          #15

                          @AWI Thanks this clear thinks up but at the same time also confusing too.

                          So "-2" activates both 0 and 1 on pin 2 or pin 3 as you mentioned earlier. I always though it represents a pin number.

                          Raspberry Pi 2
                          Domoticz
                          RFXCOM
                          ZWAVE Aeon stick
                          Coming soon Arduino mysensors GW

                          AWIA 1 Reply Last reply
                          0
                          • ChakkieC Chakkie

                            @AWI Thanks this clear thinks up but at the same time also confusing too.

                            So "-2" activates both 0 and 1 on pin 2 or pin 3 as you mentioned earlier. I always though it represents a pin number.

                            AWIA Offline
                            AWIA Offline
                            AWI
                            Hero Member
                            wrote on last edited by
                            #16

                            @Chakkie The function digitalPinToInterrupt(pin) translates the pin number to the attached interrupt like "-2" does but in a reliable and more understandable way.

                            ChakkieC 1 Reply Last reply
                            0
                            • AWIA AWI

                              @Chakkie The function digitalPinToInterrupt(pin) translates the pin number to the attached interrupt like "-2" does but in a reliable and more understandable way.

                              ChakkieC Offline
                              ChakkieC Offline
                              Chakkie
                              wrote on last edited by
                              #17

                              @AWI thanks. this looks like more promising.

                              Raspberry Pi 2
                              Domoticz
                              RFXCOM
                              ZWAVE Aeon stick
                              Coming soon Arduino mysensors GW

                              1 Reply Last reply
                              0
                              • mfalkviddM Offline
                                mfalkviddM Offline
                                mfalkvidd
                                Mod
                                wrote on last edited by
                                #18

                                Pull request done for the development version: https://github.com/mysensors/Arduino/pull/441

                                1 Reply Last reply
                                3
                                • siodS Offline
                                  siodS Offline
                                  siod
                                  wrote on last edited by
                                  #19

                                  I would like to build the same but I am still very confused now. Could you upload some pictures so I can make myself an image of how you wirde things together?

                                  Also I don´t really understand what happens here: gw.sleep(BUTTON_PIN_Deur - 2, CHANGE, BUTTON_PIN_Post - 2, CHANGE, 86400000);

                                  still learning...

                                  AWIA 1 Reply Last reply
                                  0
                                  • siodS siod

                                    I would like to build the same but I am still very confused now. Could you upload some pictures so I can make myself an image of how you wirde things together?

                                    Also I don´t really understand what happens here: gw.sleep(BUTTON_PIN_Deur - 2, CHANGE, BUTTON_PIN_Post - 2, CHANGE, 86400000);

                                    AWIA Offline
                                    AWIA Offline
                                    AWI
                                    Hero Member
                                    wrote on last edited by AWI
                                    #20

                                    @siod If you are using a "standard" Atmega328 (pro-mini, etc.) don't make it hard for yourself. This processor has two external interrupts (0 & 1) connected to pins 2 & 3. Taking your line of code gw.sleep(BUTTON_PIN_Deur - 2, CHANGE, BUTTON_PIN_Post - 2, CHANGE, 86400000); the arguments are respectively:

                                    1. Use interrupt "0" (connected to pin 2 is BUTTON_PIN_Deur)
                                    2. Have is fired on change (i.e Rising and Falling)
                                    3. Use interrupt "1" (connected to pin 3 is BUTTON_PIN_Post)
                                    4. Have is fired on change (i.e Rising and Falling)
                                    5. Wake after every 86400000 ms (24 hrs) if no interrupt fired. To be safe you should use 86400000UL (the UL indicating that it is an Unsigned long type)
                                    siodS 1 Reply Last reply
                                    0
                                    • AWIA AWI

                                      @siod If you are using a "standard" Atmega328 (pro-mini, etc.) don't make it hard for yourself. This processor has two external interrupts (0 & 1) connected to pins 2 & 3. Taking your line of code gw.sleep(BUTTON_PIN_Deur - 2, CHANGE, BUTTON_PIN_Post - 2, CHANGE, 86400000); the arguments are respectively:

                                      1. Use interrupt "0" (connected to pin 2 is BUTTON_PIN_Deur)
                                      2. Have is fired on change (i.e Rising and Falling)
                                      3. Use interrupt "1" (connected to pin 3 is BUTTON_PIN_Post)
                                      4. Have is fired on change (i.e Rising and Falling)
                                      5. Wake after every 86400000 ms (24 hrs) if no interrupt fired. To be safe you should use 86400000UL (the UL indicating that it is an Unsigned long type)
                                      siodS Offline
                                      siodS Offline
                                      siod
                                      wrote on last edited by
                                      #21

                                      @AWI
                                      Ok thx for explanation, but that means he is using 2 reed switches, right (interrupt at pin2 and int pin3)?

                                      still learning...

                                      AWIA 1 Reply Last reply
                                      0
                                      • siodS siod

                                        @AWI
                                        Ok thx for explanation, but that means he is using 2 reed switches, right (interrupt at pin2 and int pin3)?

                                        AWIA Offline
                                        AWIA Offline
                                        AWI
                                        Hero Member
                                        wrote on last edited by
                                        #22

                                        @siod I guess so. But don't ask me why. One should be sufficient to detect if the door is open :smile:

                                        In addition (and for what it is worth). If you use a real low power circuit you can save around a factor 3 on battery lifetime when you get rid of the timed wakeup. The arduino sleeps a lot better when it doesn't have to look at the alarm clock every few ms :zzz:

                                        siodS ChakkieC 2 Replies Last reply
                                        0
                                        • AWIA AWI

                                          @siod I guess so. But don't ask me why. One should be sufficient to detect if the door is open :smile:

                                          In addition (and for what it is worth). If you use a real low power circuit you can save around a factor 3 on battery lifetime when you get rid of the timed wakeup. The arduino sleeps a lot better when it doesn't have to look at the alarm clock every few ms :zzz:

                                          siodS Offline
                                          siodS Offline
                                          siod
                                          wrote on last edited by
                                          #23

                                          @AWI
                                          yes, makes sense, but how should you do this wihout the wakeup ? I guess this is just the disadvantage one must accept...

                                          still learning...

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


                                          13

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.1k

                                          Posts


                                          Copyright 2025 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