Discovery not work

Discovery not work ... :- ( WHY please

#include <WiFi.h>
#include <PubSubClient.h>

// Wi-Fi připojení
const char* ssid = "W";
const char* password = "L";

// MQTT připojení
const char* mqttServer = "xxx.xxx.xxx.xxx";
const int mqttPort = 1885;
const char* mqttUser = "xxxx";
String mqttPassword = "Xxxx";

// MQTT témata
const char* mqttDiscoveryTopic = "homeassistant/sensor/esp32_monitor/config";
const char* mqttMonitorTopic = "homeassistant/sensor/esp32_monitor/state";

WiFiClient espClient;
PubSubClient client(espClient);

float temperature = 22.5;
float humidity = 60.0;

void connectToWiFi() {
Serial.print("Připojuji k Wi-Fi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);

unsigned long startAttemptTime = millis();
while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < 20000) {
delay(1000);
Serial.println("Připojuji...");
}

if (WiFi.status() == WL_CONNECTED) {
Serial.println("Připojeno k Wi-Fi!");
Serial.print("IP adresa: ");
Serial.println(WiFi.localIP());
} else {
Serial.println("Nepodařilo se připojit k Wi-Fi.");
}
}

void connectToMQTT() {
client.setServer(mqttServer, mqttPort);

while (!client.connected()) {
Serial.print("Připojuji k MQTT brokeru: ");
Serial.println(mqttServer);

String clientId = "ESP32Client-" + String(random(0xffff), HEX);

if (client.connect(clientId.c_str(), mqttUser, mqttPassword.c_str())) {
  Serial.println("Připojeno k MQTT brokeru!");
} else {
  Serial.print("MQTT připojení selhalo, kód: ");
  Serial.println(client.state());
  delay(2000);
}

}
}

void sendDiscoveryConfig() {
// Správně naformátovaný JSON payload
String payload = R"###(
{
"name": "ESP32 Monitor",
"state_topic": "homeassistant/sensor/esp32_monitor/state",
"unique_id": "esp32_monitor",
"device": {
"identifiers": ["esp32_device"],
"name": "ESP32 Device",
"manufacturer": "Custom",
"model": "ESP32 MQTT Monitor"
},
"value_template": "{{ value_json.temperature }}",
"unit_of_measurement": "°C",
"device_class": "temperature"
}
)###";

Serial.print("Odesílám Discovery zprávu: ");
Serial.println(payload);

// Vytvoření ekvivalentního příkazu Bash
String bashCommand = "mosquitto_pub -h ";
bashCommand += mqttServer;
bashCommand += " -p ";
bashCommand += mqttPort;
bashCommand += " -u "" + String(mqttUser) + """;
bashCommand += " -P '" + mqttPassword + "'";
bashCommand += " -t "" + String(mqttDiscoveryTopic) + """;
bashCommand += " -m '" + payload + "'";
Serial.print("Ekvivalentní příkaz Bash: ");
Serial.println(bashCommand);

// PUBLISH s retenčním příznakem
if (client.publish(mqttDiscoveryTopic, payload.c_str(), true)) {
Serial.println("Discovery zpráva úspěšně odeslána.");
} else {
Serial.println("Nepodařilo se odeslat Discovery zprávu.");
}
}

void sendState() {
temperature += random(-5, 6) * 0.1;
humidity += random(-3, 4) * 0.1;

String payload = "{"temperature": " + String(temperature, 1) +
", "humidity": " + String(humidity, 1) + "}";

Serial.print("Odesílám stavovou zprávu: ");
Serial.println(payload);

// Vytvoření ekvivalentního příkazu Bash
String bashCommand = "mosquitto_pub -h ";
bashCommand += mqttServer;
bashCommand += " -p ";
bashCommand += mqttPort;
bashCommand += " -u "" + String(mqttUser) + """;
bashCommand += " -P '" + mqttPassword + "'";
bashCommand += " -t "" + String(mqttMonitorTopic) + """;
bashCommand += " -m '" + payload + "'";
Serial.print("Ekvivalentní příkaz Bash: ");
Serial.println(bashCommand);

if (client.publish(mqttMonitorTopic, payload.c_str())) {
Serial.println("Stavová zpráva úspěšně odeslána.");
} else {
Serial.println("Nepodařilo se odeslat stavovou zprávu.");
}
}

void setup() {
Serial.begin(115200);
connectToWiFi();
connectToMQTT();
sendDiscoveryConfig();
}

void loop() {
if (!client.connected()) {
connectToMQTT();
}
client.loop();

static unsigned long lastPublish = 0;
if (millis() - lastPublish > 15000) {
lastPublish = millis();
sendState();
}
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

2 Likes

Need to fix this:

sketch.ino: In function 'void sendState()':
sketch.ino:110:34: error: unable to find string literal operator 'operator""temperature' with 'const char [4]', 'unsigned int' arguments
   String payload = "{"temperature": " + String(temperature, 1) +
                                  ^~~~
sketch.ino:111:32: error: unable to find string literal operator 'operator""humidity' with 'const char [5]', 'unsigned int' arguments
                    ", "humidity": " + String(humidity, 1) + "}";
                                ^~~~

Error during build: exit status 1

... with this...

  String payload = "{temperature: " + String(temperature, 1) +
                   ", humidity: " + String(humidity, 1) + "}";
1 Like