relay motion domoticz help

  • Hi,

    i ḿ trying to make my own sketch. using a sketch form has a working relay. i'm trying to implement a motion sensor with no luck so far. can anybody help me?
    here s my sketch;

    // Example sketch showing how to control physical relays.
    // This example will remember relay state even after power failure.

    #include <MySensor.h>
    #include <SPI.h>

    #define RELAY_1 4 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 1 // Total number of attached relays
    #define RELAY_ON 0 // GPIO value to write to turn on attached relay
    #define RELAY_OFF 1 // GPIO value to write to turn off attached relay
    #define CHILD_ID 1 // Id of the sensor child
    #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)

    bool state;
    bool value;

    MySensor gw;
    MyMessage msg(1,V_LIGHT);

    void setup()
    // Initialize library and add callback for incoming messages
    gw.begin(incomingMessage, AUTO, true);
    // Send the sketch version information to the gateway and Controller
    gw.sendSketchInfo("RelayDoMoticz", "1.0");

    // Fetch relay status
    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)
    gw.present(sensor, V_LIGHT);
    // Then set relay pins in output mode
    pinMode(pin, OUTPUT);
    // Set relay to last known state (using eeprom storage)
    digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg(CHILD_ID,V_TRIPPED);
    gw.send(msg.set(value==HIGH ? 1 : 0));

      // Send the sketch version information to the gateway and Controller

    gw.sendSketchInfo("Motion Sensor", "1.0");

    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);


    void loop()
    // Alway process incoming messages whenever possible

    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(message.sensor-1+RELAY_1, 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(", New status: ");
    // Read digital motion value
    boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;

    gw.send(msg.set(tripped?"1":"0")); // Send tripped value to gw


  • @floris I have got it working!!!!!using a motion/relay sketch from the forum.


  • it might be that this line

    gw.present(sensor, V_LIGHT);

    should be changed to

    gw.present(sensor, S_LIGHT);

  • hello, I search for a code for 2 pir sensor and relay 2
      it exists?

  • @floris can you post the final sketch you used? I am having the same issue.

Log in to reply

Suggested Topics