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. Troubleshooting
  3. Motion & Relay Sensor issue

Motion & Relay Sensor issue

Scheduled Pinned Locked Moved Troubleshooting
interruptsleep issuemultisensormotion sensorrelay
10 Posts 5 Posters 6.0k 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.
  • K Offline
    K Offline
    kunall
    wrote on last edited by kunall
    #1

    I'm attempting to merge Relay & Motion Sensor sketches, and so far i'm partially succeeded. But..

    Here is the code that works for me: Relay and Motion both work.

    /*
    // Example sketch showing how to control physical relays. 
    // This example will remember relay state even after power failure.
    
    REVISION HISTORY
    Version 1.0 - December 29, 2014 - Pete B - Adapted from MySensors Relay Code
    */
    #define NODE_ID 20
    #define SN "Motion&LED"
    #define SV "1.0"
    
    #include <MySensor.h>
    #include <SPI.h>
    
    
    #define RELAY_PIN  4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define MOTION_PIN  3
    #define RELAY_CHILD 2
    #define MOTION_CHILD 1
    #define NUMBER_OF_RELAYS 1 // Total number of attached relays
    #define RELAY_ON 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    #define INTERRUPT MOTION_PIN -2
    //unsigned long SLEEP_TIME = 120000;
    
    
    MySensor gw;
    
    MyMessage msgRLY(RELAY_CHILD,V_LIGHT);
    MyMessage msgMO(MOTION_CHILD, V_TRIPPED);
    
    
    void setup()  
    {   
      // Initialize library and add callback for incoming messages
      gw.begin(incomingMessage, NODE_ID);
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo(SN, SV);
    
    pinMode(MOTION_PIN, INPUT);
    
      // Register all sensors to gw (they will be created as child devices)
      gw.present(RELAY_CHILD, S_LIGHT);
      gw.present(MOTION_CHILD, S_MOTION);
      
      // Make sure relays are off when starting up
      digitalWrite(RELAY_PIN, RELAY_OFF);
      
      // Then set relay pins in output mode
      pinMode(RELAY_PIN, OUTPUT);   
    
    }
    
    
    void loop() 
    {
      // Alway process incoming messages whenever possible
      gw.process();
      
      // Read digital motion value
      boolean tripped = digitalRead(MOTION_PIN) == HIGH; 
            
      Serial.println(tripped);
      gw.send(msgMO.set(tripped?"1":"0"));  // Send tripped value to gw 
      
      // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
     // gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME);
      
      
    }
    
    void incomingMessage(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type==V_LIGHT) {
         // Change relay state
         digitalWrite(RELAY_PIN, message.getBool()?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
    //     gw.saveState(message.sensor, message.getBool());
    //     // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
       } 
    }
    
    

    However, My problem is that Motion Sensor continuously sends the message and does not sleep like the following log shows: I want the motion sensor to only send message when triggered.

    Screen Shot 2015-03-27 at 12.07.07 PM.png

    If I uncomment the following line in the sketch, The Motion sensor sends message only when triggered (Perfect, thats what I want) but the Relay does not operate:

    gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME);
    

    I would really appreciate any help with this issue.

    Thank You

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mikeones
      wrote on last edited by
      #2

      You should check if the value changed and only send updates when true. Something like this.

      if (lastTripped != tripped ) {
      
      1 Reply Last reply
      0
      • hekH Offline
        hekH Offline
        hek
        Admin
        wrote on last edited by
        #3

        You cannot sleep a node that expects incoming messages.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kunall
          wrote on last edited by
          #4

          Got it. Thank you for your response. So I simply removed the sleep function and put if - else statement to stop my Motion sensor flooding my console window. :)

          1 Reply Last reply
          0
          • V Offline
            V Offline
            virgus1970
            wrote on last edited by
            #5

            To kunall
            Have You solved this problem?

            1 Reply Last reply
            0
            • V Offline
              V Offline
              virgus1970
              wrote on last edited by virgus1970
              #6

              I solved this problem by adding following code:

                // Read digital motion value
                tripped = digitalRead(DIGITAL_INPUT_SENSOR); 
                  if(lastTripped != tripped) 
                  { 
                    if(lastTripped == 0)
                    {
                      Serial.print("Motion detected. Sensor value=");
                      lastTripped = 1;
                    }
                    else
                    {
                      Serial.print("Sensor value=");
                      lastTripped = 0;
                    }  
                    Serial.println(tripped);
                    // Send tripped value to gw 
                    gw.send(msg.set(tripped?"1":"0"));  
                   }
              

              But now I have anorther problem.
              I test now two boards (Node1 & Node2). Node1 is Relay & Motion. Node2 is Relay with button. Everything is OK after start of Node1. But after motion sensor is trigged Vera begin to transmit to Node1 Asking Message constantly without any action from me. Like that:
              Скриншот 2015-03-29 20.39.17.png
              It stops only after couple of times pushing the button of Node2.
              Any ideas of solution?
              P.S. It doesn't interrupt work of both relays (Node1 & Node2) & reports of Motion sensor.

              1 Reply Last reply
              0
              • V Offline
                V Offline
                virgus1970
                wrote on last edited by
                #7

                The further observation shows that "this" comes more probably from Vera (from plugin IMHO). Then I connect GW to serial monitor nothing similar happens.
                May be reading logs of Vera can help? But I don't know where I can find them...

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  kunall
                  wrote on last edited by
                  #8

                  I'm using OpenHab. So not sure about Vera. But I have the same issue. After using the code you provided, The node constantly sends messages 1 (Motion) or 0 (No motion). So I conclude, as Hek mentioned, the node can not sleep because it is waiting for incoming messages, I guess.

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    dias
                    wrote on last edited by
                    #9

                    hello, desolated for my english
                      I want a code with 2 relays and 2 pir
                    Thank you

                    hekH 1 Reply Last reply
                    0
                    • D dias

                      hello, desolated for my english
                        I want a code with 2 relays and 2 pir
                      Thank you

                      hekH Offline
                      hekH Offline
                      hek
                      Admin
                      wrote on last edited by hek
                      #10

                      @dias

                      I want a code with 2 relays and 2 pir

                      You got to get started coding then :) If you bump into problem you can ask questions here.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      11

                      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