Help make button-sketch sleep.



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


  • Hardware Contributor

    I looked at the motion scetch when i did this:

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



  • 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,

  • Hardware Contributor

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


  • Admin

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


  • Hardware Contributor

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

Log in to reply
 

Suggested Topics

  • 4
  • 4
  • 17
  • 9
  • 3
  • 2

55
Online

11.4k
Users

11.1k
Topics

112.6k
Posts