pir combination with working 4-relay and 4 buttons



  • I have a working 4-relay with 4 buttons project. Now I want to integrade a pir that only send the status to my Domoticz controler. I am a newbee but lerning.
    What I have now is this

    #include <MySensor.h>
    #include <SPI.h>
    #include "Bounce2.h"
    
    unsigned long SLEEP_TIME = 10000; // 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 INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
    #define CHILD_ID 1   // Id of the sensor child
    
    #define RELAY_ON 0                      // switch around for realy HIGH/LOW state
    #define RELAY_OFF 1
    
    //
    MySensor gw;
    // Initialize motion message
    MyMessage msg(CHILD_ID, V_TRIPPED);
    
    #define RADIO_ID 11                    // radio Id, whatever channel you assigned to
    #define noRelays 4
    const int relayPin[] = {A1, A2, A3, A0}; //  switch around pins to your desire
    const int buttonPin[] = {A5, 6, 7, 8}; //  switch around pins to your desire
    
    class Relay             // relay class, store all relevant data (equivalent to struct)
    {
    public: 
      int buttonPin;                    // physical pin number of button
      int relayPin;                     // physical pin number of relay
      byte oldValue;                    // last Values for key (debounce)
      boolean relayState;               // relay status (also stored in EEPROM)
    };
    
    Relay Relays[noRelays];
    Bounce debouncer[noRelays];
    MyMessage msg[noRelays];
    
    void setup()
    {  
      gw.begin();
    
      // 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);
      
    }
    
    {
      gw.begin(incomingMessage, RADIO_ID, true);
      delay(250);
      gw.sendSketchInfo("Multy-Relay&Pulsanti", "0.2");
      delay(250);
    
      // Initialize Relays with corresponding buttons
      for (int i = 0; i < noRelays; i++)
      {
        Relays[i].buttonPin = buttonPin[i];              // assign physical pins
        Relays[i].relayPin = relayPin[i];
        msg[i].sensor = i;                                   // initialize messages
        msg[i].type = V_LIGHT;
        debouncer[i] = Bounce();                        // initialize debouncer
        debouncer[i].attach(buttonPin[i]);
        debouncer[i].interval(5);
        pinMode(Relays[i].buttonPin, INPUT_PULLUP);
        pinMode(Relays[i].relayPin, OUTPUT);
        Relays[i].relayState = gw.loadState(i);                               // retrieve last values from EEPROM
        digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
        gw.send(msg[i].set(Relays[i].relayState ? true : false));                 // make controller aware of last status
        gw.present(i, S_LIGHT);                               // present sensor to gateway
        delay(250);
    
      }
    }
    
    void loop()
    {     
      // Read digital motion value
      boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
            
      Serial.println(tripped);
      gw.send(msg.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);
    }
    
    {
      gw.process();
      for (byte i = 0; i < noRelays; i++)
      {
        debouncer[i].update();
        byte value = debouncer[i].read();
        if (value != Relays[i].oldValue && value == 0)
        {
          Relays[i].relayState = !Relays[i].relayState;
          digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF);
          gw.send(msg[i].set(Relays[i].relayState ? true : false));
          gw.saveState( i, Relays[i].relayState );
        }                 // save sensor state in EEPROM (location == sensor number)
    
        Relays[i].oldValue = value;
    
      }
    }
    
    // process incoming message
    void incomingMessage(const MyMessage &message)
    {
    
      if (message.type == V_LIGHT)
      {
        if (message.sensor < noRelays)            // check if message is valid for relays..... previous line  [[[ if (message.sensor <=noRelays){ ]]]
        {
          Relays[message.sensor].relayState = message.getBool();
          digitalWrite(Relays[message.sensor].relayPin, Relays[message.sensor].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
          gw.saveState( message.sensor, Relays[message.sensor].relayState ); // save sensor state in EEPROM (location == sensor number)
        }
      }
    }
    

    During the compilation I get the next error

    _4relay:34: error: conflicting declaration 'MyMessage msg [4]'
    
     MyMessage msg[noRelays];
    
                           ^
    
    _4relay:16: error: 'msg' has a previous declaration as 'MyMessage msg'
    
     MyMessage msg(CHILD_ID, V_TRIPPED);
    
               ^
    
    _4relay:49: error: expected unqualified-id before '{' token
    
     {```
    
    I understand that it has to do something with the MyMessage msg but don't know hoe to solve.
    
    Who can help me??

  • Admin

    You cannot declare two variables with the same name ("msg" in your case). The error message is pretty clear in this case.



  • Still having problems with my combination design. This is the working 4 relais sensor:

    #include <MySensor.h>
    #include <SPI.h>
    #include "Bounce2.h"
    
    #define RELAY_ON 0                      // switch around for realy HIGH/LOW state
    #define RELAY_OFF 1
    
    //
    MySensor gw;
    
    #define RADIO_ID 11                    // radio Id, whatever channel you assigned to
    #define noRelays 4
    const int relayPin[] = {A1, A2, A3, A0}; //  switch around pins to your desire
    const int buttonPin[] = {A5, 6, 7, 8}; //  switch around pins to your desire
    
    class Relay             // relay class, store all relevant data (equivalent to struct)
    {
    public: 
      int buttonPin;                    // physical pin number of button
      int relayPin;                     // physical pin number of relay
      byte oldValue;                    // last Values for key (debounce)
      boolean relayState;               // relay status (also stored in EEPROM)
    };
    
    Relay Relays[noRelays];
    Bounce debouncer[noRelays];
    MyMessage msg[noRelays];
    
    void setup()
    {
      gw.begin(incomingMessage, RADIO_ID, true);
      delay(250);
      gw.sendSketchInfo("Multy-Relay&Pulsanti", "0.2");
      delay(250);
    
      // Initialize Relays with corresponding buttons
      for (int i = 0; i < noRelays; i++)
      {
        Relays[i].buttonPin = buttonPin[i];              // assign physical pins
        Relays[i].relayPin = relayPin[i];
        msg[i].sensor = i;                                   // initialize messages
        msg[i].type = V_LIGHT;
        debouncer[i] = Bounce();                        // initialize debouncer
        debouncer[i].attach(buttonPin[i]);
        debouncer[i].interval(5);
        pinMode(Relays[i].buttonPin, INPUT_PULLUP);
        pinMode(Relays[i].relayPin, OUTPUT);
        Relays[i].relayState = gw.loadState(i);                               // retrieve last values from EEPROM
        digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
        gw.send(msg[i].set(Relays[i].relayState ? true : false));                 // make controller aware of last status
        gw.present(i, S_LIGHT);                               // present sensor to gateway
        delay(250);
    
      }
    }
    
    void loop()
    {
      gw.process();
      for (byte i = 0; i < noRelays; i++)
      {
        debouncer[i].update();
        byte value = debouncer[i].read();
        if (value != Relays[i].oldValue && value == 0)
        {
          Relays[i].relayState = !Relays[i].relayState;
          digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF);
          gw.send(msg[i].set(Relays[i].relayState ? true : false));
          gw.saveState( i, Relays[i].relayState );
        }                 // save sensor state in EEPROM (location == sensor number)
    
        Relays[i].oldValue = value;
    
      }
    }
    
    // process incoming message
    void incomingMessage(const MyMessage &message)
    {
    
      if (message.type == V_LIGHT)
      {
        if (message.sensor < noRelays)            // check if message is valid for relays..... previous line  [[[ if (message.sensor <=noRelays){ ]]]
        {
          Relays[message.sensor].relayState = message.getBool();
          digitalWrite(Relays[message.sensor].relayPin, Relays[message.sensor].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
          gw.saveState( message.sensor, Relays[message.sensor].relayState ); // save sensor state in EEPROM (location == sensor number)
        }
      }
    }
    

    I try so combine it with the "motion Sensor" example from my sensors:

    /**
     * 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
     *
     */
    
    #include <MySensor.h>  
    #include <SPI.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 INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
    #define CHILD_ID 1   // Id of the sensor child
    
    MySensor gw;
    // Initialize motion message
    MyMessage msg(CHILD_ID, V_TRIPPED);
    
    void setup()  
    {  
      gw.begin();
    
      // 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()     
    {     
      // Read digital motion value
      boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
            
      Serial.println(tripped);
      gw.send(msg.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);
    }
    
    
    

    Now it looks like this combined with each other without compilation errors:

     #include <MySensor.h>
    #include <SPI.h>
    #include "Bounce2.h"
    
    unsigned long SLEEP_TIME = 90000; // 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 INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
    #define CHILD_ID 1   // Id of the sensor child
    
    #define RELAY_ON 0                      // switch around for realy HIGH/LOW state
    #define RELAY_OFF 1
    
    //
    MySensor gw;
    // Initialize motion message
    MyMessage motionmsg(CHILD_ID, V_TRIPPED);
    
    #define RADIO_ID 11                    // radio Id, whatever channel you assigned to
    #define noRelays 4
    const int relayPin[] = {A1, A2, A3, A0}; //  switch around pins to your desire
    const int buttonPin[] = {A5, 6, 7, 8}; //  switch around pins to your desire
    
    class Relay             // relay class, store all relevant data (equivalent to struct)
    {
    public: 
      int buttonPin;                    // physical pin number of button
      int relayPin;                     // physical pin number of relay
      byte oldValue;                    // last Values for key (debounce)
      boolean relayState;               // relay status (also stored in EEPROM)
    };
    
    Relay Relays[noRelays];
    Bounce debouncer[noRelays];
    MyMessage msg[noRelays];
    
    void setup()
    {  
      gw.begin();
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Motion Sensor", "gordijn");
    
      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);
      
    
    
    
      gw.begin(incomingMessage, RADIO_ID, true);
      delay(250);
      gw.sendSketchInfo("Multy-Relay", "gordijn");
      delay(250);
    
      // Initialize Relays with corresponding buttons
      for (int i = 0; i < noRelays; i++)
      {
        Relays[i].buttonPin = buttonPin[i];              // assign physical pins
        Relays[i].relayPin = relayPin[i];
        msg[i].sensor = i;                                   // initialize messages
        msg[i].type = V_LIGHT;
        debouncer[i] = Bounce();                        // initialize debouncer
        debouncer[i].attach(buttonPin[i]);
        debouncer[i].interval(5);
        pinMode(Relays[i].buttonPin, INPUT_PULLUP);
        pinMode(Relays[i].relayPin, OUTPUT);
        Relays[i].relayState = gw.loadState(i);                               // retrieve last values from EEPROM
        digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
        gw.send(msg[i].set(Relays[i].relayState ? true : false));                 // make controller aware of last status
        gw.present(i, S_LIGHT);                               // present sensor to gateway
        delay(250);
    
      }
    }
    
    void loop()
    {     
      // Read digital motion value
      boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
            
      Serial.println(tripped);
      gw.send(motionmsg.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);
    
      gw.process();
      for (byte i = 0; i < noRelays; i++)
      {
        debouncer[i].update();
        byte value = debouncer[i].read();
        if (value != Relays[i].oldValue && value == 0)
        {
          Relays[i].relayState = !Relays[i].relayState;
          digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF);
          gw.send(msg[i].set(Relays[i].relayState ? true : false));
          gw.saveState( i, Relays[i].relayState );
        }                 // save sensor state in EEPROM (location == sensor number)
    
        Relays[i].oldValue = value;
    
      }
    }
    
    // process incoming message
    void incomingMessage(const MyMessage &message)
    {
    
      if (message.type == V_LIGHT)
      {
        if (message.sensor < noRelays)            // check if message is valid for relays..... previous line  [[[ if (message.sensor <=noRelays){ ]]]
        {
          Relays[message.sensor].relayState = message.getBool();
          digitalWrite(Relays[message.sensor].relayPin, Relays[message.sensor].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
          gw.saveState( message.sensor, Relays[message.sensor].relayState ); // save sensor state in EEPROM (location == sensor number)
        }
      }
    }```
    
    during starting-up the sensor all relays get activated. If a movement takes place (pir active) the relays deactvate. I am not able to controle my relays with the buttons also a part of the project, disconnecting the pir and the reais works fine as well as the buttons.
    
    Who give it a try to solve it?
    
    Dick


  • Can anybody help me to get this combi sensor to work?

    This is the script at this moment and during compilation I get an error:

    expected initializer before 'MyMessage'bolded text

    #include <MySensor.h>
    #include <SPI.h>
    #include "Bounce2.h"
    
    unsigned long SLEEP_TIME = 90000; // 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 CHILD_ID 1   // Id of the sensor child
    boolean lastmotionstate
    #define RELAY_ON 0                      // switch around for realy HIGH/LOW state
    #define RELAY_OFF 1
    
    
    MySensor gw;
    // Initialize motion message
    MyMessage motionmsg(CHILD_ID, V_TRIPPED);
    
    #define RADIO_ID 11                    // radio Id, whatever channel you assigned to
    #define noRelays 4
    const int relayPin[] = {A1, A2, A3, A0}; //  switch around pins to your desire
    const int buttonPin[] = {A5, 6, 7, 8}; //  switch around pins to your desire
    
    class Relay             // relay class, store all relevant data (equivalent to struct)
    {
    public: 
     int buttonPin;                    // physical pin number of button
     int relayPin;                     // physical pin number of relay
     byte oldValue;                    // last Values for key (debounce)
     boolean relayState;               // relay status (also stored in EEPROM)
    }
    
    Relay Relays[noRelays];
    Bounce debouncer[noRelays];
    MyMessage msg[noRelays];
    
    void setup()
    {  
     gw.begin();
    lastmotionstate =0;
     // Send the sketch version information to the gateway and Controller
     gw.sendSketchInfo("Motion Sensor", "gordijn");
    
     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);
     
    
    
    
     gw.begin(incomingMessage, RADIO_ID, true);
     delay(250);
     gw.sendSketchInfo("Multy-Relay", "gordijn");
     delay(250);
    
     // Initialize Relays with corresponding buttons
     for (int i = 0; i < noRelays; i++)
     {
       Relays[i].buttonPin = buttonPin[i];              // assign physical pins
       Relays[i].relayPin = relayPin[i];
       msg[i].sensor = i;                                   // initialize messages
       msg[i].type = V_LIGHT;
       debouncer[i] = Bounce();                        // initialize debouncer
       debouncer[i].attach(buttonPin[i]);
       debouncer[i].interval(5);
       pinMode(Relays[i].buttonPin, INPUT_PULLUP);
       pinMode(Relays[i].relayPin, OUTPUT);
       Relays[i].relayState = gw.loadState(i);                               // retrieve last values from EEPROM
       digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
       gw.send(msg[i].set(Relays[i].relayState ? true : false));                 // make controller aware of last status
       gw.present(i, S_LIGHT);                               // present sensor to gateway
       delay(250);
    
     }
    }
    
    void loop()
    {     
     // Read digital motion value
     boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
           
     Serial.println(tripped);
       if (lastmotionstate != tripped)
       {
    gw.send(motionmsg.set(tripped?"1":"0"));  // Send tripped value to gw 
    lastmotionstate =tripped;
    }
     // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
    //  gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME);
    
     gw.process();
     for (byte i = 0; i < noRelays; i++)
     {
       debouncer[i].update();
       byte value = debouncer[i].read();
       if (value != Relays[i].oldValue && value == 0)
       {
         Relays[i].relayState = !Relays[i].relayState;
         digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF);
         gw.send(msg[i].set(Relays[i].relayState ? true : false));
         gw.saveState( i, Relays[i].relayState );
       }                 // save sensor state in EEPROM (location == sensor number)
    
       Relays[i].oldValue = value;
    
     }
    }
    
    // process incoming message
    void incomingMessage(const MyMessage &message)
    {
    
     if (message.type == V_LIGHT)
     {
       if (message.sensor < noRelays)            // check if message is valid for relays..... previous line  [[[ if (message.sensor <=noRelays){ ]]]
       {
         Relays[message.sensor].relayState = message.getBool();
         digitalWrite(Relays[message.sensor].relayPin, Relays[message.sensor].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
         gw.saveState( message.sensor, Relays[message.sensor].relayState ); // save sensor state in EEPROM (location == sensor number)
       }
     }
    }
    

  • Mod

    @Dick looks like you are missing a ; after

    boolean lastmotionstate
    


  • Thanks for the tip, I added the Boolean, so that is one step. No I thought it is in place but now I got a warning
    'Sleep' was not declarered in this scope
    that is strange because I declared it. Pleas advise me

    In#include <SPI.h>
    #include <MySensor.h>
    #include "Bounce2.h"
    
    unsigned long SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
    
    #define CHILD_ID_PIR 1
    #define RELAY_ON 0                      // switch around for realy HIGH/LOW state
    #define RELAY_OFF 1
    #define RADIO_ID 11                // Radio Id, whatever channel you assigned to
    #define PIR_SENSOR_DIGITAL_INPUT 3     // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    
    #define noRelays 4
    const int relayPin[] = {A1, A2, A3, A4}; //  switch around pins to your desire
    const int buttonPin[] = {5, 6, 7, 8}; //  switch around pins to your desire
    
    //unsigned long SLEEP_TIME = 60000; // Sleep time between reads (in milliseconds)
    
    
    
    
    
    
    class Relay             // relay class, store all relevant data (equivalent to struct)
    {
    public: 
      int buttonPin;                    // physical pin number of button
      int relayPin;                     // physical pin number of relay
      byte oldValue;                    // last Values for key (debounce)
      boolean relayState;               // relay status (also stored in EEPROM)
    };
    
    
    Relay Relays[noRelays];
    Bounce debouncer[noRelays];
    boolean metric = true;
    boolean prevPIRstatus = false;
    int lastLightLevel;
    
    MySensor gw;
    
    MyMessage msgPIR(CHILD_ID_PIR, V_TRIPPED);
    MyMessage msgRelays[noRelays];
    
    void setup()
    {
      gw.begin();
      
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Combi PIR, Temp, Hum, 4 Relay and Switch, LDR sensor", "1.0");
    
     
      // Initialize and present Relays with corresponding buttons
      for (int i = 0; i < noRelays; i++)
      {
        Relays[i].buttonPin = buttonPin[i];              // assign physical pins
        Relays[i].relayPin = relayPin[i];
        msgRelays[i].sensor = i;                                   // initialize messages
        msgRelays[i].type = V_LIGHT;
        debouncer[i] = Bounce();                        // initialize debouncer
        debouncer[i].attach(buttonPin[i]);
        debouncer[i].interval(5);
        pinMode(Relays[i].buttonPin, INPUT_PULLUP);
        pinMode(Relays[i].relayPin, OUTPUT);
        Relays[i].relayState = gw.loadState(i);                               // retrieve last values from EEPROM
        digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
        gw.send(msgRelays[i].set(Relays[i].relayState ? true : false));                 // make controller aware of last status
        gw.present(i, S_LIGHT);                               // present sensor to gateway
        delay(250);
      }
      
     
      gw.present(CHILD_ID_PIR, S_MOTION);
      
     
    }
    
    void loop()
    {
    
      // Deal with incoming messages and when enabled repeat
      gw.process();
    
      
      // Deal with PIR
      // Read digital motion value
      boolean tripped = digitalRead(PIR_SENSOR_DIGITAL_INPUT) == HIGH; 
    
      // Check if PIR Changed
      if (prevPIRstatus |= tripped) {
        Serial.println(tripped);
        gw.send(msgPIR.set(tripped?"1":"0"));  // Send tripped value to gw 
       // prevPIRstatus = tripped; // store new value to PIR status to run update on next loop when changed
      // Sleep until interrupt comes in on motion sensor. Send update every two minute.
      sleep(digitalPinToInterrupt(PIR_SENSOR_DIGITAL_INPUT), CHANGE, SLEEP_TIME);
      }
      
      // Deal with relays and buttons
      for (byte i = 0; i < noRelays; i++)
      {
        debouncer[i].update();
        byte value = debouncer[i].read();
        if (value != Relays[i].oldValue && value == 0)
        {
          Relays[i].relayState = !Relays[i].relayState;
          digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF);
          gw.send(msgRelays[i].set(Relays[i].relayState ? true : false));
          gw.saveState( i, Relays[i].relayState );
        }                 // save sensor state in EEPROM (location == sensor number)
    
        Relays[i].oldValue = value;
      }  
      
      //gw.sleep(SLEEP_TIME); //sleep a bit
    }
    
    
    
    // process incoming message
    void incomingMessage(const MyMessage &message)
    {
    
      if (message.type == V_LIGHT)
      {
        if (message.sensor < noRelays)            // check if message is valid for relays..... previous line  [[[ if (message.sensor <=noRelays){ ]]]
        {
          Relays[message.sensor].relayState = message.getBool();
          digitalWrite(Relays[message.sensor].relayPin, Relays[message.sensor].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
          gw.saveState(message.sensor, Relays[message.sensor].relayState); // save sensor state in EEPROM (location == sensor number)
        }
      }
    }
    
    `

  • Mod

    @Dick the IDE normally says which line the error occurred on. Knowing that helps a lot when troubleshooting.

    This line

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

    should probably be

    gw.sleep(digitalPinToInterrupt(PIR_SENSOR_DIGITAL_INPUT), CHANGE, SLEEP_TIME);
    


  • thats logic, I hoped that I learned something in the last cople of days but trouble shooting is something else. Thank you for your advise and go on with testing and adding other devises.



  • still strugling. it must beeasy but it isn't, mij working 4 button and 4 relais

    #include <MySensor.h>
    #include <SPI.h>
    #include "Bounce2.h"
    
    #define RELAY_ON 0                      // switch around for realy HIGH/LOW state
    #define RELAY_OFF 1
    
    //
    MySensor gw;
    
    #define RADIO_ID 11                    // radio Id, whatever channel you assigned to
    #define noRelays 4
    const int relayPin[] = {A1, A2, A3, A4}; //  switch around pins to your desire
    const int buttonPin[] = {5, 6, 7, 8}; //  switch around pins to your desire
    
    class Relay             // relay class, store all relevant data (equivalent to struct)
    {
    public: 
      int buttonPin;                    // physical pin number of button
      int relayPin;                     // physical pin number of relay
      byte oldValue;                    // last Values for key (debounce)
      boolean relayState;               // relay status (also stored in EEPROM)
    };
    
    Relay Relays[noRelays];
    Bounce debouncer[noRelays];
    MyMessage msg[noRelays];
    
    void setup()
    {
      gw.begin(incomingMessage, RADIO_ID, true);
      delay(250);
      gw.sendSketchInfo("Multy-Relay&Pulsanti", "0.2");
      delay(250);
    
      // Initialize Relays with corresponding buttons
      for (int i = 0; i < noRelays; i++)
      {
        Relays[i].buttonPin = buttonPin[i];              // assign physical pins
        Relays[i].relayPin = relayPin[i];
        msg[i].sensor = i;                                   // initialize messages
        msg[i].type = V_LIGHT;
        debouncer[i] = Bounce();                        // initialize debouncer
        debouncer[i].attach(buttonPin[i]);
        debouncer[i].interval(5);
        pinMode(Relays[i].buttonPin, INPUT_PULLUP);
        pinMode(Relays[i].relayPin, OUTPUT);
        Relays[i].relayState = gw.loadState(i);                               // retrieve last values from EEPROM
        digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
        gw.send(msg[i].set(Relays[i].relayState ? true : false));                 // make controller aware of last status
        gw.present(i, S_LIGHT);                               // present sensor to gateway
        delay(250);
    
      }
    }
    
    void loop()
    {
      gw.process();
      for (byte i = 0; i < noRelays; i++)
      {
        debouncer[i].update();
        byte value = debouncer[i].read();
        if (value != Relays[i].oldValue && value == 0)
        {
          Relays[i].relayState = !Relays[i].relayState;
          digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF);
          gw.send(msg[i].set(Relays[i].relayState ? true : false));
          gw.saveState( i, Relays[i].relayState );
        }                 // save sensor state in EEPROM (location == sensor number)
    
        Relays[i].oldValue = value;
    
      }
    }
    
    // process incoming message
    void incomingMessage(const MyMessage &message)
    {
    
      if (message.type == V_LIGHT)
      {
        if (message.sensor < noRelays)            // check if message is valid for relays..... previous line  [[[ if (message.sensor <=noRelays){ ]]]
        {
          Relays[message.sensor].relayState = message.getBool();
          digitalWrite(Relays[message.sensor].relayPin, Relays[message.sensor].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
          gw.saveState( message.sensor, Relays[message.sensor].relayState ); // save sensor state in EEPROM (location == sensor number)
        }
      }
    }
    

    Combined with a pir like this where the pir is working perfect but the buttons in combi with the relay are not working anymore. What is wrong in this comnined script?
    NO ERRORS DURING COMPILATION!

    #include <SPI.h>
    #include <MySensor.h>
    #include "Bounce2.h"
    
    unsigned long SLEEP_TIME = 60000; // Sleep time between reports (in milliseconds)
    
    #define CHILD_ID_PIR 1
    #define RELAY_ON 0                      // switch around for realy HIGH/LOW state
    #define RELAY_OFF 1
    
    
    
    
    #define RADIO_ID 11                // Radio Id, whatever channel you assigned to
    #define PIR_SENSOR_DIGITAL_INPUT 3     // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    
    #define noRelays 4
    const int relayPin[] = {A1, A2, A3, A4}; //  switch around pins to your desire
    const int buttonPin[] = {5, 6, 7, 8}; //  switch around pins to your desire
    
    
    class Relay             // relay class, store all relevant data (equivalent to struct)
    {
    public: 
      int buttonPin;                    // physical pin number of button
      int relayPin;                     // physical pin number of relay
      byte oldValue;                    // last Values for key (debounce)
      boolean relayState;               // relay status (also stored in EEPROM)
    };
    
    
    Relay Relays[noRelays];
    Bounce debouncer[noRelays];
    boolean metric = true;
    boolean prevPIRstatus = false;
    
    
    MySensor gw;
    
    MyMessage msgPIR(CHILD_ID_PIR, V_TRIPPED);
    MyMessage msgRelays[noRelays];
    
    void setup()
    {
      //gw.begin();
      
      // Send the sketch version information to the gateway and Controller
      //gw.sendSketchInfo("Combi PIR, Temp, Hum, 4 Relay and Switch, LDR sensor", "1.0");
    
    
    gw.begin(incomingMessage, RADIO_ID, true);
      delay(250);
      gw.sendSketchInfo("Multy-Relay&Pulsanti", "0.2");
      delay(250);
    
     
      // Initialize and present Relays with corresponding buttons
      for (int i = 0; i < noRelays; i++)
      {
        Relays[i].buttonPin = buttonPin[i];              // assign physical pins
        Relays[i].relayPin = relayPin[i];
        msgRelays[i].sensor = i;                                   // initialize messages
        msgRelays[i].type = V_LIGHT;
        debouncer[i] = Bounce();                        // initialize debouncer
        debouncer[i].attach(buttonPin[i]);
        debouncer[i].interval(5);
        pinMode(Relays[i].buttonPin, INPUT_PULLUP);
        pinMode(Relays[i].relayPin, OUTPUT);
        Relays[i].relayState = gw.loadState(i);                               // retrieve last values from EEPROM
        digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
        gw.send(msgRelays[i].set(Relays[i].relayState ? true : false));                 // make controller aware of last status
        gw.present(i, S_LIGHT);                               // present sensor to gateway
        delay(250);
      }
      
     
      gw.present(CHILD_ID_PIR, S_MOTION);
      
     
    }
    
    void loop()
    {
    
      // Deal with incoming messages and when enabled repeat
      gw.process();
    
      
      // Deal with PIR
      // Read digital motion value
      boolean tripped = digitalRead(PIR_SENSOR_DIGITAL_INPUT) == HIGH; 
    
      // Check if PIR Changed
      if (prevPIRstatus |= tripped) {
        Serial.println(tripped);
        gw.send(msgPIR.set(tripped?"1":"0"));  // Send tripped value to gw 
       // prevPIRstatus = tripped; // store new value to PIR status to run update on next loop when changed
      // Sleep until interrupt comes in on motion sensor. Send update every two minute.
      gw.sleep(digitalPinToInterrupt(PIR_SENSOR_DIGITAL_INPUT), CHANGE, SLEEP_TIME);
      }
      
      // Deal with relays and buttons
      for (byte i = 0; i < noRelays; i++)
      {
        debouncer[i].update();
        byte value = debouncer[i].read();
        if (value != Relays[i].oldValue && value == 0)
        {
          Relays[i].relayState = !Relays[i].relayState;
          digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF);
          gw.send(msgRelays[i].set(Relays[i].relayState ? true : false));
          gw.saveState( i, Relays[i].relayState );
        }                 // save sensor state in EEPROM (location == sensor number)
    
        Relays[i].oldValue = value;
      }  
      
      
    }
    
    
    
    // process incoming message
    void incomingMessage(const MyMessage &message)
    {
    
      if (message.type == V_LIGHT)
      {
        if (message.sensor < noRelays)            // check if message is valid for relays..... previous line  [[[ if (message.sensor <=noRelays){ ]]]
        {
          Relays[message.sensor].relayState = message.getBool();
          digitalWrite(Relays[message.sensor].relayPin, Relays[message.sensor].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
          gw.saveState(message.sensor, Relays[message.sensor].relayState); // save sensor state in EEPROM (location == sensor number)
        }
      }
    }
    
    
    

  • Mod

    @Dick the code is using child id 0, 1, 2 and 3 for the relays, and then 1 again for the pir. That will confuse the system. I would change

    #define CHILD_ID_PIR 1
    

    to

    #define CHILD_ID_PIR noRelays  // relays use child id 0...noRelays-1 so let the pir use the next number
    

    and move this definition after #define noRelays.



  • I finaly made it 4 buttons, 4 relay and a pir and here the script for sharing

    #include <MySensor.h>
    #include <SPI.h>
    #include "Bounce2.h"
    #define USE_MOTION_SENSOR
    #define MOTION_SENSOR_PIN    3
    #define RELAY_ON 0                      // switch around for realy HIGH/LOW state
    #define RELAY_OFF 1
    
    Bounce motionsDebouncer = Bounce();
    //
    MySensor gw;
    
    #define RADIO_ID 11                    // radio Id, whatever channel you assigned to
    #define noRelays 4
    const int relayPin[] = {A1, A2, A3, A4}; //  switch around pins to your desire
    const int buttonPin[] = {A5, 6, 7, 8}; //  switch around pins to your desire
    
    class Relay             // relay class, store all relevant data (equivalent to struct)
    {
    public: 
      int buttonPin;                    // physical pin number of button
      int relayPin;                     // physical pin number of relay
      byte oldValue;                    // last Values for key (debounce)
      boolean relayState;               // relay status (also stored in EEPROM)
    };
    
    Relay Relays[noRelays];
    Bounce debouncer[noRelays];
    MyMessage msg[noRelays];
    
    void setup()
    {
      gw.begin(incomingMessage, RADIO_ID, true);
      delay(250);
      gw.sendSketchInfo("Multy-Relay&Pulsanti", "0.2");
      delay(250);
    
      pinMode( MOTION_SENSOR_PIN, INPUT_PULLUP );
      motionsDebouncer.attach(MOTION_SENSOR_PIN);
      motionsDebouncer.interval(50);
    
      // Initialize Relays with corresponding buttons
      for (int i = 0; i < noRelays; i++)
      {
        Relays[i].buttonPin = buttonPin[i];              // assign physical pins
        Relays[i].relayPin = relayPin[i];
        msg[i].sensor = i;                                   // initialize messages
        msg[i].type = V_LIGHT;
        debouncer[i] = Bounce();                        // initialize debouncer
        debouncer[i].attach(buttonPin[i]);
        debouncer[i].interval(5);
        pinMode(Relays[i].buttonPin, INPUT_PULLUP);
        pinMode(Relays[i].relayPin, OUTPUT);
        Relays[i].relayState = gw.loadState(i);                               // retrieve last values from EEPROM
        digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
        gw.send(msg[i].set(Relays[i].relayState ? true : false));                 // make controller aware of last status
        gw.present(i, S_LIGHT);                               // present sensor to gateway
        delay(250);
    
      }
    }
    
    void loop()
    {
      if ( motionsDebouncer.update()) {
        int value = motionsDebouncer.read();
        Serial.println( "Motion sensor is " + (String)value );
      }
    {
      gw.process();
      for (byte i = 0; i < noRelays; i++)
      {
        debouncer[i].update();
        byte value = debouncer[i].read();
        if (value != Relays[i].oldValue && value == 0)
        {
          Relays[i].relayState = !Relays[i].relayState;
          digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF);
          gw.send(msg[i].set(Relays[i].relayState ? true : false));
          gw.saveState( i, Relays[i].relayState );
        }                 // save sensor state in EEPROM (location == sensor number)
    
        Relays[i].oldValue = value;
      }
      }
    }
    
    // process incoming message
    void incomingMessage(const MyMessage &message)
    {
    
      if (message.type == V_LIGHT)
      {
        if (message.sensor < noRelays)            // check if message is valid for relays..... previous line  [[[ if (message.sensor <=noRelays){ ]]]
        {
          Relays[message.sensor].relayState = message.getBool();
          digitalWrite(Relays[message.sensor].relayPin, Relays[message.sensor].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
          gw.saveState( message.sensor, Relays[message.sensor].relayState ); // save sensor state in EEPROM (location == sensor number)
        }
      }
      delay( 50 ); // give the input pins some rest. Incomming messages are still being processed.
    }
    

  • Mod

    Great work @Dick !
    You might want to change

    delay( 50 ); // give the input pins some rest. Incomming messages are still being processed.
    

    to

    gw.wait( 50 ); // give the input pins some rest. Incoming messages are still being processed.
    

    Delay will prevent incoming messages from being processed while gw.wait handles incoming messages.



  • thanks for this change. I read about it. I change it right away.


Log in to reply
 

Suggested Topics

  • 4
  • 17
  • 15
  • 8
  • 20
  • 4

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts