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. General Discussion
  3. Help make button-sketch sleep.

Help make button-sketch sleep.

Scheduled Pinned Locked Moved General Discussion
6 Posts 4 Posters 3.2k Views 2 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.
  • Cliff KarlssonC Offline
    Cliff KarlssonC Offline
    Cliff Karlsson
    wrote on last edited by Cliff Karlsson
    #1

    I tried using the button-sketch unmodified to control a lightswitch (non-momentary) I just wanted the arduino to register when it was switched and have the controller send a command to light or turn off a lamp. All worked like it should but the battery (18650) only lasated a few days before dying. I guess that it does not sleep properly.

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * DESCRIPTION
     *
     * Simple binary switch example 
     * Connect button or door/window reed switch between 
     * digitial I/O pin 3 (BUTTON_PIN below) and GND.
     * http://www.mysensors.org/build/binary
     */
    
    
    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    
    #define CHILD_ID 3
    #define BUTTON_PIN  3  // Arduino Digital I/O pin for button/reed switch
    
    MySensor gw;
    Bounce debouncer = Bounce(); 
    int oldValue=-1;
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg(CHILD_ID,V_TRIPPED);
    
    void setup()  
    {  
      gw.begin();
    
     // Setup the button
      pinMode(BUTTON_PIN,INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN,HIGH);
      
      // After setting up the button, setup debouncer
      debouncer.attach(BUTTON_PIN);
      debouncer.interval(5);
      
      // 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, S_DOOR);  
    }
    
    
    //  Check if digital input has changed and send in new value
    void loop() 
    {
      debouncer.update();
      // Get the update value
      int value = debouncer.read();
     
      if (value != oldValue) {
         // Send in the new value
         gw.send(msg.set(value==HIGH ? 1 : 0));
         oldValue = value;
      }
    } 
    

    How do I modify this sketch to only wake up when a input is activated?

    Or am I missing to understand how to connect the arduino? I connected the battery directly to the arduino and wanted it to wake up when for example the switch connected digital input 3 to ground.

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

      I looked at the motion scetch when i did this:

      Sleep on interupt is: gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME);

      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

      1 Reply Last reply
      0
      • Cliff KarlssonC Offline
        Cliff KarlssonC Offline
        Cliff Karlsson
        wrote on last edited by
        #3

        Ok I am not very good at this I usaly just try to copy the sketches as they are.

        But if I use the motion sketch and connect a switch to GND and D3 it will wake up and report to the controller when switch has been activated. And if I connect the switch so either D3-GND or D4-GND is activated when flicking the switch it will also wake up report to controller and then sleep.

        /**
         * The MySensors Arduino library handles the wireless radio link and protocol
         * between your home built sensors/actuators and HA controller of choice.
         * The sensors forms a self healing radio network with optional repeaters. Each
         * repeater and gateway builds a routing tables in EEPROM which keeps track of the
         * network topology allowing messages to be routed to nodes.
         *
         * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
         * Copyright (C) 2013-2015 Sensnology AB
         * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
         *
         * Documentation: http://www.mysensors.org
         * Support Forum: http://forum.mysensors.org
         *
         * This program is free software; you can redistribute it and/or
         * modify it under the terms of the GNU General Public License
         * version 2 as published by the Free Software Foundation.
         *
         *******************************
         *
         * REVISION HISTORY
         * Version 1.0 - Henrik Ekblad
         * 
         * DESCRIPTION
         * Motion Sensor example using HC-SR501 
         * http://www.mysensors.org/build/motion
         *
         */
        
        #include <MySensor.h>  
        #include <SPI.h>
        
        unsigned long SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
        #define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
        #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
        #define CHILD_ID 1   // Id of the sensor child
        
        MySensor gw;
        // Initialize motion message
        MyMessage msg(CHILD_ID, V_TRIPPED);
        
        void setup()  
        {  
          gw.begin();
        
          // Send the sketch version information to the gateway and Controller
          gw.sendSketchInfo("Motion Sensor", "1.0");
        
          pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
          // Register all sensors to gw (they will be created as child devices)
          gw.present(CHILD_ID, S_MOTION);
          
        }
        
        void loop()     
        {     
          // Read digital motion value
          boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
                
          Serial.println(tripped);
          gw.send(msg.set(tripped?"1":"0"));  // Send tripped value to gw 
         
          // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
          gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME);
        }
        
        And hopefully the battery will last several months if activated a couple times a day,
        m26872M 1 Reply Last reply
        0
        • sundberg84S Offline
          sundberg84S Offline
          sundberg84
          Hardware Contributor
          wrote on last edited by
          #4

          I think only D2 and D3 can handle interups and D2 is used by the radio.

          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

          hekH 1 Reply Last reply
          0
          • sundberg84S sundberg84

            I think only D2 and D3 can handle interups and D2 is used by the radio.

            hekH Offline
            hekH Offline
            hek
            Admin
            wrote on last edited by
            #5

            @sundberg84

            I think only D2 and D3 can handle interups and D2 is used by the radio.

            No, it is currently not used by the library. We just keep it connected to D2 (in the guides) because we could eventually find a good use for it in the future.

            Also, actually all digital pins on ATMega328 can be used to wake the MCU but only D2/D3 can trigger on RISING/FALLING. The rest support wake on CHANGE.
            (Please correct me if I'm wrong here HW-guys)

            1 Reply Last reply
            1
            • Cliff KarlssonC Cliff Karlsson

              Ok I am not very good at this I usaly just try to copy the sketches as they are.

              But if I use the motion sketch and connect a switch to GND and D3 it will wake up and report to the controller when switch has been activated. And if I connect the switch so either D3-GND or D4-GND is activated when flicking the switch it will also wake up report to controller and then sleep.

              /**
               * The MySensors Arduino library handles the wireless radio link and protocol
               * between your home built sensors/actuators and HA controller of choice.
               * The sensors forms a self healing radio network with optional repeaters. Each
               * repeater and gateway builds a routing tables in EEPROM which keeps track of the
               * network topology allowing messages to be routed to nodes.
               *
               * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
               * Copyright (C) 2013-2015 Sensnology AB
               * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
               *
               * Documentation: http://www.mysensors.org
               * Support Forum: http://forum.mysensors.org
               *
               * This program is free software; you can redistribute it and/or
               * modify it under the terms of the GNU General Public License
               * version 2 as published by the Free Software Foundation.
               *
               *******************************
               *
               * REVISION HISTORY
               * Version 1.0 - Henrik Ekblad
               * 
               * DESCRIPTION
               * Motion Sensor example using HC-SR501 
               * http://www.mysensors.org/build/motion
               *
               */
              
              #include <MySensor.h>  
              #include <SPI.h>
              
              unsigned long SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
              #define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
              #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
              #define CHILD_ID 1   // Id of the sensor child
              
              MySensor gw;
              // Initialize motion message
              MyMessage msg(CHILD_ID, V_TRIPPED);
              
              void setup()  
              {  
                gw.begin();
              
                // Send the sketch version information to the gateway and Controller
                gw.sendSketchInfo("Motion Sensor", "1.0");
              
                pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
                // Register all sensors to gw (they will be created as child devices)
                gw.present(CHILD_ID, S_MOTION);
                
              }
              
              void loop()     
              {     
                // Read digital motion value
                boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
                      
                Serial.println(tripped);
                gw.send(msg.set(tripped?"1":"0"));  // Send tripped value to gw 
               
                // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
                gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME);
              }
              
              And hopefully the battery will last several months if activated a couple times a day,
              m26872M Offline
              m26872M Offline
              m26872
              Hardware Contributor
              wrote on last edited by
              #6

              @Cliff-Karlsson
              There's a good example in the MySensors lib with sleep and two switches. https://github.com/mysensors/Arduino/blob/master/libraries/MySensors/examples/BinarySwitchSleepSensor/BinarySwitchSleepSensor.ino

              My adaption of that one to my own need and with one swtich looked like this

              #include <MySensor.h>
              #include <SPI.h>
              
              #define SKETCH_NAME "BinSwSleepBat"
              #define SKETCH_VERSION "1-2"
              #define CHILD_ID 0
              
              #define BUTTON_PIN 3 // Arduino Digital I/O pin for button/reed switch. On Atmega328p (nano and pro mini) D2 and/or D3 are interrupt pins.
              
              MySensor mys;
              
              MyMessage msg(CHILD_ID, V_TRIPPED); // Change to V_LIGHT if you use S_LIGHT in presentation below
              
              int sentValue=2;
              
              void setup()  
              {    
                mys.begin();  
                
                pinMode(BUTTON_PIN, INPUT);   // Setup the buttons
                digitalWrite(BUTTON_PIN, HIGH);   // Activate internal pull-ups
                
                mys.sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
                mys.present(CHILD_ID, S_DOOR);
              }
              
              void loop()  // Loop will iterate on changes on the BUTTON_PINs
              {
              
                mys.sleep(50);  // Short delay to allow buttons to properly settle 
                
                uint8_t val = digitalRead(BUTTON_PIN);
                if (val != sentValue) {  // If value has changed from last transmission, send the updated value
              	 mys.send(msg.set(val==HIGH ? 1 : 0));
              	 sentValue = val;
                }
                 
                mys.sleep(BUTTON_PIN-2, CHANGE, 0); // Sleep until something happens with the sensor
              }
              
              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              20

              Online

              11.7k

              Users

              11.2k

              Topics

              113.2k

              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