我正在尝试使esp传感器自动发现到家庭:
#include <WiFi.h>
#include <PubSubClient.h>
#include "HX710B.h"
const int DOUT = 21;
const int SCLK = 22;
int prev = 0;
int pa = 0;
HX710B pressure_sensor;
const char *WIFI_SSID = "ssid";
const char *WIFI_PASSWORD = "pass";
const char* mqttServer = "192.168.1.xx";
const int mqttPort = 1883;
const char* mqttUser = "user";
const char* mqttPassword = "pass";
float pressure;
int sensorNumber = 1;
String mqttName = "prsensor " + String(sensorNumber);
String stateTopic = "prsensor_" + String(sensorNumber) + "/pressure/state";
String statusTopic = "prsensor_" + String(sensorNumber) + "/status";
String discoveryTopic = "homeassistant/sensor/prsensor/" + String(sensorNumber) + "/pressure/config";
WiFiClient wifiClient;
PubSubClient client(wifiClient);
void setup() {
Serial.begin(115200);
pressure_sensor.begin(DOUT, SCLK);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.println("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
WiFi.setAutoReconnect(true);
WiFi.persistent(true);
Serial.println("Connected to Wi-Fi");
client.setServer(mqttServer, mqttPort);
Serial.println("Connecting to MQTT");
}
void publishDiscoveryMessage() {
String payload = "{";
payload += ""dev_cla":"pressure",";
payload += ""unit_of_meas":"Pa",";
payload += ""stat_cla":"measurement",";
payload += ""name":"prsensor pressure",";
payload += ""entity_category":"diagnostic",";
payload += ""stat_t":"" + String(stateTopic) + "",";
payload += ""avty_t":"" + String(statusTopic) + "",";
payload += ""uniq_id":"prsensor_id",";
payload += ""val_tpl":"{{ value_json.pressure|default(0) }}",";
payload += ""dev":{"ids":"5ccf7f9b18a2"}";
payload += "}";
// Publish MQTT discovery message
bool published = client.publish(discoveryTopic.c_str(), payload.c_str(), true);
Serial.print("published: ");
Serial.print(published);
Serial.printf(" Published %s to %sn", payload.c_str(), discoveryTopic.c_str());
}
void loop() {
while (!client.connected()) {
Serial.print(".");
if (client.connect(mqttName.c_str(), mqttUser, mqttPassword)) {
Serial.println("Connected to MQTT");
} else {
Serial.println("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
client.loop();
if (WiFi.status() == WL_CONNECTED) {
Serial.println("===== Sending Data =====");
pressure = pressure_sensor.pascal();
if (isnan(pressure)) {
pressure = 0;
}
String pl = "{";
pl += ""pressure":"" + String(pressure) + """;
pl += "}";
bool plpub = client.publish(stateTopic.c_str(), pl.c_str(), true);
client.publish(statusTopic.c_str(), String("online").c_str(), true);
Serial.print("111published: ");
Serial.print(plpub);
Serial.printf(" Published %s to %sn", pl.c_str(), stateTopic.c_str());
publishDiscoveryMessage();
delay(2000);
Serial.println("pressure: ");
Serial.println(pressure);
}
else {
Serial.println("WiFi Disconnected");
}
}
具有状态和状态发布的有效负载正常,但publishDiscoveryMessage()中的有效负载不正常。发布状态= 0。我用的是Lolin D32 pro。以前我曾尝试使用ArduinoJson库,结果是相同的。
here logs of publishing:
00:11:30.677 -> ===== Sending Data =====
00:11:30.677 -> 111published: 1 Published {"pressure":"462.10"} to prsensor_1/pressure/state
00:11:30.677 -> published: 0 Published {"dev_cla":"pressure","unit_of_meas":"Pa","stat_cla":"measurement","name":"prsensor pressure","entity_category":"diagnostic","stat_t":"prsensor_1/pressure/state","avty_t":"prsensor_1/status","uniq_id":"prsensor_id","val_tpl":"{{ value_json.pressure|default(0) }}","dev":{"ids":"5ccf7f9b18a2"}} to homeassistant/sensor/prsensor/1/pressure/config
如果我将payload改为
String payload = "{";
payload += ""dev_cla":"signal_strength",";
payload += ""unit_of_meas":"Pa",";
payload += ""stat_cla":"measurement",";
payload += ""name":"prsensor pressure",";
// payload += ""entity_category":"diagnostic",";
payload += ""stat_t":"" + String(stateTopic) + "",";
payload += ""avty_t":"" + String(statusTopic) + "",";
payload += ""uniq_id":"prsensor_id"";
// payload += ""val_tpl":"{{ value_json.pressure|default(0) }}",";
// payload += ""dev"";
// payload += ":{";
// payload += ""ids":"5ccf7f9b18a2"";
// payload += "}";
payload += "}";
mqtt有效负载发布,但它不足以在homeassistant中实现自动发现。
PubSubClient有一个最大数据包大小。如果你阅读文档,你会发现里面有解释:
MQTT_MAX_PACKET_SIZE设置最大数据包大小,以字节为单位客户将处理。接收到的任何超过此大小的数据包会被忽略。此值可以通过调用来重写setBufferSize(大小)。
默认值:128字节
您试图发布的字符串大于128字节,因此,根据文档,发布失败。
您可以在设置MQTT客户端时重写它:
client.setServer(mqttServer, mqttPort);
client.setBufferSize(400);
400足以容纳代码中的字符串;如果你需要的话,你应该修改它。