motion sensor and 2 relay code



  • hi all
    im running mysensors 2.0 with the following code on a node

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik Ekblad
     *
     * DESCRIPTION
     * Motion Sensor example using HC-SR501
     * http://www.mysensors.org/build/motion
     *
     */
    
    // Enable debug print
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #include <MySensors.h>
    
    unsigned long SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
    #define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    #define RELAY_1  4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define RELAY_2  5  // Arduino Digital I/O pin number for first relay
    #define NUMBER_OF_RELAYS 2 // 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 CHILD_ID 1   // Id of the sensor child
    
    // Initialize motion message
    MyMessage msg(CHILD_ID, V_TRIPPED);
    void before()
    {
        for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
            // Then set relay pins in output mode
            pinMode(pin, OUTPUT);
            // Set relay to last known state (using eeprom storage)
            digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
        }
    }
    
    void setup()
    {
        pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
    }
    
    void presentation()
    {
        // Send the sketch version information to the gateway and Controller
        sendSketchInfo("Motion Sensor and officelights", "1.2");
    
        // Register all sensors to gw (they will be created as child devices)
        present(CHILD_ID, S_MOTION);
        for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
            // Register all sensors to gw (they will be created as child devices)
            present(sensor, S_BINARY);
    }
    }
    
    void loop()
    {
        // Read digital motion value
        bool tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
    
        Serial.println(tripped);
        send(msg.set(tripped?"1":"0"));  // Send tripped value to gw
    
        // Sleep until interrupt comes in on motion sensor. Send update every two minute.
        sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), CHANGE, SLEEP_TIME);
    }
    
    void receive(const MyMessage &message)
    {
        // We only expect one type of message from controller. But we better check anyway.
        if (message.type==V_STATUS) {
            // Change relay state
            digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
            // Store state in eeprom
            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());
        }
    }
    
    

    its a mix of copies of different examples but i think i stuffed it up
    i dont see any communication from domoticz when i switch my relays


  • Mod

    If you use sleep the node can't receive anything, try using wait(500)



  • This is my sketch for relays + motion sensor:

    You can specify everything in the "#define"-section of the code.
    Features:

    • multiple Relays
    • optional motion sensor
    • specify which relays should be triggered through the motion sensor
    • child ID of the motion sensor: 0
    • The Child-IDs of the attached relays range from 1 up to (1-(NUMBER_OF_RELAYS))

    A few things are missing at the moment (e.g. report the state of the motion-activated relays to the controller), but it should work out of the box.

    /**
       The MySensors Arduino library handles the wireless radio link and protocol
       between your home built sensors/actuators and HA controller of choice.
       The sensors forms a self healing radio network with optional repeaters. Each
       repeater and gateway builds a routing tables in EEPROM which keeps track of the
       network topology allowing messages to be routed to nodes.
    
       Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
       Copyright (C) 2013-2015 Sensnology AB
       Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
    
       Documentation: http://www.mysensors.org
       Support Forum: http://forum.mysensors.org
    
       This program is free software; you can redistribute it and/or
       modify it under the terms of the GNU General Public License
       version 2 as published by the Free Software Foundation.
    
     *******************************
    
       REVISION HISTORY
       Version 1.0 - Henrik Ekblad
       Version 1.1 - HenryWhite
    
       DESCRIPTION
       Example sketch showing how to control physical relays.
       This example will remember relay state after power failure.
       Optional attachment of motion sensor to control the relays is possible.
       Note: The Child-IDs of the attached relays range from 1 up to (1-(NUMBER_OF_RELAYS))
    */
    
    //----------------------- Library Configuration ---------------------
    #define MY_DEBUG                          // uncomment to enable debug prints to serial monitor
    //#define MY_REPEATER_FEATURE               // uncomment to enable repeater functionality for this node
    //#define MY_NODE_ID 20                       // uncomment to define static node ID
    
    // Enable and uncomment attached radio type
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    //#define MY_TRANSPORT_WAIT_READY_MS 1      // uncomment this to enter the loop() and setup()-function even when the node cannot be registered to gw
    
    //----------------------- Relay Configuration -----------------------
    const int RELAYS[] = {6, 3, 5};             // digital pins of attached relays
    #define NUMBER_OF_RELAYS 3                  // 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
    
    //----------------------- Motion Sensor Configuration ---------------
    #define MOTION                              // un-comment to enable motion sensing
    #define MOTION_PIN 2                        // The digital input pin of the motion sensor
    const int MOTION_RELAY_PINS[] = {5};        // The digital input pins of the relays, which should be triggered through motion
    #define NUMBER_OF_MOTION_RELAYS 1           // Total number of relays, which should be triggered through motion
    #define MOTION_CHILD_ID 0                   // Set the child id of the motion sensor
    
    //----------------------- DO NOT CHANGE -----------------------------
    #include <MySensors.h>
    MyMessage msg(MOTION_CHILD_ID, V_TRIPPED);  // Initialize motion message
    bool lastTripped = 0;
    
    
    void before()
    {
      int i;
      for (int sensor = 1, i = 0; sensor <= NUMBER_OF_RELAYS; sensor++, i++) {
        // set relay pins to output mode
        pinMode(RELAYS[i], OUTPUT);
        // Restore relay to last known state (using eeprom storage)
        digitalWrite(RELAYS[i], loadState(sensor) ? RELAY_ON : RELAY_OFF);
      }
      // set motion pin to output mode, if MOTION is defined
    #ifdef MOTION
      pinMode(MOTION_PIN, INPUT);
    #endif
    }
    
    void setup()
    {
      
    }
    
    void presentation()
    {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Relay/Motion", "1.0");
    
      // Register all sensors to gw (they will be created as child devices)
      for (int sensor = 1; sensor <= NUMBER_OF_RELAYS; sensor++) {
        present(sensor, S_BINARY);
      }
    #ifdef MOTION
      present(MOTION_CHILD_ID, S_MOTION);
    #endif
    
    }
    
    
    void loop()
    {
    #ifdef MOTION
      // Read digital motion value
      bool tripped = digitalRead(MOTION_PIN) == HIGH;
      
      if (lastTripped != tripped) {
        Serial.print("New Motion State: ");
        Serial.println(tripped);
        // Change relay states and store state in eeprom
        for (int i = 0; i < NUMBER_OF_MOTION_RELAYS; i++) {
          digitalWrite(MOTION_RELAY_PINS[i], tripped ? RELAY_ON : RELAY_OFF);
          saveState(i, tripped);
        }
        // Send tripped value to gw
        send(msg.set(tripped ? "1" : "0"));
        lastTripped = tripped;
      }
    #endif
    }
    
    void receive(const MyMessage &message)
    {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type == V_STATUS) {
        // Change relay state
        digitalWrite(RELAYS[message.sensor - 1], message.getBool() ? RELAY_ON : RELAY_OFF);
        // Store state in eeprom
        saveState(message.sensor - 1, message.getBool());
        // Write some debug info
        Serial.print("Incoming change for sensor:");
        Serial.print(message.sensor);
        Serial.print(", New status: ");
        Serial.println(message.getBool());
      }
    }
    


  • ok im really new at this coding ide stuff...what if i want none of the relays triggered by the motion sensor
    i would prefer to link each component back to domoticz by itself, and let it dictate what happens each part seperately...then i can change it easier if i want to leverage other things later


  • Mod

    Just remove the FOR cycle in the loop function



  • @gohan said in motion sensor and 2 relay code:

    Just remove the FOR cycle in the loop function

    It's much easier than that: Just set NUMBER_OF_MOTION_RELAYS to 0



  • i have attempted both
    the motion sensor now turns on and off on demand by waving my hand in front of it perfectly

    but the relay doesnt respond at all....

    the node receives the following message
    212874 TSF:MSG:READ,0-0-1,s=1,c=1,t=2,pt=0,l=1,sg=0:1
    Incoming change for sensor:1, New status: 1

    so i know the node receives it, but it doesnt seem to do anything with the message

    this is current code

    /**
       The MySensors Arduino library handles the wireless radio link and protocol
       between your home built sensors/actuators and HA controller of choice.
       The sensors forms a self healing radio network with optional repeaters. Each
       repeater and gateway builds a routing tables in EEPROM which keeps track of the
       network topology allowing messages to be routed to nodes.
    
       Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
       Copyright (C) 2013-2015 Sensnology AB
       Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
    
       Documentation: http://www.mysensors.org
       Support Forum: http://forum.mysensors.org
    
       This program is free software; you can redistribute it and/or
       modify it under the terms of the GNU General Public License
       version 2 as published by the Free Software Foundation.
    
     *******************************
    
       REVISION HISTORY
       Version 1.0 - Henrik Ekblad
       Version 1.1 - HenryWhite
    
       DESCRIPTION
       Example sketch showing how to control physical relays.
       This example will remember relay state after power failure.
       Optional attachment of motion sensor to control the relays is possible.
       Note: The Child-IDs of the attached relays range from 1 up to (1-(NUMBER_OF_RELAYS))
    */
    
    //----------------------- Library Configuration ---------------------
    #define MY_DEBUG                          // uncomment to enable debug prints to serial monitor
    //#define MY_REPEATER_FEATURE               // uncomment to enable repeater functionality for this node
    #define MY_NODE_ID 1                       // uncomment to define static node ID
    
    // Enable and uncomment attached radio type
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    //#define MY_TRANSPORT_WAIT_READY_MS 1      // uncomment this to enter the loop() and setup()-function even when the node cannot be registered to gw
    
    //----------------------- Relay Configuration -----------------------
    const int RELAYS[] = {5};             // digital pins of attached relays
    #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
    
    //----------------------- Motion Sensor Configuration ---------------
    #define MOTION                              // un-comment to enable motion sensing
    #define MOTION_PIN 3                        // The digital input pin of the motion sensor
    const int MOTION_RELAY_PINS[] = {5};        // The digital input pins of the relays, which should be triggered through motion
    #define NUMBER_OF_MOTION_RELAYS 0          // Total number of relays, which should be triggered through motion
    #define MOTION_CHILD_ID 0                   // Set the child id of the motion sensor
    
    //----------------------- DO NOT CHANGE -----------------------------
    #include <MySensors.h>
    MyMessage msg(MOTION_CHILD_ID, V_TRIPPED);  // Initialize motion message
    bool lastTripped = 0;
    
    
    void before()
    {
      int i;
      for (int sensor = 1, i = 0; sensor <= NUMBER_OF_RELAYS; sensor++, i++) {
        // set relay pins to output mode
        pinMode(RELAYS[i], OUTPUT);
        // Restore relay to last known state (using eeprom storage)
        digitalWrite(RELAYS[i], loadState(sensor) ? RELAY_ON : RELAY_OFF);
      }
      // set motion pin to output mode, if MOTION is defined
    #ifdef MOTION
      pinMode(MOTION_PIN, INPUT);
    #endif
    }
    
    void setup()
    {
      
    }
    
    void presentation()
    {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Office Switches", "2.0");
    
      // Register all sensors to gw (they will be created as child devices)
      for (int sensor = 1; sensor <= NUMBER_OF_RELAYS; sensor++) {
        present(sensor, S_BINARY);
      }
    #ifdef MOTION
      present(MOTION_CHILD_ID, S_MOTION);
    #endif
    
    }
    
    
    void loop()
    {
    #ifdef MOTION
      // Read digital motion value
      bool tripped = digitalRead(MOTION_PIN) == HIGH;
      
      if (lastTripped != tripped) {
        Serial.print("New Motion State: ");
        Serial.println(tripped);
        
        // Send tripped value to gw
        send(msg.set(tripped ? "1" : "0"));
        lastTripped = tripped;
      }
    #endif
    }
    
    void receive(const MyMessage &message)
    {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type == V_STATUS) {
        // Change relay state
        digitalWrite(RELAYS[message.sensor - 1], message.getBool() ? RELAY_ON : RELAY_OFF);
        // Store state in eeprom
        saveState(message.sensor - 1, message.getBool());
        // Write some debug info
        Serial.print("Incoming change for sensor:");
        Serial.print(message.sensor);
        Serial.print(", New status: ");
        Serial.println(message.getBool());
      }
    }
    

  • Mod

    @markjgabb are you sure you connected the relay to the right pin? I usually try with LEDs attached to pins to check if all are working, it is easy to confuse pin numbers



  • ill double check it tonight, but im almost positive i have the pins right....
    it was working untill i removed the for part of the loop


  • Mod

    OK, then leave the FOR cycle and set the other parameter to 0 as suggested earlier




Log in to reply
 

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.