<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[MultiRelayExpanderWithToggleSwitch unstable]]></title><description><![CDATA[<p dir="auto">Hello,<br />
Im working on GW with directly attached sensors for Relay array using PCF8574 ( i need about 24 relays &amp; buttons ) using MySensors development branch<br />
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..</p>
<p dir="auto">faulty arduino ? dont have any spare one to test :(<br />
Or maybe there is a code problem ?</p>
<p dir="auto">check it out:</p>
<pre><code>//#define MY_RADIO_NRF24
#define MY_GATEWAY_SERIAL
//#define MY_REPEATER_FEATURE

#include &lt;MySensor.h&gt;
#include &lt;SPI.h&gt;
#include &lt;Wire.h&gt;
#include &lt;PCF8574.h&gt;

#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 &lt; noExpanders ; i++ )
{

  for ( int j = 0 ; j &lt; 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 &lt; noExpanders ; i++ )
{
  for ( int j = 0 ; j &lt; 8 ; j++ )
  {
    present(8*i+j, S_LIGHT);
  }
}
}

void loop()
{
for ( int i = 0 ; i &lt; noExpanders ; i++ )
{
  for ( int j = 0 ; j &lt; 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 &amp;message){        
   if (message.type == V_LIGHT)
   { 
     if (message.sensor &lt; noExpanders * 8)
     {
       if (message.sensor &lt; 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 &gt;= 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);
}

</code></pre>
]]></description><link>https://forum.mysensors.org/topic/4142/multirelayexpanderwithtoggleswitch-unstable</link><generator>RSS for Node</generator><lastBuildDate>Thu, 15 Oct 2026 01:51:22 GMT</lastBuildDate><atom:link href="https://forum.mysensors.org/topic/4142.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 23 Jun 2016 09:51:12 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to MultiRelayExpanderWithToggleSwitch unstable on Sun, 01 Jul 2018 12:00:39 GMT]]></title><description><![CDATA[<p dir="auto">Hello<br />
I wanted to understand are you using 2 PCF8574? one for input and another for output?<br />
also in the code which entries are to add the relays and assign a specific button?<br />
the code if inserted on wemos d1 works?<br />
sorry for the many questions because I have to put 8 relays controlled with 8 buttons and interface with domoticz</p>
<p dir="auto">thank you</p>
]]></description><link>https://forum.mysensors.org/post/91292</link><guid isPermaLink="true">https://forum.mysensors.org/post/91292</guid><dc:creator><![CDATA[Ausonius]]></dc:creator><pubDate>Sun, 01 Jul 2018 12:00:39 GMT</pubDate></item><item><title><![CDATA[Reply to MultiRelayExpanderWithToggleSwitch unstable on Thu, 23 Jun 2016 14:32:12 GMT]]></title><description><![CDATA[<p dir="auto">Everything works now :)<br />
below is corrected code:</p>
<pre><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 &lt;MySensor.h&gt;
#include &lt;SPI.h&gt;
#include &lt;Wire.h&gt;
#include &lt;PCF8574.h&gt;

#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 &lt; noExpanders ; i++ )
{

  for ( int j = 0 ; j &lt; 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 &lt; noExpanders ; i++ )
{
  for ( int j = 0 ; j &lt; 8 ; j++ )
  {
    present(8*i+j, S_LIGHT);
  }
}
}

void loop()
{
for ( int i = 0 ; i &lt; noExpanders ; i++ )
{
  for ( int j = 0 ; j &lt; 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 &amp;message){        
   if (message.type == V_LIGHT)
   { 
     if (message.sensor &lt; noExpanders * 8)
     {
       if (message.sensor &lt; 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 &gt;= 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);
}


</code></pre>
]]></description><link>https://forum.mysensors.org/post/41599</link><guid isPermaLink="true">https://forum.mysensors.org/post/41599</guid><dc:creator><![CDATA[Mateusz Pięta]]></dc:creator><pubDate>Thu, 23 Jun 2016 14:32:12 GMT</pubDate></item></channel></rss>