【问题标题】:MacAddress for topic name - Arduino IDE MQTT主题名称的 MacAddress - Arduino IDE MQTT
【发布时间】:2021-05-24 21:06:27
【问题描述】:

我想用我系统的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); 
  }

【问题讨论】:

  • Edit 包含minimal reproducible example 的问题。你在哪里打电话publish?你在哪里/如何使用你的宏?
  • 感谢您的回答!我已经用更多代码编辑了我的问题,希望对您有所帮助:)
  • publish 在类MQTTClient 中的声明是什么?
  • 我认为是发布(java.lang.String topic, byte[] payload, int qos, boolean reserved)
  • 编辑问题以说明您使用的是哪个 MQTT 客户端。

标签: c++ mqtt arduino-ide arduino-esp8266


【解决方案1】:

您正在使用#define,但尝试将字符串添加到变量中。请记住,#define 原语只是将您放在它后面的内容替换到代码中。该错误告诉您没有对具有StringSumHelper&amp; 参数的publish() 的函数调用,这就是#define 所放下的。

使用变量找出 MQTT 主题,然后在您的 publish() 调用中使用该变量。

【讨论】:

    【解决方案2】:

    首先你不应该以这种方式使用#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);
    

    【讨论】:

      猜你喜欢
      • 2023-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-27
      相关资源
      最近更新 更多