[英]MacAddress for topic name - Arduino IDE MQTT
我想用我系統的mac地址作為主題名。
我想要類似的東西:project/00:1B:44:11:3A:B7/temperature/status
我試過這樣:
#define TEMP_STATUS_TOPIC "project/" + WiFi.macAddress() + "temperature/status"
#define TEMP_CONTROL_TOPIC "project/temperature/control"
但我得到這個錯誤:
no matching function for call to 'MQTTClient::publish(StringSumHelper&, char [128], size_t&)'
任何提示將不勝感激!
編輯:
我正在使用 mqtt.fx 客戶端。
這是我稱之為發布的地方:
sensors.requestTemperatures();
float t = sensors.getTempCByIndex(0);
bool static led_temp_status = HIGH;
const int capacity = JSON_OBJECT_SIZE(3);
StaticJsonDocument<capacity> poolsystem;
if (t < destemp) {
led_temp_status = LOW;
digitalWrite(LED_EXTERNAL_TEMP, led_temp_status);
const int capacity = JSON_OBJECT_SIZE(3);
StaticJsonDocument<capacity> poolsystem;
poolsystem["temp"] = t;
poolsystem["heatstatus"] = "on";
char buffer[128];
size_t n = serializeJson(poolsystem, buffer);
Serial.print(F("JSON message: "));
Serial.println(buffer);
mqttClient.publish(TEMP_STATUS_TOPIC, buffer, n);
} else {
led_temp_status = HIGH;
digitalWrite(LED_EXTERNAL_TEMP, led_temp_status);
poolsystem["temp"] = t;
poolsystem["heatstatus"] = "off";
char buffer[128];
size_t n = serializeJson(poolsystem, buffer);
Serial.print(F("JSON message: "));
Serial.println(buffer);
mqttClient.publish(TEMP_STATUS_TOPIC, buffer, n);
}
您正在使用#define
但嘗試像變量一樣添加字符串。 請記住, #define
原語只是將您放在它后面的內容替換到代碼中。 該錯誤告訴您沒有 function 調用具有StringSumHelper&
參數的publish()
,這是#define
所放下的。
使用變量找出 MQTT 主題,然后在您的publish()
調用中使用該變量。
首先,您不應該以這種方式使用#define
,就像在另一個答案中指出的那樣。
您可以改為TEMP_STATUS_TOPIC
聲明為const String
:
const String TEMP_STATUS_TOPIC = "project/" + WiFi.macAddress() + "temperature/status";
MQTTClient::publish()
的問題在於,第一個參數需要一個 C 字符串const char*
來作為主題名稱。 將這些行替換為:
mqttClient.publish(TEMP_STATUS_TOPIC.c_str(), buffer, n);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.