Is this completely wrong?



  • Hi.
    I am about to set up a battery-switch-node with Home Assistant as controller.
    It's a mini pro 3V3 with led and regulator removed.
    I am trying two different sketches and will try to get low power consumption .

    Here i use pin 3 and ground.

    #include <MySensor.h>
    #include <SPI.h>
    
    #define SN "TestSleepSensor"
    #define SV "1.0"
    
    #define CHILD_ID 3
    
    #define BUTTON_PIN 3   // Arduino Digital I/O pin for button/reed switch
    
     
    MySensor sensor_node;
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg(CHILD_ID, V_TRIPPED);
    
    void setup()  
    {  
      sensor_node.begin();
    
      // Setup the buttons
      pinMode(BUTTON_PIN, INPUT_PULLUP);
      
      // Send the sketch version information to the gateway and Controller
      sensor_node.sendSketchInfo(SN, SV);
    
      // Register binary input sensor to sensor_node (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.
      sensor_node.present(CHILD_ID, S_DOOR);  
    }
    
    // Loop will iterate on changes on the BUTTON_PINs
    void loop() 
    {
      uint8_t value;
      static uint8_t sentValue=2;
    
      // Short delay to allow buttons to properly settle
      sensor_node.sleep(5);
      
      value = digitalRead(BUTTON_PIN);
      
      if (value != sentValue) {
         // Value has changed from last transmission, send the updated value
         sensor_node.send(msg.set(value==LOW ? 1 : 0));
         sentValue = value;
      }
    
      // Sleep until something happens with the sensor
      sensor_node.sleep(BUTTON_PIN-2, CHANGE, 0);
    }
    

    This seems to work. I can't tell if it's good or not. Its my first, a example i chaged.
    It consumes about 0.09mA when the connector is closed witch it will be most of the time.
    When switch is open it consumes less.

    Here i use pin 3 and VCC

    #include <MySensor.h>
    #include <SPI.h>
    
    #define SN "TestSleepSensor"
    #define SV "1.0"
    
    #define CHILD_ID 3
    
    #define BUTTON_PIN 3   // Arduino Digital I/O pin for button/reed switch
    
     
    MySensor sensor_node;
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg(CHILD_ID, V_TRIPPED);
    
    void setup()  
    {  
      sensor_node.begin();
    
      // Setup the buttons
      pinMode(BUTTON_PIN, INPUT);
      
      // Send the sketch version information to the gateway and Controller
      sensor_node.sendSketchInfo(SN, SV);
    
      // Register binary input sensor to sensor_node (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.
      sensor_node.present(CHILD_ID, S_DOOR);  
    }
    
    // Loop will iterate on changes on the BUTTON_PINs
    void loop() 
    {
      uint8_t value;
      static uint8_t sentValue=2;
    
      // Short delay to allow buttons to properly settle
      sensor_node.sleep(5);
      
      value = digitalRead(BUTTON_PIN);
      
      if (value != sentValue) {
         // Value has changed from last transmission, send the updated value
         sensor_node.send(msg.set(value==HIGH ? 1 : 0));
         sentValue = value;
      }
    
      // Sleep until something happens with the sensor
      sensor_node.sleep(BUTTON_PIN-2, CHANGE, 0);
    }
    

    This consumes less when the switch is closed, approx 1.2uA according to my multimeter. it seems very low but anyway, it's much lower than the other sketch.
    One thing with this. When door is open it shows closed in the controller and the other way around. Can i fix it?
    Another thing. When i open my switch, it takes about 2-3 seconds before it send the changed state. Not a problem but i guess it is since i feed the pin directly from VCC?

    Is this stupid? I am New to this.


  • Hero Member

    @xydix

    Try changeing this line:

    sensor_node.send(msg.set(value==HIGH ? 1 : 0)); 
    

    To this:

    sensor_node.send(msg.set(value==HIGH ? 0 : 1)); 
    


  • Thank you. That will get it right.
    But now I get the delay when the door open.
    I guess I need a pull down resistor for this to work. 4.7k? 10k?
    I guess it's the internal pull up that consumes power?
    Another thing.
    I changed digitalWrite = HIGH (original example)
    to
    Pinmode INPUT_PULLUP
    in my first sketch

    Wich option is best if I want low power consumption?


 

334
Online

8.0k
Users

8.8k
Topics

94.4k
Posts