Hi guys.
I have the same connection problem, identical to you. What changes is that I use the Arduino Mega with an ENC28j60 ethernet board. I actually use two Arduino Mega boards each with the ENC28j60 ethernet. The two have practically the same Scheduler, carrying one of them after a few touches on the switches or using the HA for some command disconnects, the other card is stable and working perfectly. I have already changed the plates, I have rewritten the codes, I have left only one connection on the network, I have done the cabling inspection, replaced the number of IPs and there is no way to be revolved. Follow my schedule, if anyone can help me. And I use a different name for each board.
#include <PubSubClient.h>
#include <UIPEthernet.h>
byte mac[] = {0x80, 0x7D, 0x3A, 0x69, 0x20, 0xC8 }; //physical mac address
byte ip[] = {192, 168, 0, 202 }; // ip in lan
int botao[] = { 11, 12, 10, 9, 8, 7, 6, 5, 4, 3, 27, 14, 17, 16, 15, 18, 22, 21, 20, 19, 23, 26, 25, 24 };//PORTAS DOS INTERRUPTORES
int rele[] = { A11, A10, A12, A13, A14, A15, A6, A5, A4, A3, 49, 48, 45, 46, 47, 44, 40, 41, 42, 43, 39, 36, 37, 38 }; //PORTAS DOS RELES
const char* TOPIC_PUBLISH [] = {"ha/v_principal","ha/v_cabeceira", "ha/v_acessoria", "ha/c_principal", "ha/c_acessoria", "ha/c_parede", "ha/q_principal", "ha/q_parede", "ha/q_entrada", "ha/q_esquerda", "ha/q_direita", "ha/q_arandela", "ha/m_principal", "ha/m_acessoria", "ha/m_parede", "ha/m_sacada", "ha/lustre", "ha/jantar_indireta", "ha/s_indireta", "ha/mezanino", "ha/v_sacada", "ha/porta_arandela", "ha/calcada", "ha/j_frente" };
String strTopic []= {"ha/v_principal","ha/v_cabeceira", "ha/v_acessoria", "ha/c_principal", "ha/c_acessoria", "ha/c_parede", "ha/q_principal", "ha/q_parede", "ha/q_entrada", "ha/q_esquerda", "ha/q_direita", "ha/q_arandela", "ha/m_principal", "ha/m_acessoria", "ha/m_parede", "ha/m_sacada", "ha/lustre", "ha/jantar_indireta", "ha/s_indireta", "ha/mezanino", "ha/v_sacada", "ha/porta_arandela", "ha/calcada", "ha/j_frente" };
String Switch[] = { "v_principal" , "v_cabeceira" , "v_acessoria" , "c_principal" , "c_acessoria" , "c_parede" , "q_principal" , "q_parede" , "q_entrada" , "q_esquerda" , "q_direita" , "q_arandela" , "m_principal" , "m_acessoria" , "m_parede" , "m_sacada" , "lustre" , "j_indireta" , "s_indireta" , "mezanino" , "v_sacada" , "porta_arandela" , "calcada" , "j_frente" };
boolean statusrele[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, };
static unsigned long debounceInterruptor[]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, };
int relecont = 24; // NR DE PORTAS USADAS RELES OU INTERRUPETORES PARA O INDICE (i)
int i;
const char* mqtt_server = "192.168.xxx.xxx";
const char* mqttUser = "amkoxxxxxx";
const char* mqttPassword = "xxxxxxxxx";
EthernetClient espClient;
PubSubClient client(espClient);
String strTopic1;
String strPayload;
void interruptor(int i) // FUNCAO PARA LIGAR/DESLIGAR LAMPADAS
{
for (int i = 0; i < relecont; i++){
if ( (millis() - debounceInterruptor[i]) > 30 ) {
if((digitalRead(botao[i]) == HIGH))
{
while(digitalRead(botao[i]) == HIGH){};
digitalWrite(rele[i],!statusrele[i]);
statusrele[i] = !statusrele[i];
if (statusrele[i]==false){
client.publish(TOPIC_PUBLISH[i], "OFF");}
if (statusrele[i]==true){
client.publish(TOPIC_PUBLISH[i], "ON");}
delay(350);
}
}
}
}
void setup_inicio() {
Serial.begin(115200);
delay(100);
Ethernet.begin(mac,ip);
}
void callback(char* topic, byte* payload, unsigned int length) {
payload[length] = '\0';
strTopic1 = String((char*)topic);
for (int i = 0; i < relecont; i++)
{
if(strTopic1 == strTopic[i])
{
Switch[i] = String((char*)payload);
if(Switch[i] == "ON")
{statusrele[i] = true;
Serial.println("ON");
digitalWrite(rele[i], HIGH);
delay(350);
}
else
{statusrele[i] = false;
Serial.println("OFF");
digitalWrite(rele[i], LOW);
delay(350);
}
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("arduinoClientSuperior",mqttUser, mqttPassword)) {
Serial.println("connected");
// Once connected, publish an announcement...
client.subscribe("ha/#");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup()
{
Serial.begin(115200);
for (int i = 0; i < relecont; i++)
{
pinMode(rele[i],OUTPUT); //DEFINI PORTAS RELE COMO PORTA DE SAIDA
pinMode(botao[i],INPUT); //DEFINE PORTAS DE INTERRUPTORES COMO PORTA DE ENTRADA
digitalWrite(rele[i],HIGH); // DEFINIE ESTADO HIGH PARA PORTAS DE RELE
statusrele[i] = true; // DEFINE O STATUS RELE COMO TRUE
}
setup_inicio();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop()
{
if (!client.connected()) {
reconnect();
}
interruptor(i);
client.loop();
}