MultiRelayExpanderWithToggleSwitch unstable



  • Hello,
    Im working on GW with directly attached sensors for Relay array using PCF8574 ( i need about 24 relays & buttons ) using MySensors development branch
    I know that code works but sometimes gateway does not respond to domoticz at startup ( or even serial monitor ) and sometimes it breaks after few minutes of working..

    faulty arduino ? dont have any spare one to test 😞
    Or maybe there is a code problem ?

    check it out:

    //#define MY_RADIO_NRF24
    #define MY_GATEWAY_SERIAL
    //#define MY_REPEATER_FEATURE
    
    #include <MySensor.h>
    #include <SPI.h>
    #include <Wire.h>
    #include <PCF8574.h>
    
    #define RELAY_ON 0                     
    #define RELAY_OFF 1
    #define noExpanders 2
    
    class Relay             
    {
    public:                                      
      int buttonPin;                
      int relayPin;      
      boolean relayState;
      boolean buttonState = 1;
    };
    PCF8574 relays_exp[noExpanders];
    PCF8574 buttons_exp[noExpanders];
    Relay Relays[noExpanders][8]; 
    
    MyMessage msg;
    
    void setup(){
    relays_exp[0].begin(0x38);
    relays_exp[1].begin(0x39);
    buttons_exp[0].begin(0x3B);
    buttons_exp[1].begin(0x3F);
    for ( int i = 0 ; i < noExpanders ; i++ )
    {
    
      for ( int j = 0 ; j < 8 ; j++ )
      {
        Relays[i][j].buttonPin = j;
        Relays[i][j].relayPin = j;
        Relays[i][j].relayState = loadState(8*i+j);  
        buttons_exp[i].pinMode(j, INPUT_PULLUP);
        wait(50);
        relays_exp[i].pinMode(j, OUTPUT);
        relays_exp[i].digitalWrite(Relays[i][j].relayPin, Relays[i][j].relayState? RELAY_ON:RELAY_OFF);
        msg.setSensor(8*i+j); 
        msg.setType(V_LIGHT); 
        send(msg.set(Relays[i][j].relayState? true : false)); 
        wait(50);
    }
    }
    }
    
    void presentation()  {
    sendSketchInfo("MultiRelayExpanderWithToggleSwitch", "0.9b");
    wait(100);
    
    for ( int i = 0 ; i < noExpanders ; i++ )
    {
      for ( int j = 0 ; j < 8 ; j++ )
      {
        present(8*i+j, S_LIGHT);
      }
    }
    }
    
    void loop()
    {
    for ( int i = 0 ; i < noExpanders ; i++ )
    {
      for ( int j = 0 ; j < 8 ; j++ )
      {  
            if (buttons_exp[i].digitalRead(j) != Relays[i][j].buttonState) 
            {
                Relays[i][j].relayState = !Relays[i][j].relayState;
                Relays[i][j].buttonState = !Relays[i][j].buttonState;
                relays_exp[i].digitalWrite(Relays[i][j].relayPin, Relays[i][j].relayState?RELAY_ON:RELAY_OFF);
                msg.setSensor(8*i+j);
                send(msg.set(Relays[i][j].relayState? true : false));
                saveState(8*i+j, Relays[i][j].relayState );
            }                 
        
      }
    }
        wait(50);
    }
    void receive(const MyMessage &message){        
       if (message.type == V_LIGHT)
       { 
         if (message.sensor < noExpanders * 8)
         {
           if (message.sensor < 8)
           {
             Relays[0][message.sensor].relayState = message.getBool(); 
             relays_exp[0].digitalWrite(Relays[0][message.sensor].relayPin, Relays[0][message.sensor].relayState? RELAY_ON:RELAY_OFF);
             saveState( message.sensor, Relays[0][message.sensor].relayState ); 
           }
           if (message.sensor >= 8)
           {
             Relays[1][message.sensor -8].relayState = message.getBool(); 
             relays_exp[1].digitalWrite(Relays[0][message.sensor -8].relayPin, Relays[1][message.sensor -8].relayState? RELAY_ON:RELAY_OFF);
             saveState( message.sensor, Relays[1][message.sensor -8].relayState );
           }
        }
       }
      wait(20);
    }
    
    


  • Everything works now 🙂
    below is corrected code:

    //"MultiRelayExpanderWithToggleSwitch" for MySensors 2.0
    //Author : Mateusz Pieta
    //email: m.pieta@outlook.com
    //
    //Sketch to use PCF8574 expanders for Relay Arrays with Button Actuators
    //
    //
    
    //#define MY_RADIO_NRF24
    //#define MY_REPEATER_FEATURE
    #define MY_GATEWAY_SERIAL
    
    #include <MySensor.h>
    #include <SPI.h>
    #include <Wire.h>
    #include <PCF8574.h>
    
    #define RELAY_ON 1                     
    #define RELAY_OFF 0
    #define noExpanders 2
    
    class Relay         
    {
    public:                                      
      int buttonPin;                
      int relayPin;      
      boolean relayState;
      boolean buttonState = 1;
    };
    
    PCF8574 relays_exp[noExpanders];
    PCF8574 buttons_exp[noExpanders];
    Relay Relays[noExpanders][8]; 
    
    MyMessage msg;
    
    void setup(){
    relays_exp[0].begin(0x38);
    relays_exp[1].begin(0x39);
    buttons_exp[0].begin(0x3B);
    buttons_exp[1].begin(0x3F);
    
    for ( int i = 0 ; i < noExpanders ; i++ )
    {
    
      for ( int j = 0 ; j < 8 ; j++ )
      {
        Relays[i][j].buttonPin = j;
        Relays[i][j].relayPin = j;
        Relays[i][j].relayState = loadState(8*i+j);  
        buttons_exp[i].pinMode(j, INPUT_PULLUP);
        wait(100);
        relays_exp[i].pinMode(j, OUTPUT);
        relays_exp[i].digitalWrite(Relays[i][j].relayPin, Relays[i][j].relayState? RELAY_ON:RELAY_OFF);
        msg.setSensor(8*i+j); 
        msg.setType(V_LIGHT); 
        send(msg.set(Relays[i][j].relayState? true : false)); 
        wait(50);
    }
    }
    }
    
    void presentation()  {
    sendSketchInfo("MultiRelayExpanderWithToggleSwitch", "0.9b");
    wait(100);
    
    for ( int i = 0 ; i < noExpanders ; i++ )
    {
      for ( int j = 0 ; j < 8 ; j++ )
      {
        present(8*i+j, S_LIGHT);
      }
    }
    }
    
    void loop()
    {
    for ( int i = 0 ; i < noExpanders ; i++ )
    {
      for ( int j = 0 ; j < 8 ; j++ )
      {  
            if (buttons_exp[i].digitalRead(j) != Relays[i][j].buttonState) 
            {
                Relays[i][j].relayState = !Relays[i][j].relayState;
                Relays[i][j].buttonState = !Relays[i][j].buttonState;
                relays_exp[i].digitalWrite(Relays[i][j].relayPin, Relays[i][j].relayState?RELAY_ON:RELAY_OFF);
                msg.setSensor(8*i+j);
                send(msg.set(Relays[i][j].relayState? true : false));
                saveState(8*i+j, Relays[i][j].relayState );
            }                 
        
      }
    }
        wait(50);
    }
    void receive(const MyMessage &message){        
       if (message.type == V_LIGHT)
       { 
         if (message.sensor < noExpanders * 8)
         {
           if (message.sensor < 8)
           {
             Relays[0][message.sensor].relayState = message.getBool(); 
             relays_exp[0].digitalWrite(Relays[0][message.sensor].relayPin, Relays[0][message.sensor].relayState? RELAY_ON:RELAY_OFF);
             saveState( message.sensor, Relays[0][message.sensor].relayState ); 
           }
           if (message.sensor >= 8)
           {
             Relays[1][message.sensor -8].relayState = message.getBool(); 
             relays_exp[1].digitalWrite(Relays[1][message.sensor -8].relayPin, Relays[1][message.sensor -8].relayState? RELAY_ON:RELAY_OFF);
             saveState( message.sensor, Relays[1][message.sensor -8].relayState );
           }
        }
       }
      wait(20);
    }
    
    
    


  • Hello
    I wanted to understand are you using 2 PCF8574? one for input and another for output?
    also in the code which entries are to add the relays and assign a specific button?
    the code if inserted on wemos d1 works?
    sorry for the many questions because I have to put 8 relays controlled with 8 buttons and interface with domoticz

    thank you


Log in to reply
 

Suggested Topics

  • 1
  • 1
  • 5
  • 2
  • 3
  • 2

24
Online

11.4k
Users

11.1k
Topics

112.7k
Posts