Arduino的Knolleary PubSubClient将公布消息,但不能接收 [英] Arduino Knolleary PubSubClient will publish messages but can't receive them

查看:792
本文介绍了Arduino的Knolleary PubSubClient将公布消息,但不能接收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用的是Knolleary PubSubClient,使我的MQTT服务器的连接。我已经能够成功进行身份验证,使后作品不多的连接。我甚至可以发布消息到主题。不过,我遇到的问题是,我可以订阅主题,并得到没有错误,但是当我发布到该主题(从mosquitto我的Mac)回调不会被调用和消息发送到订阅主题不会出现被接收。我试图在同一时间运行mosquitto订阅同一主题,而不会收到发布的消息。不知道是否有一个问题在我的回调code或什么是怎么回事。任何帮助将是AP preciated。 Arduino的code是如下:

I am using the Knolleary PubSubClient to make a connection to my MQTT server. I have been able to successfully authenticate and make a connection after not much work. I can even publish messages to topics. However, the issue I am having is that I can subscribe to topics and get no error, but when I publish to that topic (from mosquitto on my Mac) the callback does not get called and the message to the subscribe topic does not appear to be received. I have tried running a mosquitto subscription to the same topic at the same time, and that does receive the published message. Not sure if there is a problem in my callback code or what is going on here. Any help would be appreciated. Arduino code is below:

/*
 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。任何帮助或想法将不胜AP preciated。

Like I said, I can see everything working properly with mosquitto. I have even tried matching up client_ids with no luck. Any help or ideas would be greatly appreciated.

推荐答案

您订阅主题testq必须是一个数组像

your subscribe topic "testq" must be in an array like

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

请务必与以finisht阵列,'\\ 0'

be sure to finisht your array with an ,'\0'

然后你订阅:

client.subscribe(testq);

这篇关于Arduino的Knolleary PubSubClient将公布消息,但不能接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆