Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Hardware
  3. Window / motion sensor: noob help on sketc

Window / motion sensor: noob help on sketc

Scheduled Pinned Locked Moved Hardware
3 Posts 3 Posters 1.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • tenaciousmetalT Offline
    tenaciousmetalT Offline
    tenaciousmetal
    wrote on last edited by tenaciousmetal
    #1

    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);
    } 
    
    
    AWIA 1 Reply Last reply
    0
    • sundberg84S Offline
      sundberg84S Offline
      sundberg84
      Hardware Contributor
      wrote on last edited by
      #2

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

      Controller: Proxmox VM - Home Assistant
      MySensors GW: Arduino Uno - W5100 Ethernet, Gw Shield Nrf24l01+ 2,4Ghz
      MySensors GW: Arduino Uno - Gw Shield RFM69, 433mhz
      RFLink GW - Arduino Mega + RFLink Shield, 433mhz

      1 Reply Last reply
      0
      • tenaciousmetalT tenaciousmetal

        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);
        } 
        
        
        AWIA Offline
        AWIA Offline
        AWI
        Hero Member
        wrote on last edited by
        #3

        @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);
        } 
        
        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        18

        Online

        11.7k

        Users

        11.2k

        Topics

        113.1k

        Posts


        Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • MySensors
        • OpenHardware.io
        • Categories
        • Recent
        • Tags
        • Popular