【问题标题】:Callback function not executing second statement - C++ MQTT回调函数不执行第二条语句 - C++ MQTT
【发布时间】:2017-06-21 18:35:28
【问题描述】:

我编写了一个处理 MQTT 消息(有效负载)的回调函数。我想切换不基于传入有效负载的引脚。我现在遇到的问题是第一个语句在发送 11 时执行得很好。但是第二个语句不起作用。奇怪的是,当我发送两个包含 1x 和另一个 x1 的消息时,我可以再次切换两个引脚。这是一个非常奇怪的问题! 谁能帮我解决这个问题,非常感谢。我正在使用 PubSubClient 库。

 void callback(char* topic, byte* payload, unsigned int length) {
      Serial.print("Message arrived [");
      Serial.print(topic);
      Serial.print("] ");
      for (int i = 0; i < length; i++) {
        Serial.print((char)payload[i]);
      }

       //Set GPIO0 to HIGH or LOW on first character received in message
      if (payload[0] == '1') {
        digitalWrite(GPIO0, HIGH);   // Turn the relay on
        client.publish(getOutTopic().c_str(), "GPIO0 set to HIGH");
      } else if (payload[0] == '0') {
        digitalWrite(GPIO0, LOW);  // Turn the relay off
        client.publish(getOutTopic().c_str(), "GPIO0 set to LOW");
      }

      //Set GPIO2 to HIGH or LOW on first character received in message
      if (payload[1] == '1') {
        digitalWrite(GPIO2, HIGH);   // turn LED off. With High it is inactive  on the ESP-01)
        client.publish(getOutTopic().c_str(), "GPIO2 set to HIGH");
      } else if (payload[1] == '0') {
        digitalWrite(GPIO2, LOW);  // Turn the LED on by making the voltage LOW
        client.publish(getOutTopic().c_str(), "GPIO2 set to LOW");
      } 
}

【问题讨论】:

  • payload为11时for循环的输出是什么?此外,您应该在 for 循环中放置一个分隔符来区分有效负载 [0] 和有效负载 [1]。
  • forloop 正确显示了有效负载。
  • 在第一个 if / elseif 之后再次输出您的 payload 数据。我有一种感觉,您的 client.publish() 会破坏您的 payload 指针?如果它在第一个for 循环之后输出11,我看不出代码中为什么第二个if/else 不会触发。您是否还可以删除所有client.publish() 调用,只查看带 LED 的 GPIO 输出引脚?
  • 嗨,马克西米利安,你是绝对正确的。我已通过将有效负载保存到另一个变量来解决此问题,这样有效负载缓冲区就不会被破坏。谢谢

标签: c++ arduino mqtt publish-subscribe


【解决方案1】:

client.publish 会破坏负载缓冲区。所以我不得不将有效载荷捕获到新的变量中来解决这个问题。

 void callback(char* topic, byte* payload, unsigned int length) {
      char p0 = (char)payload[0];
      char p1 = (char)payload[1];
      Serial.print("Message arrived [");
      Serial.print(topic);
      Serial.print("] ");
      for (int i = 0; i < length; i++) {
        Serial.print((char)payload[i]);
      }

       //Set GPIO0 to HIGH or LOW on first character received in message
      if (p0 == '1') {
        digitalWrite(GPIO0, HIGH);   // Turn the relay on
        client.publish(getOutTopic().c_str(), "GPIO0 set to HIGH");
      } else if (p0 == '0') {
        digitalWrite(GPIO0, LOW);  // Turn the relay off
        client.publish(getOutTopic().c_str(), "GPIO0 set to LOW");
      }

      //Set GPIO2 to HIGH or LOW on first character received in message
      if (p1 == '1') {
        digitalWrite(GPIO2, HIGH);   // turn LED off. With High it is inactive  on the ESP-01)
        client.publish(getOutTopic().c_str(), "GPIO2 set to HIGH");
      } else if (p1 == '0') {
        digitalWrite(GPIO2, LOW);  // Turn the LED on by making the voltage LOW
        client.publish(getOutTopic().c_str(), "GPIO2 set to LOW");
      } 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 2018-07-22
    相关资源
    最近更新 更多