As I said I managed to communicate the arduino mega with HA via wifi, LAN and USB, via wifi and LAN is the best way to work, in my opinion, because we can better use the functionality of the cards. As the photos posted I have two mega arudino boards, with practically all the pins used, and it is working harmoniously with HA. Below is the arduino scheduler
type or paste code here
#include <PubSubClient.h>
#include <UIPEthernet.h>
byte mac[] = {0x80, 0x7D, 0x3A, 0x69, 0xxx, 0xxx }; //physical mac address
byte ip[] = {192, 168, xxx, xxx }; // ip in lan
int botao[] = { 13, 12, 11, 9, 8, 10, 6, 5, 7, 4, 3, 29, 15, 14, 16, 17, 18, 19, 20, 23, 21, 22, 28, 27, 26, 25, 24};//PORTAS DOS INTERRUPTORES
int rele[] = { A12, A11, A10, A15, A14, A13, A8, A9, A7, 49, 48, 30, 46, 47, 43, 44, 45, 41, 42, 38, 39, 40, 33, 34, 35, 36, 37}; //PORTAS DOS RELE
const char* TOPIC_PUBLISH [] = {"ha/C_Cozinha","ha/B_Cozinha", "ha/P_Cozinha", "ha/escritorio", "ha/portao", "ha/computador", "ha/box_suite", "ha/espelho", "ha/banheira", "ha/garagem", "ha/fit_garagem", "ha/muro_cozinha", "ha/mesa_jantar", "ha/entrada", "ha/academia", "ha/academia_111", "ha/c_academia", "ha/pia_churras", "ha/b_churras", "ha/lavabo", "ha/mesa_churras", "ha/porta_churras", "ha/t_lava", "ha/lava", "ha/muro", "ha/j_lateral", "ha/pergolado" };
String strTopic []= {"ha/C_Cozinha","ha/B_Cozinha", "ha/P_Cozinha", "ha/escritorio", "ha/portao", "ha/computador", "ha/box_suite", "ha/espelho", "ha/banheira", "ha/garagem", "ha/fit_garagem", "ha/muro_cozinha", "ha/mesa_jantar", "ha/entrada", "ha/academia", "ha/academia_111", "ha/c_academia", "ha/pia_churras", "ha/b_churras", "ha/lavabo", "ha/mesa_churras", "ha/porta_churras", "ha/t_lava", "ha/lava", "ha/muro", "ha/j_lateral", "ha/pergolado" };
String Switch[] = { "C_Cozinha" , "B_Cozinha" , "P_Cozinha" , "escritorio", "portao" , "computador" , "box_suite" , "espelho" , "banheira" , "garagem" , "fit_garagem" , "muro_cozinha" , "mesa_jantar" , "entrada" , "academia" , "academia_111" , "c_academia" , "pia_churras" , "b_churras" , "lavabo" , "mesa_churras" , "porta_churras" , "t_lava" , "lava" , "muro" , "j_lateral" , "pergolado" };
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, 25, 26, 27 };
uint8_t AnalogicaTimer [] = { A5, A5, A5, A3, A6, A3, 0, 0, 0, A4, A4, A5, A5, 0, 0, 0, 0, A3, A3, 0, A3, A3, A2, A2, 0, 0, 0 };
int relecont = 27;
int i;
const char* mqtt_server = "192.168.xxx.xxx";
const char* mqttUser = "xxxxxxx";
const char* mqttPassword = "xxxxxxx";
const char* TOPIC_GARAGE;
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((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() {
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
}
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);
}
else
{statusrele[i] = false;
Serial.println("OFF");
digitalWrite(rele[i], LOW);
}
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("arduinoClient",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()
{
setup_inicio();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop()
{
if (!client.connected()) {
reconnect();
}
interruptor(i);
client.loop();
if ((analogRead(A4)>200)){
client.publish("TOPIC_GARAGE","ON");}
}