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.
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
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]
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().