Combining a motion sensor with an led on one node



  • Using mockmysensors as a guide I tried to combine a pir motion sensor with one led. The motion sensor is being picked up by home assistant fine but its seeing the led as a switch and not a light and won't turn the light on. I don't need the led to be dimmable, just on and off. I'm going to be using both in automations once I get the code right.

    I've tested all the parts using basic arduino coding to make sure they work. I already have a gateway set up, and a pir motion sensor elsewhere in my home that is working and has been used in its own automations.
    I've watched videos and searched google trying to figure out how to do this, at this point I think I need a bit of help sorting this out.

    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_RF24
    //#define MY_RADIO_NRF5_ESB
    //#define MY_RADIO_RFM69
    //#define MY_RADIO_RFM95
    
    #define MY_NODE_ID 5
    
    #include <MySensors.h>
    
    // Wait times
    #define LONG_WAIT 500
    #define SHORT_WAIT 50
    
    #define SKETCH_NAME "My Kitchen"
    #define SKETCH_VERSION "5"
    
    // Define Sensors ids
    
    #define ID_S_MOTION            1
    #define ID_S_LIGHT             2
    
    // Define input output sensors
    #define MOTION_DIGITAL_INPUT_SENSOR 3
    #define LIGHT_PIN 8
    
    // Global Vars
    uint32_t SLEEP_TIME = 9000; // Sleep time between reads (in milliseconds)
    
    #define LIGHT_OFF 0
    #define LIGHT_ON 1
    
    int16_t last_state = LIGHT_OFF;
    
    //Instantiate Messages objects
    
    
    #ifdef ID_S_MOTION // V_TRIPPED, V_ARMED
    MyMessage msg_S_MOTION_T(ID_S_MOTION, V_TRIPPED);
    #endif
    
    #ifdef ID_S_LIGHT
    MyMessage msg_S_LIGHT(ID_S_LIGHT, V_LIGHT);
    bool isLightOn = 1;
    #endif
    
    
    void setup()
    {
      pinMode(MOTION_DIGITAL_INPUT_SENSOR, INPUT);
      pinMode(LIGHT_PIN, OUTPUT);
    
      wait(LONG_WAIT);
      Serial.println("GW Started");
    }
    
    void presentation()
    {
      // Send the Sketch Version Information to the Gateway
      Serial.print("Send Sketch Info: ");
      sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
      Serial.print(SKETCH_NAME);
      Serial.println(SKETCH_VERSION);
      wait(LONG_WAIT);
    
    
      // Register all sensors to gw (they will be created as child devices)
      Serial.println("Presenting Nodes");
      Serial.println("________________");
    
    #ifdef ID_S_MOTION
      Serial.println("  S_MOTION");
      present(ID_S_MOTION, S_MOTION, "Kitchen Motion");
      wait(SHORT_WAIT);
    #endif
    
    #ifdef ID_S_LIGHT
      Serial.println("  S_LIGHT");
      present(ID_S_LIGHT, S_LIGHT, "Kitchen Night Light");
      wait(SHORT_WAIT);
    #endif
    
      Serial.println("________________");
    
    }
    
    void loop()
    {
      Serial.println("Request Time");
      requestTime();
      wait(LONG_WAIT);
    
    #ifdef ID_S_MOTION
      void motion();
      {
        // Read digital motion value
        bool tripped = digitalRead(MOTION_DIGITAL_INPUT_SENSOR) == HIGH;
    
        Serial.println(tripped);
        send(msg_S_MOTION_T.set(tripped ? "1" : "0")); // Send tripped value to gw
    
        // Sleep until interrupt comes in on motion sensor. Send update every two minute.
        sleep(digitalPinToInterrupt(MOTION_DIGITAL_INPUT_SENSOR), CHANGE, SLEEP_TIME);
      }
    #endif
      {
    #ifdef ID_S_LIGHT
        void light();
        Serial.print("Light is: " );
        Serial.println(isLightOn ? "On" : "Off");
    
        send(msg_S_LIGHT.set(isLightOn));
      }
    #endif
    
    }
    void receive(const MyMessage & message)
    
    
    //    case V_STATUS: // V_LIGHT:
    #ifdef ID_S_LIGHT
    {
      //For this example, just print the light status to console.
      if ( last_state == LIGHT_OFF ) {
        Serial.println( "Light state: OFF" );
      } else {
        Serial.print( "Light state: ON, Level: " );
      }
    }
    #endif
    
      //     default:
      //        Serial.print("Unknown/Unimplemented message type: ");
      //        Serial.println(message.getType());
      //    }
    
    
    

  • Hero Member

    @TonicCorvid ,

    issue 1: The light node cannot receive messages from home assistant while it is sleeping. You may start by replacing the sleep command by a wait:

    wait( SLEEP_TIME );
    

    issue 2: Usually Home Assistant needs the light send its current status, in order to 'activate' it. Try adding a line in the end of presentation part:

    #ifdef ID_S_LIGHT
      Serial.println("  S_LIGHT");
      present(ID_S_LIGHT, S_LIGHT, "Kitchen Night Light");
      wait(SHORT_WAIT);
      send(msg_S_LIGHT.set(isLightOn));   <<<<<<<<<<<<<<
    #endif
    


  • @rvendrame Okay, I changed the light line. I'm not sure how/where to change the sleep time.



  • @TonicCorvid
    Some additional remarks:

    • if the node is not battery powered, I'd recommend to use a "non blocking loop" instead of "sleep" or "wait". See the "SLEEP_MODE" - false tree in pulse water meter sketch as a reference how to implement this. Note: this only is needed in case you want some kind of regular reporting (e.g. for a heartbeat message). So just looping and checking pir state (and switching light off after some periode of time) might be closer to what you want to achieve - and easier to program.
    • in case it's battery powered and your controller supports the smartSleep feature, imo using that feature instead of wait() is recommended.
    • nut sure, but using long wait() periodes in battery powered nodes might dramatically shorten battery life.

  • Hero Member

    @TonicCorvid basically you just replace this line:

    sleep(digitalPinToInterrupt(MOTION_DIGITAL_INPUT_SENSOR), CHANGE, SLEEP_TIME);
    

    by this:

    wait( SLEEP_TIME );
    

    But as pointed by @rejoe2 , this will drain the battery quite fast, as the node will be always on-line. Usually for actuator nodes (such as lights), a power supply is used, instead of batteries.



  • @rvendrame Ah, I see. Yeah this node will be plugged in so I'm not worried there. The light still isn't working correctly yet.


Log in to reply
 

Suggested Topics

  • 1
  • 3
  • 5
  • 10
  • 1
  • 3

1
Online

11.4k
Users

11.1k
Topics

112.7k
Posts