Arduino and home assistant connected (lan or usb)

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?

THANKS

1 Like

Thanks, I saw this topic, it helped me in parts, but I didn’t get the results I wanted

You have posted the same question already in more then 8 topics. If you showed your configuration.yaml, and explained what does not work, we could help. “I didn’t get the results I wanted” is not enough to help someone, we simply need more information.

ok I will send

You all right. This is may configuration. yaml

Blockquote
arduino:
port: /dev/ttyACM0

and below switch code on configuration. yaml

Blockquote
switch:
platform: arduino
pins:
11:
name: Fan Office
12:
name: Light Desk
initial: true
negate: true

Is the same as on de the example. And it´s work very well

On arduino I am using firmata schedule.

I need that arduino send data to home assistant about digital pin low or right. This is what I need.
On the pictures you can see my arduinos

Wow! Awesome installation!

I need that arduino send data to home assistant about digital pin low or right.

You can only read the values of the analog pins using the sensor integration.

sensor:
  - platform: arduino
    pins:
      1:
        name: arduinoA1

To get the “pseudo-state” of a digital pin you need to read the state of the switch assigned to the pin. If everything is working properly the digital pin state should always equal the state of the switch in Home Assistant.

First setup your switch:

- platform: arduino
  pins:
    13:
      name: arduino_led

The state of the digital pin will now be equal to the state of switch.arduino_led. Now you could create some kind of automation, for example when the Arduino LED is switched on or off, this automation would fire respectively:

- alias: 'do something when arduino LED is turned on'
  trigger:
    platform: state
    entity_id: switch.arduino_led
    to: 'on'
  action:
    # put some action here

- alias: 'do something when arduino LED is turned off'
  trigger:
    platform: state
    entity_id: switch.arduino_led
    to: 'off'
  action:
    # put some action here

Hope it helps!

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.

Can you post your final configuration ? We all want to learn.

To turn on a light at sunset (add to automations.yaml, replace light.lichtzitkamermuur with your light :

- id: '1585157850349'
  alias: Turn on the lights when the sun is set
  description: ''
  trigger:
  - event: sunset
    platform: sun
  condition: []
  action:
  - data:
      entity_id: light.lichtzitkamermuur
    service: light.turn_on

Thanks for you fast answer. I will post yes, I will prepare it and put it here in the post. I have a doubt as to what you put in now was to turn on the light, in fact I don’t want the light to turn on automatically at sunset. I want it to be possible for it to rise, for example. If you try to turn on during the day the lamp does not turn on, when the night comes if I click on the switch or on the home assistant the lamp will turn on.

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");}

}

Bom dia Ângelo, eu gostava de usar 1 arduino uno com shield Ethernet para ligar ao meu homeassistant por mqtt, já uso vários nodemcu com firmware tasmota também com mqtt e tudo funciona bem.

Mas no arduino nunca configurei mqtt , consegues me dar uma ajuda? Mandar o código que usas? Obrigado

amkochaki
Hello, you wrote a great and clear code. I am trying to run it for me, but something goes wrong. Could you please provide me the code from configuration.yaml

regards