Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. MultiRelayExpanderWithToggleSwitch unstable

MultiRelayExpanderWithToggleSwitch unstable

Scheduled Pinned Locked Moved Development
3 Posts 2 Posters 1.4k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Mateusz PiętaM Offline
    Mateusz PiętaM Offline
    Mateusz Pięta
    wrote on last edited by Mateusz Pięta
    #1

    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);
    }
    
    
    1 Reply Last reply
    0
    • Mateusz PiętaM Offline
      Mateusz PiętaM Offline
      Mateusz Pięta
      wrote on last edited by
      #2

      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);
      }
      
      
      
      1 Reply Last reply
      0
      • A Offline
        A Offline
        Ausonius
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0

        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

        With your input, this post could be even better 💗

        Register Login
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        18

        Online

        12.0k

        Users

        11.2k

        Topics

        113.4k

        Posts


        Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • MySensors
        • OpenHardware.io
        • Categories
        • Recent
        • Tags
        • Popular