Motion & Relay Sensor issue



  • 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



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

    if (lastTripped != tripped ) {

  • Admin

    You cannot sleep a node that expects incoming messages.



  • 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. πŸ™‚



  • To kunall
    Have You solved this problem?



  • 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.



  • 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...



  • 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.



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


  • Admin

    @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.



Suggested Topics

  • 2
  • 26
  • 1
  • 8
  • 13
  • 2
  • 89
  • 28

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts