Problem with sketch 8ch relay 4 buttons 1 motion sensor



  • I am trying to create a sketch to handle 8ch relay, 4 buttons, 3 motion sensors, 3 temp sensors and a dimmer.
    So far i have 8ch relay, 4 buttons, 1 motion sensor running. The rest is added when i got this stable.
    Relay i attached as done in the Irrigation controller sensor
    The 4 buttons are meant to toggle the first 4 relays.

    All seems to work fine for some hours, then the sketch stops accepting incoming messages.
    The buttons works just fine.
    I have tried to put in some prints to serial to see where it stops, but it does not help me

    The second problem is that it acts as a repeater node, but i have not put the "#define MY_REPEATER_NODE" in.
    I would like it to stop act as repeater.

    the code:

    #include <MySensor.h>
    #include <SPI.h>
    #include "Bounce2.h"
    
    #define MY_DEBUG
    
    #define USE_MOTION_SENSOR
    #define MOTION_SENSOR_PIN    19
    #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 8
    const int relayPin[] = {1, 2, 3, 4, 5, 6, 7, 8}; //  switch around pins to your desire
    const int buttonPin[] = {A0, A1, A2, A3}; //  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)
    };
    
    //#define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #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
    
    ////Pin connected to Data in (DS) of 74HC595
    const int dataPin = 6;
    //Pin connected to latch pin (ST_CP) of 74HC595
    const int latchPin = 7;
    //Pin connected to clock pin (SH_CP) of 74HC595
    const int clockPin = 8;
    
    #define NUMBER_OF_RELAYS 8  // Change this to set your valve count up to 16.
    #define NUMBER_OF_BUTTONS 4  // Change this to set your valve count up to 16.
    int ACTIVE_LOW=0; 
    int SendToRelay;
    
    int SumStatus = 0;
                    
    Relay Relays[NUMBER_OF_RELAYS];
    Bounce debouncer[NUMBER_OF_RELAYS];
    MyMessage msg[NUMBER_OF_RELAYS];
    
    void setup()
    {
      gw.begin(incomingMessage, RADIO_ID, true);
      delay(250);
      gw.sendSketchInfo("LivingRoom", "0.7");
      delay(250);
    
      pinMode( MOTION_SENSOR_PIN, INPUT_PULLUP );
      motionsDebouncer.attach(MOTION_SENSOR_PIN);
      motionsDebouncer.interval(50);
      gw.present(21, S_MOTION);    // present sensor to gateway
      
      // Initialize Relays with corresponding buttons
      for (int i = 0; i < NUMBER_OF_RELAYS; i++)
      {
        if (i < NUMBER_OF_BUTTONS) {
          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
        Serial.println( "Setup relay " + (String)i );
        SwitchRelays(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF);
        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 < NUMBER_OF_RELAYS; 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);
          Serial.println( "SwitchRelays, i " + (String)i );
          Serial.println( "Relays[i].relayPin " + (String)Relays[i].relayPin );
          SwitchRelays(Relays[i].relayPin, 2);
          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;
      }
    }
    
    void SwitchRelays(int whichPin, int whichState) {
    
      switch(whichState) { 
        case 0:
      //    relaystatus[whichPin] = 0;
          Serial.print("Turn Off Relay ");
          Serial.println(whichPin);
          break;
        case 1:
      //    relaystatus[whichPin] = 1;
          Serial.print("Turn On Relay ");
          Serial.println(whichPin);
          break;
        default:
          Serial.print("Toggle Relay ");
          Serial.println(whichPin);
          break;
      }      
      
     
      SumStatus = (Relays[0].relayState*1) +
                  (Relays[1].relayState*2) +
                  (Relays[2].relayState*4) +
                  (Relays[3].relayState*8) +
                  (Relays[4].relayState*16) +
                  (Relays[5].relayState*32) +
                  (Relays[6].relayState*64) +
                  (Relays[7].relayState*128);
                    
      Serial.print("ACTIVE_LOW = ");
      Serial.print(ACTIVE_LOW);
      Serial.print(" -   SumStatus ");
      Serial.print(SumStatus);
     
      if (ACTIVE_LOW==1) {
        SendToRelay=SumStatus;
        }
      else {
        SendToRelay=255-SumStatus;
        }
      Serial.print(" -- SendToRelay ");
      Serial.println(SendToRelay);
      // turn off the output so the pins don't light up
      // while you're shifting bits:
      digitalWrite(latchPin, LOW);
    
    
      // shift the bits out:
      shiftOut(dataPin, clockPin, MSBFIRST, SendToRelay);
    
        // turn on the output so the LEDs can light up:
      digitalWrite(latchPin, HIGH);
      //delay(500);
    }
    
    // process incoming message
    void incomingMessage(const MyMessage &message)
    {
      Serial.println("Incoming message");
      if (message.type == V_LIGHT)
      {
        if (message.sensor < NUMBER_OF_RELAYS)            // check if message is valid for relays..... previous line  [[[ if (message.sensor <=NUMBER_OF_RELAYS){ ]]]
        {
          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)
          Serial.print("Incoming change for sensor:");
          Serial.print(message.sensor);
          Serial.print(", New status: ");
          Serial.println(message.getBool());
          SwitchRelays(Relays[message.sensor].relayPin, Relays[message.sensor].relayState ? RELAY_ON : RELAY_OFF);
        }
      }
      gw.wait( 250 ); // give the input pins some rest. Incomming messages are still being processed.
    }
    

    Any suggestions to what i can improve to solve it is most appreciated.

    Thanks,

    Rene



  • Looks like I have fixed it myself
    This sensor-node was very close to the gw-node.
    Moved it further away and problem seems to be gone :smiley:


  • Mod

    Great work @pryning, thanks for reporting back. Hopefully it can help someone else with a similar problem in the future.


Log in to reply
 

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