Help with sleeping door sensor



  • Hi friend i need help with my code of sleeping door sensor.

    i need add sending actual status of digital input and battery level and send this info each 7 seconds to GW.

    #define MY_DEBUG
    #define MY_RADIO_NRF24
    uint8_t NodeID = 88;
    #define MY_NODE_ID NodeID
    #define MY_PARENT_NODE_ID 0
    uint8_t RF24_Channel = 30;
    #define MY_RF24_CHANNEL RF24_Channel
    
    #define LED_PIN 17
    #define PRIMARY_BUTTON_PIN 3   // Arduino Digital I/O pin for button/reed switch
    #define SECONDARY_BUTTON_PIN 2 // Arduino Digital I/O pin for button/reed switch
    
    unsigned long SLEEP_TIME = 7000; // Sleep time between tX
    
    #include <MySensors.h>
    
    #define SKETCH_NAME "Door RF Sensor"
    #define SKETCH_MAJOR_VER "1"
    #define SKETCH_MINOR_VER "1"
    #define PRIMARY_CHILD_ID 1
    #define SECONDARY_CHILD_ID 2
    
    
    MyMessage msg(PRIMARY_CHILD_ID, V_TRIPPED);
    MyMessage msg2(SECONDARY_CHILD_ID, V_TRIPPED);
    
    
    void before () {
      // Setup the buttons
      pinMode(PRIMARY_BUTTON_PIN, INPUT);
      pinMode(SECONDARY_BUTTON_PIN, INPUT);
    
      // Activate internal pull-ups
      digitalWrite(PRIMARY_BUTTON_PIN, HIGH);
      digitalWrite(SECONDARY_BUTTON_PIN, HIGH);
      pinMode(LED_PIN, OUTPUT);
      
      
     }
    
    void setup()
    {
    }
    
    void presentation()
    {
    	sendSketchInfo(SKETCH_NAME, SKETCH_MAJOR_VER "." SKETCH_MINOR_VER);
    	present(PRIMARY_CHILD_ID, S_DOOR);
    	present(SECONDARY_CHILD_ID, S_DOOR);
    }
    
    void loop()
    {
    	uint8_t value;
    	static uint8_t sentValue=2;
    	static uint8_t sentValue2=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
    		digitalWrite(LED_PIN, HIGH);
        delay (5);
    		send(msg.set(value==HIGH));
        digitalWrite(LED_PIN, LOW);
    		sentValue = value;
    	}
    
    	value = digitalRead(SECONDARY_BUTTON_PIN);
    
    	if (value != sentValue2) {
    		// Value has changed from last transmission, send the updated value
    		digitalWrite(LED_PIN, HIGH);
        delay (5);
    		send(msg2.set(value==HIGH));
         digitalWrite(LED_PIN, LOW);
    		sentValue2 = value;
    	}
    
     
    
    
    	// Sleep until something happens with the sensor
    	sleep(PRIMARY_BUTTON_PIN-2, CHANGE,SECONDARY_BUTTON_PIN-2, CHANGE, 7000);
    

    how make it...???

    also interrupt must working correctly.

    please help me with my code.

    tnx
    Peter


  • Mod

    Are you having problems with the code?
    Is there a reason why you don't use

    #define MY_NODE_ID 88
    

    ?



  • In my specific application i use one GW as receiver and one node with Static NODE ID as transmitter.


  • Mod

    that didn't answer much my questions, as that is the common setup for Mysensors network. 🙂


  • Hero Member

    @ustredna , try changing this line:

    send(msg.set(value==HIGH));

    by

    send(msg.set(value));

    The same for msg2 line:

    send(msg2.set(value==HIGH));

    to

    send(msg2.set(value));

    And if you only need to update the GW on every 7 seconds (and not immediately), change the sleep:

    sleep( 7000 ) ;

    Hope it helps!


Log in to reply
 

Suggested Topics

  • 1
  • 3
  • 5
  • 3
  • 6
  • 2

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts