Using an Arduino via LAN

Or mqtt.

I use mqtt as my link to hass from Arduino etc

Yep, same here MQTT.

Hi, I need to connect arduino one with ethernet shield to home assistant too. Could you kindly post the sketch with the configuration in the home assistant?

Hi, were you able to get a way to check arduino for an ethernet with HA? I’d be interested. Could you explain how to do it? Possibly an example of the sketch?

Hello Evebybody

I have an automation project with two Mega Arduinos and over 70 relays, all wired. I am implementing the Home Assistant and I am having difficulty making the communication between the two. The two arduinos are connected to my network via LAN. Could someone help me how to do this case. I’m having a hard time and that would be very important for my project. If anyone can help me with a simple project on how to change the status of any Arduino pin from 0 to 1 via the Home Assistant it would be of great help
tks

Hello

I need a lot of help. I have a home automation project with two mega Arduinos, practically all the pins are being used. The entire system is wired. I also use Broadlink and Google Home. I’m deploying the Home Assistant that worked on everything, tv, cameras, tv flap, etc. As for the automation of the lampsis my problem. I made the USB communication accessed ok, I created a switch that worked via HA with the FirmataStandard installed on the Arduino, however in this case the Arduino cannot manage the switches in the house, that is, the switches send the pulses to the Arduino and as the port it commands the what you should do (if you light a lamp). With the firmata the Arduino is a mere coadjuvate in the system, in this case it could only light the lamps by the application or by voice command, the house switches do not work. Another way would be to connect the arduinos via LAN both are on the network I can ping but I can’t make the thing work with HA. The Arduino is connected to the network by this Enc28j60 Mini Ethernet Network Card. Could someone help me in this regard how to do or by USB (half is working) or by LAN?

Guys,
I managed to do HA communications with the arduino, both via usb, wifi or LAN, I confess that I am using LAN on my two mega arduino boards. The programming of the arduino I did in matrix due to the conditionals it has and is “talking” very well with the HA via MQTT, the state of the lamps and sensors is working well. I am now fumbling with automation. Could someone help me with this? What I need at the moment would be the following: The lamps outside the house I wish they could only be switched on or off at night. I am unable to do this.

Hi, I’m interested in the project, I can take a look. I would like to understand how to turn on 8 mqtt relays with arduino uno and ethernet. can you help me?

Of course, follow my code. I made it in matrix form because it is simpler and more functional. You can use this same code so replace the numbers of ports of switches and relays.

#include <PubSubClient.h>
#include <UIPEthernet.h>

byte mac[] = {0x80, 0x7D, 0x3A, 0x69, 0x20, 0xC8 }; //physical mac address
byte ip[] = {192, 168, xxx, xxx }; // 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 = "mqttUser";
const char* mqttPassword = "xxxxx";

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();


      
}
1 Like

This automation does the opposite of what you ask (turns the bell off at night and turns it on in the morning). You could try to adapt it change the entities.

# Turns off the doorbell chime after 8pm.
# Turns it on again in the morning @ 7am.
#
automation:
  - alias: "Doorbell off after 8pm"
    trigger:
      platform: time
      at: "20:00:00"
    action:
      service: switch.turn_off
      entity_id: switch.doorbell_chime_active

  - alias: "Doorbell on after 7am"
    trigger:
      platform: time
      at: "07:00:00"
    action:
      service: switch.turn_on
      entity_id: switch.doorbell_chime_active

Hi, it would seem that the sketch you posted is for arduino Mega. Can you adapt it for arduino uno (to drive only output relays)?

for an uno with ehernet shield will be great

i,can any one help me ! i have an arduino with ethernet shield with 4 relays,but i can´t detect on home assistant!!!how can i do it?

We might need a bit more detail!

Do you have a sketch loaded on the “arduino”?

How are you expecting Home assistant to see the arduino? MQTT, aRest, something else? (the sketch should help us understand)

Can you share the sketch you used?

Can you troubleshoot via serial connection?

Is the arduino connecting to your network?

Does anyone know about if the following hardware HHC-N8I8OP (cheap 8 relays with lan board) is compute by arduino ?
If it is the case I found this link that could possibly help with Mqqt.


If anyone could try, please report here

hello,i have this sketch on my arduino uno:

//zoomkat 3-17-12
//simple button GET server code to control servo and arduino pin 5
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html
//address will look like http://xxx.xxx.x.xx:xxxx when submited
//for use with W5100 based ethernet shields

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

// Configuração do acesso ao Broker MQTT
#define MQTT_AUTH false
#define MQTT_USERNAME “nxxxxxxxxxx”
#define MQTT_PASSWORD “2xxxxxxxxxxxxxa”
#define RELAY D2

//#include <Servo.h>
//Servo myservo; // create servo object to control a servo

byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = {
192, 168, 1, 151 }; // ip in lan
byte MQTT_SERVER[] = { 192, 168, 1, 235 };
byte gateway[] = {
192, 168, 1, 1 }; // internet access via router
byte subnet[] = {
255, 255, 255, 0 }; //subnet mask
EthernetServer server(8761); //server port

String readString;

EthernetClient ethClient;
PubSubClient client(ethClient);

//////////////////////

void setup(){

pinMode(4, OUTPUT); //pin selected to control
pinMode(5, OUTPUT); //pin selected to control
pinMode(6, OUTPUT); //pin selected to control
pinMode(7, OUTPUT); //pin selected to control
//start Ethernet
Ethernet.begin(mac, ip, gateway, gateway, subnet);
server.begin();

//enable serial data print
Serial.begin(9600);
Serial.println(“server LED test 1.0”); // so I can keep track of what is loaded
}

void loop(){
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();

   //read char by char HTTP request
   if (readString.length() < 100) {

     //store characters to string
     readString += c;
     //Serial.print(c);
   }

   //if HTTP request has ended
   if (c == '\n') {

     ///////////////
     Serial.println(readString); //print to serial monitor for debuging

       client.println("HTTP/1.1 200 OK"); //send new page
     client.println("Content-Type: text/html");
     client.println();

     client.println("<HTML>");
     client.println("<HEAD>");
     client.println("<TITLE>Arduino GET test page</TITLE>");
     client.println("</HEAD>");
     client.println("<BODY>");

     client.println("<H1>NEOS HOME</H1>");

     
     
     // DIY buttons
    
     // mousedown buttons
     client.println("<br><input type=\"button1\" value=\"ON\" onmousedown=\"location.href = '/?off0';\"/>");
        client.println("<input type=\"button1\" value=\"OFF\" onmousedown=\"location.href = '/?on0';\"/>");        
     client.println("<br><input type=\"button2\" value=\"ON\" onmousedown=\"location.href = '/?off1';\"/>");
        client.println("<input type=\"button2\" value=\"OFF\" onmousedown=\"location.href = '/?on1';\"/>"); 
     client.println("<br><input type=\"button3\" value=\"ON\" onmousedown=\"location.href = '/?off2';\"/>");
        client.println("<input type=\"button3\" value=\"OFF\" onmousedown=\"location.href = '/?on2';\"/>"); 
     client.println("<br><input type=\"button4\" value=\"ON\" onmousedown=\"location.href = '/?off3';\"/>");
        client.println("<input type=\"button4\" value=\"OFF\" onmousedown=\"location.href = '/?on3';\"/>");  
 
     client.println("</BODY>");
     client.println("</HTML>");

     delay(1);
     //stopping client
     client.stop();

     ///////////////////// control arduino pin
     if(readString.indexOf("on0") >0) {
       digitalWrite(4, HIGH);
       Serial.println("Led on0");
     }
     
     if(readString.indexOf("off0") >0) {
       digitalWrite(4, LOW);
       Serial.println("Led off0");
     }

     
     if(readString.indexOf("on1") >0) {
       digitalWrite(5, HIGH);
       Serial.println("Led on1");
     }
     if(readString.indexOf("off1") >0) {
       digitalWrite(5, LOW);
       Serial.println("Led off1");
     }

     if(readString.indexOf("on2") >0) {
       digitalWrite(6, HIGH);
       Serial.println("Led on2");
     }
     if(readString.indexOf("off2") >0) {
       digitalWrite(6, LOW);
       Serial.println("Led off2");
     }

     if(readString.indexOf("on3") >0) {
       digitalWrite(7, HIGH);
       Serial.println("Led on3");
     }
     if(readString.indexOf("off3") >0) {
       digitalWrite(7, LOW);
       Serial.println("Led off3");
     }
     //clearing string for next read
     readString="";          
   }
 }

}
}
}

i have this arduino on my home lan,with a ethernet shield, and i can connect to him throug the internet any where i want…but i want to add this arduino on my home assistant!!!
i think the mqtt is the way!? i have read similar projects but they use esp…devices!!!no ethernet shields!? is there code to add mqtt to my sketch???i try to same things on the code but i don´t really understand nothing of this!!

1 Like

use follow this project

Hi, I’m trying your sketch with my arduino mega and ethernet board…
In arduino serial monitor I see “MQTT CONNECTED”… but how can I see the switch in HA?

Many thanks

Can you write an MQTT code example? How do we do the open-close operations with a code?

Witam
Mam kilka czujników ściągniętych w jedno miejsce gdzie kiedyś działały pod Arduino. Teraz chce to przywrócić i podłączyć pod HA. I tu występuje szereg problemów:

  • HA i Arduino są w dwóch różnych pomieszczeniach więc nie ma możliwości po USB
  • mam arduino i Ethernet W5100 i nie potrafię zrobić by się te urządzenia widziały
  • nie jestem obryty w pisaniu kodów, pobieram wiedzę po Internetach

Czy jest ktoś w stanie pomóc przynajmniej w kodzie na złapanie komunikacji między HA a Arduino?
W miejscu gdzie ma być Arduino mam skrętkę z LAN