Battery door sensor doesn't wake for the 1st time after approx 12 hours of sleep...



  • Hello guys,

    I've built a battery sensor to use on an external gate.

    I'm using the same hardware setup as here.

    http://iot-playground.com/blog/2-uncategorised/10-low-power-door-window-sensor

    I've soldered the 1M resistor from pin 3 to VCC under the Pro Mini and also have radio IRQ going to Pin 2.

    The issue I have is that if the gates used frequently every few hours it's perfect. If however it's not used for a longer period of time (for example, overnight) it doesn't seem to wake on the very first opening - all subsequent opening or closings work fine!

    Any ideas, I'm thinking it's related to my code, resistors, pull-ups?

    Code

    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    #define MY_NODE_ID 4
    #define MY_PARENT_NODE_ID AUTO // 1 / AUTO
    // #define MY_BAUD_RATE 9600 // For us with 1Mhz modules
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #include <SPI.h>
    #include <MySensors.h>
    #include <Vcc.h>
    
    #define SKETCH_NAME "Door Sensor"
    #define SKETCH_MAJOR_VER "1"
    #define SKETCH_MINOR_VER "0"
    
    #define CHILD_ID1 1
    #define CHILD_ID2 2
    #define CHILD_ID3 3
    
    #define PRIMARY_BUTTON_PIN 3   // Arduino Digital I/O pin for button / reed switch
    
    MyMessage msg(CHILD_ID1, V_TRIPPED);
    MyMessage msg2(CHILD_ID2, V_CUSTOM);
    MyMessage msg3(CHILD_ID3, V_CUSTOM);
    
    const float VccMin        = 2.0 * 0.9; // Minimum expected Vcc level, in Volts. Example for 2xAA Alkaline.
    const float VccMax        = 2.0 * 1.5; // Maximum expected Vcc level, in Volts. Example for 2xAA Alkaline.
    const float VccCorrection = 2.88 / 2.82; // Measured Vcc by multimeter divided by reported Vcc
    Vcc vcc(VccCorrection);
    
    uint8_t value;
    
    void setup()
    {
      // Setup the contact sensor
      pinMode(PRIMARY_BUTTON_PIN, INPUT);
    
      // Activate internal pull-ups
      // digitalWrite(PRIMARY_BUTTON_PIN, HIGH);
    }
    
    void presentation() {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo(SKETCH_NAME, SKETCH_MAJOR_VER "." SKETCH_MINOR_VER);
    
      // 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.
      present(CHILD_ID1, S_DOOR);
      present(CHILD_ID2, S_CUSTOM);
      present(CHILD_ID3, S_CUSTOM);
    }
    
    // Loop will iterate on changes on the BUTTON_PINs
    void loop()
    {
    #ifdef MY_DEBUG
      Serial.print("Waking");
    #endif
      // uint8_t value;
      // static uint8_t sentValue = 2;
    
      // Short delay to allow buttons to properly settle
      // sleep(5);
    
      value = digitalRead(PRIMARY_BUTTON_PIN);
    
      // if (value != sentValue) {
      // Value has changed from last transmission, send the updated value
      send(msg.set(value == HIGH ? 0 : 1 ));
      // sentValue = value;
      //}
    
      float v = vcc.Read_Volts();
    #ifdef MY_DEBUG
      Serial.print("VCC = ");
      Serial.print(v);
      Serial.println(" Volts");
    #endif
    
      float p = vcc.Read_Perc(VccMin, VccMax);
    #ifdef MY_DEBUG
      Serial.print("VCC = ");
      Serial.print(p);
      Serial.println("%");
    #endif
    
      send(msg2.set(v, 2));
      send(msg3.set(p, 1));
    
      // Sleep until something happens with the sensor
    
    #ifdef MY_DEBUG
      Serial.print("Sleeping");
    #endif
      sleep(PRIMARY_BUTTON_PIN - 2, CHANGE, 0);
    }
    

  • Hardware Contributor

    Hello,

    it could be partially related to your code, you need to wait a few milliseconds at the beginning of the loop to let the contact settle at it can bounce a little bit.
    Why it would happen only after long delay I don't know, maybe a side effect with capacitance of your resistor, or mechanical effect with the "bouncing" in the reed switch when it stayed longer in the same position.
    But what I'm sure about is you need to wait a bit at the beginning of the loop to have a reliable result when you call digitalRead.



  • I've tried adding a short sleep, sadly it doesn't make any difference.

    Like I say, if the gate is opened and closed every few hours, or even every 12, it's okay. After a full night however it failed to detect the interrupt again this morning - in fact I had to open 2-3 times before it started to work.

    I'm out of ideas 😞


  • Mod

    @Mark-Swift does your area get humid during the night? Maybe it is a moisture problem?



  • That's a possibility, it's outside in a sealed box.

    I've brought it inside and will test it in the morning....



  • Well, after 16 hours, I tried the sensor this morning and it worked.

    I've a suspicion that the issue is related to either an environmental factor, or perhaps the node searching for a new parent (it's currently very close to the gateway and a repeater, so I'm not sure that's the issue).

    Is there a way for me to log the debug when its in situ?


  • Hero Member

    @Mark-Swift You could try with remote debug with V_TEXT if your controller supports this or if you send it to a logging node..

    Another option is to use different sensors to send debugging information, i.e. return values from the 'send()' and 'sleep()' functions.


Log in to reply
 

Suggested Topics

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts