Window / motion sensor: noob help on sketc



  • Hello,
    I've spent many months reading this great forum and i'm ready to build my first project.
    I'm using VeraLite as controller with a serial gateway (USB) and i would like to build a window/motion sensor battery operated (2AA).

    For the sensors I collected all the stuff:

    • list itemArduino Pro mini

    • list item2AA battery holder

    • list itemDC-DC Step up regulator

    • list itemRadio NRF24L01+ (directly linked to the step-up for power)

    • list itemMotion HC SR501 (to PIN2 not used by radio)

    • list itemReed switch

    I hacked the Nano by broking the LED (since i wasn't able to desolder it)

    Now i have some problem with the sketch, since i tried to fuse 2 different sketches (door/window and motion)

    Can someone help me?

    #include <MySensor.h>
    #include <SPI.h>
    
    unsigned long SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
    #define SKETCH_NAME "Binary / Motion Sensor"
    #define SKETCH_MAJOR_VER "2"
    #define SKETCH_MINOR_VER "0"
    
    #define PRIMARY_CHILD_ID 4
    
    #define PRIMARY_BUTTON_PIN 2   // Arduino Digital I/O pin for button/reed switch
    #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)
    
     
    // initialize motion sensor
    MySensor gw;
    // Initialize motion message
    MyMessage msg(CHILD_ID, V_TRIPPED);
    
    //initialize window sensor
    MySensor sensor_node;
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg(PRIMARY_CHILD_ID, V_TRIPPED);
    
    
    void setup()  
    {  
      sensor_node.begin();
    
      // Setup the Window
      pinMode(PRIMARY_BUTTON_PIN, INPUT);
    
      // Activate internal pull-ups
      digitalWrite(PRIMARY_BUTTON_PIN, HIGH);
      
      // Send the sketch version information to the gateway and Controller
      sensor_node.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.
      sensor_node.present(PRIMARY_CHILD_ID, S_DOOR);  
      }
    
    {  
      gw.begin();
      
      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);
      
    }
    // 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(PRIMARY_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;
      }
    // 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 something happens with the window sensor
      sensor_node.sleep(PRIMARY_BUTTON_PIN-2, CHANGE, 0);
    
      // Sleep until motion interrupt comes in on motion sensor. Send update every two minute. 
      gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME);
    } 
    
    

  • Hardware Contributor

    What is the problem, any error codes or serial output?


  • Hero Member

    @tenaciousmetal I think something went wrong in combining the two sketches. You are instantiating (defining) two nodes in one sketch. What you should do is have one node, "gw" for example and define two children/sensors. I tried to make some changes in your sketch that should lead you in the right direction. (marked with "AWI", not tested there can be a few syntax errors)

    #include <MySensor.h>
    #include <SPI.h>
    
    unsigned long SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
    #define SKETCH_NAME "Binary / Motion Sensor"
    #define SKETCH_MAJOR_VER "2"
    #define SKETCH_MINOR_VER "0"
    
    #define PRIMARY_CHILD_ID 4
    
    #define PRIMARY_BUTTON_PIN 2   // Arduino Digital I/O pin for button/reed switch
    #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)
    
    
    // initialize motion sensor
    MySensor gw;
    // Initialize motion message
    MyMessage msg(CHILD_ID, V_TRIPPED);
    
    //initialize window sensor (AWI: don't define the node twice)
    //MySensor sensor_node;
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg(PRIMARY_CHILD_ID, V_TRIPPED);
    
    
    void setup()  
    {  
     //sensor_node.begin(); (AWI: define the "gw" node here)
     gw.begin() ;
     // Setup the Window & Activate internal pull-ups
     pinMode(PRIMARY_BUTTON_PIN, INPUT_PULLUP);
     
     // Activate internal pull-ups (AWI: combined in previous definition)
    // digitalWrite(PRIMARY_BUTTON_PIN, HIGH);
     
     // Send the sketch version information to the gateway and Controller
     gw.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.
     gw.present(PRIMARY_CHILD_ID, S_DOOR);  
      
     // gw.begin();  (AWI: no need for another begin)
     
     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);
     
    }
    // 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
     gw.sleep(5);
     
     value = digitalRead(PRIMARY_BUTTON_PIN);
     
     if (value != sentValue) {
        // Value has changed from last transmission, send the updated value
        gw.send(msg.set(value==HIGH ? 1 : 0));
        sentValue = value;
       }
    
    // 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 something happens with the window sensor (AWI: combine with other sleep)
     //sensor_node.sleep(PRIMARY_BUTTON_PIN-2, CHANGE, 0);
    
     // Sleep until motion interrupt comes in on motion sensor. Send update every two minute. 
     gw.sleep(INTERRUPT, CHANGE, PRIMARY_BUTTON_PIN-2, CHANGE, SLEEP_TIME);
    } 
    

Log in to reply
 

Suggested Topics

  • 87
  • 1
  • 7
  • 5
  • 8
  • 2

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts