only wake on interupt..



  • Been a while

    getting back into the swing of things finally after a few year hiatus ive started building up my nodes again..

    so something simple to get me back in the swing..

    Im building a sensor that is literally just a momentary button
    should only wake on interrupt....
    but i cant for the life of me work out/remember how to get it working in practice.
    sensor will be built on easy newbie PCB

    /**
     * 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
     */
    
    
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Enable and select radio type attached
    #define MY_RADIO_RF24
    //#define MY_RADIO_RFM69
    
    #include <MySensors.h>
    #include <Bounce2.h>
    
    
    #define CHILD_ID 3
    #define BUTTON_PIN  3  // Arduino Digital I/O pin for button/reed switch
    unsigned long SLEEP_TIME = 120000; 
    Bounce debouncer = Bounce(); 
    int oldValue=-1;
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg(CHILD_ID,V_LIGHT);
    
    
    //=========================
    // BATTERY VOLTAGE DIVIDER SETUP
    // 1M, 470K divider across battery and using internal ADC ref of 1.1V
    // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
    // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
    // 3.44/1023 = Volts per bit = 0.003363075
    #define VBAT_PER_BITS 0.003363075  
    #define VMIN 1.9                                  //  Vmin (radio Min Volt)=1.9V (564v)
    #define VMAX 3.0                                  //  Vmax = (2xAA bat)=3.0V (892v)
    int batteryPcnt = 0;                              // Calc value for battery %
    int batLoop = 0;                                  // Loop to help calc average
    int batArray[3];                                  // Array to store value for average calc.
    int BATTERY_SENSE_PIN = A0;                       // select the input pin for the battery sense point
    //=========================
    
    
    void setup()  
    {  
      // 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);
      analogReference(INTERNAL);             // For battery sensing
    
      delay(500); // Allow time for radio if power used as reset
      
    
    }
    
    void presentation() {
      // 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.
      present(CHILD_ID, S_LIGHT);  
    }
    
    
    //  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
         send(msg.set(value==HIGH ? 1 : 0));
         oldValue = value;
      }
      batM();
      sleep(SLEEP_TIME); //sleep a bit
    }
    void batM() //The battery calculations
    {
       delay(500);
       // Battery monitoring reading
       int sensorValue = analogRead(BATTERY_SENSE_PIN);    
       delay(500);
       
       // Calculate the battery in %
       float Vbat  = sensorValue * VBAT_PER_BITS;
       int batteryPcnt = static_cast<int>(((Vbat-VMIN)/(VMAX-VMIN))*100.);
       Serial.print("Battery percent: "); Serial.print(batteryPcnt); Serial.println(" %");  
       
       // Add it to array so we get an average of 3 (3x20min)
       batArray[batLoop] = batteryPcnt;
      
       if (batLoop > 2) {  
         batteryPcnt = (batArray[0] + batArray[1] + batArray[2] + batArray[3]);
         batteryPcnt = batteryPcnt / 3;
     
       if (batteryPcnt > 100) {
         batteryPcnt=100;
     }
     
         Serial.print("Battery Average (Send): "); Serial.print(batteryPcnt); Serial.println(" %");
           sendBatteryLevel(batteryPcnt);
           batLoop = 0;
          }
         else 
         {
         batLoop++;
         }
    }
    

    Long Term goal is to create using a 3D printers a cover for my existing light switches to put over the top as a toggle so if lights are on turn them off if they are off turn them on, but to have it in a case that is easy to remove incase i have issues, and can get back to the untouched orgiginal light switches underneath



  • Here's some code for a node that never (or not yet anyway) got built. It should be enough to get you going......

    // Enable debug prints
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_RF24
    #define MY_RF24_PA_LEVEL   RF24_PA_HIGH
    #define MY_RF24_CHANNEL (97)
    #define MY_NODE_ID 91
    #define MY_PARENT_NODE_ID 0
    #define MY_PARENT_NODE_IS_STATIC
    #define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    #define CHILD_ID 1   // Id of the sensor child
    
    // Define a lower baud rate for Arduino's running on 8 MHz (Arduino Pro Mini 3.3V & SenseBender)
    #if F_CPU == 8000000L
    #define MY_BAUD_RATE 38400
    #endif
    
    #include <MySensors.h>
    
    unsigned long SLEEP_TIME = 298000; // Sleep time between reports (in milliseconds)
    
    
    // Initialize motion message
    MyMessage msg(CHILD_ID, V_TRIPPED);
    
    void setup()
    {
      	pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
    }
    
    void presentation()
    {
    	sendSketchInfo("Dining_Window", "3.0");
      wait(100);
    	present(CHILD_ID, S_DOOR, "DiningWindow", true);
    }
    
    void loop()
    {
    	// Read digital motion value
       bool tripped = digitalRead(DIGITAL_INPUT_SENSOR);
       //Software debounce.....
       //wait(25);
       //bool tripped1 = digitalRead(DIGITAL_INPUT_SENSOR);
       //if (tripped == tripped1){}
       //send(msg.set(tripped?"1":"0"),true);  // Send tripped value to gw
       
       if (tripped == HIGH){
       send(msg.set(tripped = 0),true);
       }
       else if (tripped == LOW){
       send(msg.set(tripped = 1),true);
       }
      
    	// Sleep until interrupt on motion sensor. Send update every ten minutes.
    	sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), CHANGE, SLEEP_TIME);
    }
    


  • ok thanks for that

    so i have modifed as the following....

    by the look of the alteration,

    i can just put a momentary switch in place between vcc and D3 with a resistor in line and be done with it?

    
    // Enable debug prints
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_RF24
    
    
    #define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    #define CHILD_ID 1   // Id of the sensor child
    
    #include <MySensors.h>
    
    unsigned long SLEEP_TIME = 298000; // Sleep time between reports (in milliseconds)
    //=========================
    // BATTERY VOLTAGE DIVIDER SETUP
    // 1M, 470K divider across battery and using internal ADC ref of 1.1V
    // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
    // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
    // 3.44/1023 = Volts per bit = 0.003363075
    #define VBAT_PER_BITS 0.003363075  
    #define VMIN 1.9                                  //  Vmin (radio Min Volt)=1.9V (564v)
    #define VMAX 3.0                                  //  Vmax = (2xAA bat)=3.0V (892v)
    int batteryPcnt = 0;                              // Calc value for battery %
    int batLoop = 0;                                  // Loop to help calc average
    int batArray[3];                                  // Array to store value for average calc.
    int BATTERY_SENSE_PIN = A0;                       // select the input pin for the battery sense point
    //=========================
    
    // Initialize motion message
    MyMessage msg(CHILD_ID, V_LIGHT);
    
    void setup()
    {
      	pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
    	analogReference(INTERNAL);
    }
    
    void presentation()
    {
    	sendSketchInfo("Lounge multi button", "1.0");
      wait(100);
    	present(CHILD_ID, S_LIGHT, "Lounge Light Switch", true);
    }
    
    void loop()
    {
    	// Read digital motion value
       bool tripped = digitalRead(DIGITAL_INPUT_SENSOR);
       //Software debounce.....
       //wait(25);
       //bool tripped1 = digitalRead(DIGITAL_INPUT_SENSOR);
       //if (tripped == tripped1){}
       //send(msg.set(tripped?"1":"0"),true);  // Send tripped value to gw
       
       if (tripped == HIGH){
       send(msg.set(tripped = 0),true);
       }
       else if (tripped == LOW){
       send(msg.set(tripped = 1),true);
       }
      batM();
    	// Sleep until interrupt on motion sensor. Send update every ten minutes.
    	sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), CHANGE, SLEEP_TIME);
    }
    
    void batM() //The battery calculations
    {
       delay(500);
       // Battery monitoring reading
       int sensorValue = analogRead(BATTERY_SENSE_PIN);    
       delay(500);
       
       // Calculate the battery in %
       float Vbat  = sensorValue * VBAT_PER_BITS;
       int batteryPcnt = static_cast<int>(((Vbat-VMIN)/(VMAX-VMIN))*100.);
       Serial.print("Battery percent: "); Serial.print(batteryPcnt); Serial.println(" %");  
       
       // Add it to array so we get an average of 3 (3x20min)
       batArray[batLoop] = batteryPcnt;
      
       if (batLoop > 2) {  
         batteryPcnt = (batArray[0] + batArray[1] + batArray[2] + batArray[3]);
         batteryPcnt = batteryPcnt / 3;
     
       if (batteryPcnt > 100) {
         batteryPcnt=100;
     }
     
         Serial.print("Battery Average (Send): "); Serial.print(batteryPcnt); Serial.println(" %");
           sendBatteryLevel(batteryPcnt);
           batLoop = 0;
          }
         else 
         {
         batLoop++;
         }
    }
    
    


  • Try it and see! 😉

    You might not need the sleeptime either as that was used as a heartbeat to make sure the node was still up and running. It also helps with graphing the output to see where motion was not detected for a while.



  • ok so yeah it does work....but i think its too sensitive

    i used d2 in the end for the board im using

    but the node seems to trigger every time i even wave my hand near it....

    Literally, node is currently setup with two wires you have to touch together to make the switch. when i wave my hand over it it activates
    im guesing i need to put a resister in place to decrease the sensitivity? currently its just a wire between 3.3 on a pro mini and the d2 wire

    log parse is below

    133795 MCO:SLP:WUP=0
    133797 TSF:TRI:TSB
    133804 TSF:MSG:READ,0-0-4,s=1,c=1,t=2,pt=1,l=1,sg=0:1
    133810 TSF:MSG:ACK
    133816 TSF:MSG:SEND,4-4-0-0,s=1,c=1,t=2,pt=1,l=1,sg=0,ft=0,st=OK:0
     
     __  __       ____
    |  \/  |_   _/ ___|  ___ _ __  ___  ___  _ __ ___
    | |\/| | | | \___ \ / _ \ `_ \/ __|/ _ \| `__/ __|
    | |  | | |_| |___| |  __/ | | \__ \  _  | |  \__ \
    |_|  |_|\__, |____/ \___|_| |_|___/\___/|_|  |___/
            |___/                      2.3.1
    
    16 MCO:BGN:INIT NODE,CP=RNNNA---,REL=255,VER=2.3.1
    28 TSM:INIT
    28 TSF:WUR:MS=0
    34 TSM:INIT:TSP OK
    36 TSF:SID:OK,ID=4
    38 TSM:FPAR
    75 TSF:MSG:SEND,4-4-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    1064 TSF:MSG:READ,0-0-4,s=255,c=3,t=8,pt=1,l=1,sg=0:0
    1071 TSF:MSG:FPAR OK,ID=0,D=1
    1120 TSF:MSG:READ,1-1-4,s=255,c=3,t=8,pt=1,l=1,sg=0:1
    2084 TSM:FPAR:OK
    2084 TSM:ID
    2086 TSM:ID:OK
    2088 TSM:UPL
    2093 TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1
    2103 TSF:MSG:READ,0-0-4,s=255,c=3,t=25,pt=1,l=1,sg=0:1
    2109 TSF:MSG:PONG RECV,HP=1
    2113 TSM:UPL:OK
    2115 TSM:READY:ID=4,PAR=0,DIS=1
    2129 TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=OK:0100
    2138 TSF:MSG:READ,0-0-4,s=255,c=3,t=15,pt=6,l=2,sg=0:0100
    2164 TSF:MSG:SEND,4-4-0-0,s=255,c=0,t=17,pt=0,l=5,sg=0,ft=0,st=OK:2.3.1
    2174 TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=OK:0
    2189 TSF:MSG:READ,0-0-4,s=255,c=3,t=6,pt=0,l=1,sg=0:M
    2238 !TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=11,pt=0,l=19,sg=0,ft=0,st=NACK:Lounge multi button
    2285 !TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=12,pt=0,l=3,sg=0,ft=1,st=NACK:1.1
    2439 !TSF:MSG:SEND,4-4-0-0,s=1,c=0,t=3,pt=0,l=19,sg=0,ft=2,st=NACK:Lounge Light Switch
    2447 MCO:REG:REQ
    2469 TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=3,st=OK:2
    2478 TSF:MSG:READ,0-0-4,s=255,c=3,t=27,pt=1,l=1,sg=0:1
    2484 MCO:PIM:NODE REG=1
    2486 MCO:BGN:STP
    2488 MCO:BGN:INIT OK,TSP=1
    2496 TSF:MSG:SEND,4-4-0-0,s=1,c=1,t=2,pt=1,l=1,sg=0,ft=0,st=OK:1
    Battery percent: 100 %
    3504 MCO:SLP:MS=298000,SMS=0,I1=0,M1=1,I2=255,M2=255
    3510 TSF:TDI:TSL
    3512 MCO:SLP:WUP=0
    3514 TSF:TRI:TSB
    3520 TSF:MSG:READ,0-0-4,s=1,c=1,t=2,pt=1,l=1,sg=0:1
    3526 TSF:MSG:ACK
    3563 !TSF:MSG:SEND,4-4-0-0,s=1,c=1,t=2,pt=1,l=1,sg=0,ft=0,st=NACK:0
    Battery percent: 121 %
    4573 MCO:SLP:MS=298000,SMS=0,I1=0,M1=1,I2=255,M2=255
    4579 TSF:TDI:TSL
    4581 MCO:SLP:WUP=0
    4583 TSF:TRI:TSB
    4612 TSF:MSG:SEND,4-4-0-0,s=1,c=1,t=2,pt=1,l=1,sg=0,ft=1,st=OK:1
    Battery percent: 121 %
    5621 MCO:SLP:MS=298000,SMS=0,I1=0,M1=1,I2=255,M2=255
    5627 TSF:TDI:TSL
    5629 MCO:SLP:WUP=0
    5632 TSF:TRI:TSB
    5638 TSF:MSG:READ,0-0-4,s=1,c=1,t=2,pt=1,l=1,sg=0:1
    5644 TSF:MSG:ACK
    5662 TSF:MSG:SEND,4-4-0-0,s=1,c=1,t=2,pt=1,l=1,sg=0,ft=0,st=OK:1
    Battery percent: 121 %
    Battery Average (Send): 100 %
    6678 TSF:MSG:SEND,4-4-0-0,s=255,c=3,t=0,pt=1,l=1,sg=0,ft=0,st=OK:100
    6684 MCO:SLP:MS=298000,SMS=0,I1=0,M1=1,I2=255,M2=255
    6690 TSF:TDI:TSL
    
    
    

  • Mod

    @markjgabb yes, without a pull-down or pull-up resistor, the pin will be floating and not have a defined state.



  • as @mfalkvidd said, you need a resistor between D2 and ground if the switch is connected between D2 and VCC(5V/3.3V). Higher value resistors will reduce current flowing but that's not a big consideration for momentary button presses.



  • Perfect. Tha is for that works great... Interesting thought though with how sensitive it is...

    I was actually just waving my hand near it. Possibly 2-3 inches away from the wires....

    Could be used as something like a plate you slap with your hand


  • Mod

    @markjgabb yes, that's exactly how touch-based buttons work (and your phone's touchscreen)



  • ok stuck now...

    it works once...
    it wakes up each time and send the command
    but in domoticz im using a push on button with a off delay.....
    domoticz is ignoring the sensor after the first on, because the sensor isnt available anymore to receive the command....

    do i possibly need the sensor to send an on command, wait a moment then send its own off command again?



  • @markjgabb Domoticz should display a binary On/Off according to what it is sent, so suggest looking at the Domoticz log for incoming messages, I suspect the OFF is not being sent. Why you are using a Domoticz switch with an off delay I don't understand, a simple on/off is what I use.
    In your modified @skywatch sketch the loop contains battery measurement batM() which itself incorporates a half second delay and Serial.print, suggest rem that out in the loop for now leaving the main loop to work with perhaps a 5 or 10ms delay if the switch is not debounced electrically.

    A momentary rather than latching switch allows a very fast change of state in the region of milliseconds, quite different to a real world device running seconds/minutes/hours in one state or the other.
    In your case you are looking at the interrupt as CHANGE, then reporting the digital state of the pin, any old toggle or light switch would do.



  • yeah i possibly havent been ecactly clear on that

    Scenario is that when i walk into a room i dont always want to ask google to turn on or off all the lights in the room.
    so i plan on having wall mounted momentary switches, which their sole purpose is to if the lights are on, turn them off and if they are off turn them on....

    so i had planned to use a momentary switch so that no one in the house became focused on if they were off or on, but just push them to get the opposite of current state

    In domoticz i was going to use a push on button with a dzvents script so that any time the push on on button became on it would toggle the opposite of whatever the state of lights in the room in...

    obviously a bit more logic is needed in case some lights are on and others off....

    As per advice i have shortened off the times for delays and have taken out the serial prints on the battery monitor

    
    // Enable debug prints
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_RF24
    
    
    #define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    #define CHILD_ID 1   // Id of the sensor child
    
    #include <MySensors.h>
    
    unsigned long SLEEP_TIME = 298000; // Sleep time between reports (in milliseconds)
    //=========================
    // BATTERY VOLTAGE DIVIDER SETUP
    // 1M, 470K divider across battery and using internal ADC ref of 1.1V
    // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
    // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
    // 3.44/1023 = Volts per bit = 0.003363075
    #define VBAT_PER_BITS 0.003363075  
    #define VMIN 1.9                                  //  Vmin (radio Min Volt)=1.9V (564v)
    #define VMAX 3.0                                  //  Vmax = (2xAA bat)=3.0V (892v)
    int batteryPcnt = 0;                              // Calc value for battery %
    int batLoop = 0;                                  // Loop to help calc average
    int batArray[3];                                  // Array to store value for average calc.
    int BATTERY_SENSE_PIN = A0;                       // select the input pin for the battery sense point
    //=========================
    
    // Initialize motion message
    MyMessage msg(CHILD_ID, V_LIGHT);
    
    void setup()
    {
        pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
      analogReference(INTERNAL);
    }
    
    void presentation()
    {
      sendSketchInfo("Lounge multi button", "1.1");
      wait(100);
      present(CHILD_ID, S_LIGHT, "Lounge Light Switch", true);
    }
    
    void loop()
    {
      // Read digital motion value
       bool tripped = digitalRead(DIGITAL_INPUT_SENSOR);
       //Software debounce.....
       //wait(25);
       //bool tripped1 = digitalRead(DIGITAL_INPUT_SENSOR);
       //if (tripped == tripped1){}
       //send(msg.set(tripped?"1":"0"),true);  // Send tripped value to gw
       
       if (tripped == HIGH){
       send(msg.set(tripped = 1),true);
       wait(100);
       send(msg.set(tripped = 0),true);
       }
      batM();
      // Sleep until interrupt on motion sensor. Send update every ten minutes.
      sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), CHANGE, SLEEP_TIME);
    }
    
    void batM() //The battery calculations
    {
       delay(5);
       // Battery monitoring reading
       int sensorValue = analogRead(BATTERY_SENSE_PIN);    
       delay(5);
       
       // Calculate the battery in %
       float Vbat  = sensorValue * VBAT_PER_BITS;
       int batteryPcnt = static_cast<int>(((Vbat-VMIN)/(VMAX-VMIN))*100.);
       //Serial.print("Battery percent: "); Serial.print(batteryPcnt); Serial.println(" %");  
       
       // Add it to array so we get an average of 3 (3x20min)
       batArray[batLoop] = batteryPcnt;
      
       if (batLoop > 2) {  
         batteryPcnt = (batArray[0] + batArray[1] + batArray[2] + batArray[3]);
         batteryPcnt = batteryPcnt / 3;
     
       if (batteryPcnt > 100) {
         batteryPcnt=100;
     }
     
         Serial.print("Battery Average (Send): "); Serial.print(batteryPcnt); Serial.println(" %");
           sendBatteryLevel(batteryPcnt);
           batLoop = 0;
          }
         else 
         {
         batLoop++;
         }
    }
    

  • Mod

    @markjgabb when the node is sleeping, it cannot receive messages from Domoticz.



  • @markjgabb Wasn't thinking about shortening the battery read but // it out in the loop meantime as it is only a local serial output. Whatever..

    Ok on the intent, then what you are doing is "illogical", you are reporting the state of the switch not the condition of the light.
    What you need to do is toggle a separate logic parameter on the Node for a LOW interrupt at the Node, switching ON/OFF the light, report that separate logic state to Domoticz then go to sleep... The interrupt only initiates the action, the Node reports and stores the current state until the next interrupt, Domoticz tells you the last reported state.
    Whether you control the light at at the button switch Node with a latching relay or command a separate Node is up to you, but Domoticz only reports the last known state...



  • @zboblamont
    haha ok so i went right back to the begining of what you said and got it working now (code below)

    cut back all unneccicary steps and really locked down what was going on... i can always add back battery part later

    however now when it comes back online after 5 minutes for heart beat it keep sending a switch command to domoticz...

    im guessing this is due to the issue where the state is being reported differently after it going to sleep as soon as action is performed....

    // Enable debug prints
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_RF24
    
    #define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    #define CHILD_ID 1   // Id of the sensor child
    
    
    #include <MySensors.h>
    
    unsigned long SLEEP_TIME = 298000; // Sleep time between reports (in milliseconds)
    
    
    // Initialize motion message
    MyMessage msg(CHILD_ID, V_TRIPPED);
    
    void setup()
    {
        pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
    }
    
    void presentation()
    {
      sendSketchInfo("Lounge Multi", "3.0");
      wait(100);
      present(CHILD_ID, S_DOOR);
    }
    
    void loop()
    {
      // Read digital motion value
       bool tripped = digitalRead(DIGITAL_INPUT_SENSOR);
       //Software debounce.....
       //wait(25);
       //bool tripped1 = digitalRead(DIGITAL_INPUT_SENSOR);
       //if (tripped == tripped1){}
       //send(msg.set(tripped?"1":"0"),true);  // Send tripped value to gw
       
       if (tripped == HIGH){
       send(msg.set(tripped = 1),true);
       }
       else if (tripped == LOW){
       send(msg.set(tripped = 0),true);
       }
      
      // Sleep until interrupt on motion sensor. Send update every ten minutes.
      sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), CHANGE, SLEEP_TIME);
    }
    
    


  • @markjgabb I used the trigger data for a 'heartbeat' so that it looked 'normal' on a graph and the graph updated regularly (otherwise it only updated on a change and that can be a long time indeed for some windows)!

    The solution might be to immplement the MySensors Heartbeat instead.

    Just put " sendHeartbeat(); " without the quotes in the sketch main loop. You may still get messages about status, I am not sure as I use MyController for my set-up and never used domoticz
    .
    If so you can try and set a memory for the status and only send the tripped message if the status of the last_tripped_status (or whatever you want to call it) has changed.

    I believe that the relay sketches in 'build' page use a similar approach from memory.

    Here is a strong hint of how to do it..... 🙂

     if (tripped == HIGH && last_tripped == LOW){
       send(msg.set(tripped = 1),true);
    last_tripped = HIGH;
       }
       else if (tripped == LOW) && last_tripped==HIGH{
       send(msg.set(tripped = 0),true);
    last_tripped = LOW;
       }
    

    You need to assign a type bool to last_tripped somewhere and initialise it.



  • @markjgabb "..cut back all unneccicary steps and really locked down what was going on.." was indeed the idea.

    What I went on to explain is than you are reporting the state of D3 which goes HIGH/LOW/HIGH via the button, not the light. The critical difference between @skywatch and your install is movement detection maintains a value, your button does not.

    If you create a variable before the loop, say boolean lightswitch=false; (presuming that the light is ON when you program it) this variable will be changed and reported to Domoticz in the loop.
    Top of the loop you toggle the "lightswitch" value, first time it will switch false to true, report in the "lightswitch" value then go to sleep.
    Bottom of the loop your " sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), CHANGE, SLEEP_TIME); should trigger on a LOW, as this is the momentary state of D3 ONLY when the button is pressed.
    Your next button press activates the loop, toggles "lightswitch", reports and goes back to sleep, at least two years on a pair of AA cells.

    Were it I doing this there would be a mains powered indicator on the switch plate which cuts out when the light is on, A- To find the switch in the dark, B-To provide an indicator which can be used to verify the actual state of the light to ensure it stays in sync (Murphy's Law).
    I don't see the point of using heartbeat as it is not a security device, and only fire the battery reading mosfet every X loops.


 

258
Online

9.0k
Users

9.7k
Topics

101.3k
Posts