【问题标题】:Arduino Knolleary PubSubClient will publish messages but can't receive themArduino Knolleary PubSubClient 将发布消息但无法接收它们
【发布时间】:2013-10-04 18:06:19
【问题描述】:

我正在使用 Knolleary PubSubClient 连接到我的 MQTT 服务器。经过不多的工作,我已经能够成功地进行身份验证并建立连接。我什至可以将消息发布到主题。但是,我遇到的问题是我可以订阅主题并且没有错误,但是当我发布到该主题(从我的 Mac 上的 mosquitto)时,不会调用回调并且订阅主题的消息不会出现被接收。我曾尝试同时对同一主题运行 mosquitto 订阅,并且确实会收到已发布的消息。不确定我的回调代码是否有问题或这里发生了什么。任何帮助,将不胜感激。 Arduino代码如下:

/*
 Basic MQTT example 

  - connects to an MQTT server
  - publishes "hello world" to the topic "outTopic"
  - subscribes to the topic "inTopic"
*/
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte server[] = { 10, 2, 63, 123 };
byte ip[]     = { 192, 168, 1, 10 };


void callback(char* topic, byte* payload, unsigned int length) {
  Serial.println(topic);
  //convert byte to char
  payload[length] = '\0';
  String strPayload = String((char*)payload);

  Serial.println(strPayload);
  //int valoc = strPayload.lastIndexOf(',');
  //String val = strPayload.substring(valoc+1);
  //Serial.println(val);


}

EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);

void setup()
{
  Serial.begin(19200);
  Serial.println("==STARTING==");

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print("."); 
  }

  //delay(500);
  boolean con = client.connect("1", "3snzzon5dyade:abc", "OBSCURED_FOR_SEC");
  while(con != 1){
    Serial.println("no con-while");
     con = client.connect("1", "3snzzon5dyade:abc", "OBSCURED_FOR_SEC");
  }
  //Serial.println(con);
  if(con){
    Serial.println("got con");
    client.publish("testq","hello world");
    client.subscribe("testq");
  }else Serial.println("no con");

}

void loop()
{
  client.loop();
}

就像我说的,我可以看到 mosquitto 一切正常。我什至尝试匹配client_ids,但没有成功。任何帮助或想法将不胜感激。

【问题讨论】:

  • 我看不出您的代码有任何明显错误。当然,我已经把我所有的 arduino 都留在了工作中,否则我会测试它。如果你在星期一之前还没有解决它,我会试一试。
  • knolleary,感谢您的回复。这可能是由于代理与您的库不兼容吗?相同的代码适用于其他经纪人。
  • 您使用的是哪个代理实现?
  • 不完全确定您在问什么。但它是一个类似于 mosquitto 服务器的自定义代理实现。这可能是问题所在吗?
  • 如果代码适用于 mosquitto,但不适用于您的自定义代理实现,我想更仔细地查看您的实现正在做什么。我相信你在实施你的经纪人方面做得很好,但从我所在的位置来看,这是问题中的未知数。

标签: arduino mqtt


【解决方案1】:

您的订阅主题“testq”必须在一个数组中,例如

char testq[] = {'t', 'e', 's', 't', 'q', '\0'};

请务必使用 ,'\0' 结束您的数组

然后你订阅:

client.subscribe(testq);

【讨论】:

    猜你喜欢
    • 2022-10-14
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    • 2019-10-03
    相关资源
    最近更新 更多