簡體   English   中英

Arduino Knolleary PubSubClient將發布消息但無法接收消息

[英]Arduino Knolleary PubSubClient will publish messages but can't receive them

我正在使用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();
}

就像我說的,我可以看到所有蚊子都能正常工作。 我什至嘗試匹配沒有成功的client_ids。 任何幫助或想法將不勝感激。

您的訂閱主題“ testq”必須位於以下數組中

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

確保以''\\ 0'結尾數組

然后,您訂閱:

client.subscribe(testq);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM