Please help with constructor non macro use

Dear All, I am beginner.
I am trying library HaMqttEntities.h
In an example of library, is defined macros

#define HA_DEVICE_ID   "example04"
#define HA_DEVICE_FRIENDLY_NAME "Example Numeric-Sensor HA-MQTT"

Then constructor
HADevice ha_device = HADevice(HA_DEVICE_ID,HA_DEVICE_FRIENDLY_NAME,"1.0");

But in my project, I need fill this HA_DEVICE_ID and HA_DEVICE_FRIENDLY_NAME from string, that I get from other function.

How can I put string variables to constructor?
In src library is this inputs as const char.
Thanks

Just use a String? ... XXX.c_str()

I tried that but it doesn't work.

HADevice ha_device = HADevice( exchange_data.serial_number.c_str() , exchange_data.serial_number.c_str() , exchange_data.fw_ver.c_str() );

Home Assistant will not add device. I see nonsense data in MQTT explorer.

HADevice ha_device = HADevice( "TEST" , "MYTEST" , SW_VER );

This works for a test, but is not satisfactory.

As long as do not say what you expect and don't say what you get it's hard to tell ...

Did you try to print all these strings to be sure that it contain correct data?

The library is saving the char* provided to it in the constructor, so you will need to ensure that the contents of the String does not change, and does not get deleted (do not use a local String that ceases to exist at the end of a function).

In setup, the ESP32 get some data from the second cpu Atmel ATMEGA2560.
HA MQTT is in ESP32.

string serial_number is readed from Atmega as "AH1200BL-0412133A"
string fw_ver is readed from Atmega as "1.0"

If I use c_str() in the constructor, in HA MQTT Explorer I see working sensors, but device is not added to HA devices.
In explorer, sensors has this config messages.

{"name":"WP Speed","unique_id":"rych_prac_prost","device":{"identifiers":"(��?","name":"(��?","sw_version":""},"state_topic":"homeassistant/sensor/rych_prac_prost/state","unit_of_measurement":"m/s"}

As you see, identifiers and name has mish mash

The strings from second cpu I have printed to console. Are right.
But, are readed later, in the setup(),
to this string variable:
exchange_data.serial_number

this variable is global and readed only once in setup

Well, you don't show your code, so nobody can tell what you actually do.

Sorry, zip is possible...
esp_comunicator.zip (1.5 MB)

ha_global.h has HA constructor

Sorry, I can't help. Your code is a total mess of spagetticode. grep cannot even find any of your posted constant strings like "unique_id". Clean this up, make functions instead of milelong #ifdef #endif sections, format the code.

Okay. Here is easy edited example from HA library.


/* ha-mqtt-entities library example of a sensor that shows
    history graph in Home Assistant.

  - Board: ESP32*

  You must set the definitions in the code under SECRETS_H.

*/
#include<Arduino.h>
#include<WiFi.h>
#include<PubSubClient.h>
#include<mat.h>

#include<HaMqttEntities.h>

// This file is not included in the repository only used for local testing
// #include "secrets.h"

// You must set the next defines
#ifndef SECRETS_H
#define WIFI_SSID "***"
#define WIFI_PASSWORD "***"
#define MQTT_SERVER "haiot.mydomain.sk"
#define MQTT_PORT 1883
#define MQTT_USER "espcom"
#define MQTT_PASSWORD "*****"
#endif

WiFiClient wifi_client;
PubSubClient mqtt_client(wifi_client);

// HA Parts
#define ENTITIES_COUNT 1
#define HA_DEVICE_ID   "example04"
#define HA_DEVICE_FRIENDLY_NAME "Example Numeric-Sensor HA-MQTT"

#define UNIT_OF_MEASUREMENT "V"
#define PRECISION 2  // Number of decimals

char *serial;
HADevice ha_device = HADevice( serial , serial , "1.0" );

HASensorNumeric ha_sensor = HASensorNumeric(
                              "sensor05uid", "Volts", ha_device, UNIT_OF_MEASUREMENT, PRECISION);

// SERIAL NUMBER
String serial_number;

void setup() {
  // for test, here is string filled
  serial_number = "AH1200BL-0412133A";
  serial = serial_number.c_str(); // **************** HERE IS COMPILE ERROR

  mqtt_client.setServer(MQTT_SERVER, MQTT_PORT);

  HAMQTT.begin(mqtt_client, ENTITIES_COUNT);
  HAMQTT.addEntity(ha_sensor);

  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}

void loop() {
  static unsigned long one_second_delay = millis() + 1000;
  static float counter = 0;
  HAMQTT.loop();
  if (!HAMQTT.connected() &&
      !HAMQTT.connect("examples", MQTT_USER, MQTT_PASSWORD))
    delay(1000);

  if (millis() > one_second_delay) {
    // Update the sensor every second creating a sin wave
    one_second_delay = millis() + 1000;
    counter += 0.1;
    ha_sensor.setState(3.3 * sin(counter));
  }

}

Compile error
invalid conversion from 'const char*' to 'char*' [-fpermissive]

which line does the error refer to? .. oh I see. Make the line

char *serial ="AH1200BL-0412133A";

and

// serial_number = "AH1200BL-0412133A";
// serial = serial_number.c_str(); // **************** HERE IS COMPILE ERROR

has flag
// **************** HERE IS COMPILE ERROR

Are you understand how the string pointers works?

You call the constructor with two undefined pointers< what do you try to achieve?

I'm sorry, but you don't even have minimal knowledge of the subject. The serial_number is a pointer to the char. Why on earth are you trying to apply a c_str() method of String class to it?

I'm a beginner, so I'm looking for help. I don't have much control over pointers.
I am looking for a solution to do this.
I can't define a string firmly, I get it only in setup().

try this:

This works. But how do I add the real serial_number (as String) later in setup()?

Why do you need Strings if neither your text from another function nor constructor arguments are not Strings????