No entity / arduino

Hello,
I’ll warn you that I’m a beginner.
I have a problem: I have a Raspberry Pi 5 with an Arduino Mega 2560 connected to it. I loaded a simple program using the mysensors library to control two relays. After connecting second Arduino to the second serial port, everything fell apart.

I’m now trying to revert to the original configuration, but despite setting the correct port-ID and clearing the mysensors.json files, it still only detects one entity: Relay 0 Battery.
Previously, the program worked on the first Arduino, and I had relay entities available. Now, even with only the first one connected, it doesn’t work – it also detects only one entity, “relay - battery.”
Does anyone have any ideas what could be causing this?
Please help me, I’ve been working on this for days now

Is this related to Home Assistant or ESPHome in any way?

yes, it is in home assistant.

Hi Kuba,

I see after your clarification that you’re using Home Assistant, but it’s still really unclear what you’re trying to do.

Can you walk us through what the “simple program” was? (Post your YAML code if possible.) Any additional specifics you can share will be helpful for the folks on this forum–and they are heroes.

I wanted to control the house lighting with home assistant using digital outputs in Arduino. The server is on a raspberry pi 5. The device was added manually in HA, so I didn’t change anything in the yaml file
This is my simple program for arduino:



// Enable debug prints to serial monitor
#define MY_DEBUG 


// Enable and select radio type attached
//#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69

// Set LOW transmit power level as default, if you have an amplified NRF-module and
// power your radio separately with a good regulator you can turn up PA level. 
//#define MY_RF24_PA_LEVEL RF24_PA_LOW

// Enable serial gateway
#define MY_GATEWAY_SERIAL

// Define a lower baud rate for Arduino's running on 8 MHz (Arduino Pro Mini 3.3V & SenseBender)
#if F_CPU == 8000000L
#define MY_BAUD_RATE 38400
#endif

// Flash leds on rx/tx/err
// #define MY_LEDS_BLINKING_FEATURE
// Set blinking period
// #define MY_DEFAULT_LED_BLINK_PERIOD 300

// Inverses the behavior of leds
// #define MY_WITH_LEDS_BLINKING_INVERSE

// Enable inclusion mode
#define MY_INCLUSION_MODE_FEATURE
// Enable Inclusion mode button on gateway
#define MY_INCLUSION_BUTTON_FEATURE

// Inverses behavior of inclusion button (if using external pullup)
//#define MY_INCLUSION_BUTTON_EXTERNAL_PULLUP

// Set inclusion mode duration (in seconds)
#define MY_INCLUSION_MODE_DURATION 60 
// Digital pin used for inclusion mode button
#define MY_INCLUSION_MODE_BUTTON_PIN  3 

// Uncomment to override default HW configurations
//#define MY_DEFAULT_ERR_LED_PIN 4  // Error led pin
//#define MY_DEFAULT_RX_LED_PIN  6  // Receive led pin
//#define MY_DEFAULT_TX_LED_PIN  5  // the PCB, on board LED

#include <SPI.h>
#include <MySensors.h>  
#include <Bounce2.h>

// Enable repeater functionality for this node
#define MY_REPEATER_FEATURE


#define RELAY_1  4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define RELAY_2  5
#define NUMBER_OF_RELAYS 2 // Total number of attached relays
#define RELAY_ON 1  // GPIO value to write to turn on attached relay
#define RELAY_OFF 0 // GPIO value to write to turn off attached relay

#define BUTTON_PIN A1
#define BUTTON2_PIN A2


void before() { 
  for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
    // Then set relay pins in output mode
    pinMode(pin, OUTPUT);   
    // Set relay to last known state (using eeprom storage) 
    digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
  }
}
Bounce debouncer = Bounce();
Bounce debouncer2 = Bounce();

void setup() { 
  // Setup locally attached sensors
  delay(10000);
   // Setup the button.
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(BUTTON2_PIN, INPUT_PULLUP);
  // After setting up the button, setup debouncer.
  debouncer.attach(BUTTON_PIN);
  debouncer.interval(5);
  debouncer2.attach(BUTTON2_PIN);
  debouncer2.interval(5);

  //presentation();
}
void presentation()  
{   
  // Send the sketch version information to the gateway and Controller
  sendSketchInfo("Relay", "1.0");

  for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
    // Register all sensors to gw (they will be created as child devices)
    present(sensor, S_LIGHT);
  }
}

MyMessage msg(1, V_LIGHT);
MyMessage msg2(2, V_LIGHT);

void loop() { 
  // Send locally attached sensor data here 
  if (debouncer.update()) {
    // Get the update value.
    int value = debouncer.read();
    // Send in the new value.
    if(value == LOW){
         saveState(1, !loadState(1));
         digitalWrite(RELAY_1, loadState(1)?RELAY_ON:RELAY_OFF);
         send(msg.set(loadState(1)));
         }
  }
  if (debouncer2.update()) {
      int value2 = debouncer2.read();
    if(value2 == LOW){
         saveState(2, !loadState(2));
         digitalWrite(RELAY_2, loadState(2)?RELAY_ON:RELAY_OFF);
         send(msg2.set(loadState(2)));
         }
  }
}


void receive(const MyMessage &message) {
  // We only expect one type of message from controller. But we better check anyway.
  if (message.type==V_LIGHT) {
     // Change relay state
     digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
     // Store state in eeprom
     saveState(message.sensor, message.getBool());
     // Write some debug info
     Serial.print("Incoming change for sensor:");
     Serial.print(message.sensor);
     Serial.print(", New status: ");
     Serial.println(message.getBool());
   } 
}





Hello Kuba,

Start by forgetting the serial port thing and use MQTT over Ethernet to talk to HA…

I do this with a PI3, but Arduino is just as doable.

Could you explain why not use serial? I’m just curious.
I’ll try MQTT over Ethernet.

The mega 2560 does not have Ethernet? Or you should use a w5500 shield.

Because you are working with HA’s kernel and things are probably not working the way you expect them to and you probably don’t have access or control to adjust that.
Where HA handles MQTT natively.

Thank you very much for your help.
I ordered the w5500 shiled.
I understand that now the entities should appear automatically in the MQTT add-on in HA?

I’m going to download this program. I only changed the IP address. Are these libraries enough?


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

// Set led variables to Arduino digital pins
int led1 = 2;
int led2 = 3;
int led3 = 5;                             // pin 4 used by ethernet shield
int led4 = 6;


// Set button variables to Arduino digital pins
int button1 = 7;
int button2 = 8;
int button3 = 9;
int button4 = 10;                         // pins 11,12,13 used by ethernetshield

// Set variables to act as virtual switches
// Set variable values initially to LOW (and not HIGH)
int led1Value = LOW;             
int led2Value = LOW;
int led3Value = LOW;
int led4Value = LOW;


//---------------------------------------------------------------------------

// Arduino MAC address is on a sticker on your Ethernet shield
// must be unique for every node in same network
// To make a new unique address change last letter

byte mac[]    = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE };  

// Unique static IP address of this Arduino - change to adapt to your network
IPAddress ip(192,168,0,136);

// IP Address of your MQTT broker - change to adapt to your network
byte server[] = { 192, 168, 0, 134 };

// Handle and convert incoming MQTT messages ----------------------------------------

void callback(char* topic, byte* payload, unsigned int length) {
  // handle message arrived
  String content="";
  char character;
  for (int num=0;num<length;num++) {
      character = payload[num];
      content.concat(character);
  }   
  Serial.println(topic);
  Serial.println(content); // message sent out by button actions is returned from broker and serial printed


// Set specific virtual switches on basis of specific incoming messages ----------------------------
  
  if (content == "1on") {
    led1Value = HIGH;
  }
  
  if (content == "1off") {
    led1Value = LOW;
  }
  
  if (content == "2on") {
    led2Value = HIGH;
  }
  
  if (content == "2off") {
    led2Value = LOW;
  }

  
  if (content == "3on") {
    led3Value = HIGH;
  }
  
  if (content == "3off") {
    led3Value = LOW;
  }
  
  if (content == "4on") {
    led4Value = HIGH;
  }
  
  if (content == "4off") {
    led4Value = LOW;
  }
  
    
  // Set digital pin states according to virtual switch settings
    
  digitalWrite(led1,led1Value);
  digitalWrite(led2,led2Value);
  digitalWrite(led3,led3Value);
  digitalWrite(led4,led4Value);

}

// Initiate instances -----------------------------------

EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);

// Initiate a bouncer instance for each button
Bounce bouncer1 = Bounce();
Bounce bouncer2 = Bounce();
Bounce bouncer3 = Bounce();
Bounce bouncer4 = Bounce();

//-------------------------------------------------------

void setup()

{

  // setup led, button, bouncer 1 -----------------------
  pinMode(led1, OUTPUT);
  pinMode(button1,INPUT);
  digitalWrite(button1,HIGH);
  bouncer1 .attach(button1);
  bouncer1 .interval(5);

  // setup led, button, bouncer 2 -----------------------
  pinMode(led2, OUTPUT);
  pinMode(button2,INPUT);
  digitalWrite(button2,HIGH);
  bouncer2 .attach(button2);
  bouncer2 .interval(5);

  // setup led, button, bouncer 3 -----------------------
  pinMode(led3, OUTPUT);
  pinMode(button3,INPUT);
  digitalWrite(button3,HIGH);
  bouncer3 .attach(button3);
  bouncer3 .interval(5);

  // setup led, button, bouncer 4 -----------------------
  pinMode(led4, OUTPUT);
  pinMode(button4,INPUT);
  digitalWrite(button4,HIGH);
  bouncer4 .attach(button4);
  bouncer4 .interval(5);

  // setup serial and ethernet communications -------------------------------

  // Setup serial connection
  Serial.begin(9600);

  // Setup ethernet connection to MQTT broker
  Ethernet.begin(mac);
  if (client.connect("arduino-ip-238")) {  							// change as desired - clientname must be unique for MQTT broker
    client.publish("led","hello world - here arduino ip 239");
    Serial.println("connected");
    client.subscribe("led");										// subscribe to topic "led"
  }
}

//----------------------------------------------

void loop()
{

// Listen for button interactions and take actions ----------------------------------------  
// Note: Button actions do send MQTT message AND do set led(x)Value to HIGH or LOW

  if (bouncer1.update()) {
    if (bouncer1.read() == HIGH) {
      if (led1Value == LOW) {
        led1Value = HIGH;
        client.publish("led","1on");								
      } else {
        led1Value = LOW;
        client.publish("led","1off");
      }
    }
  }  

//-----------------------------------------------
  
  if (bouncer2.update()) {
    if (bouncer2.read() == HIGH) {
      if (led2Value == LOW) {
        led2Value = HIGH;
        client.publish("led","2on");
      } else {
        led2Value = LOW;
        client.publish("led","2off");
      }
    }
  }  
  
//------------------------------------------------  

  if (bouncer3.update()) {
    if (bouncer3.read() == HIGH) {
      if (led3Value == LOW) {
        led3Value = HIGH;
        client.publish("led","3on");
      } else {
        led3Value = LOW;
        client.publish("led","3off");
      }
    }
  }  

//-----------------------------------------------
  
  if (bouncer4.update()) {
    if (bouncer4.read() == HIGH) {
      if (led4Value == LOW) {
        led4Value = HIGH;
        client.publish("led","4on");
      } else {
        led4Value = LOW;
        client.publish("led","4off");
      }
    }
  }  
  
//------------------------------------------------  
  
  client.loop();
}

// End of sketch ---------------------------------

Normally MQTT messages are not formed into a HA device, but HA have made a protocol to make it possible.
I am not sure your script include that part, but it can be done manually also.

When working with MQTT the program MQTT Explorer is a must to see what is happening on the MQTT broker.
It is available to nearly all platforms and also as an addon (or maybe it is an integration) in HA.
I prefer to run it outside HA, so it does not disconnect when HA restarts.

1 Like