简体   繁体   English

无法连接到MQTT服务器

[英]Unable to connect to MQTT Server

I am using knolleary library to connect the Arduino UNO board to MQTT server. 我正在使用knolleary库将Arduino UNO板连接到MQTT服务器。 For broker I am using test.mosquitto.org (85.119.83.194) but I am not able to connect. 对于经纪人我使用test.mosquitto.org(85.119.83.194),但我无法连接。

Here is my code: 这是我的代码:

    /*
 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, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
byte server[] = { 10, 2, 63, 123};
byte ip[]     = { 85, 119, 83, 194 };

void callback(char* topic, byte* payload, unsigned int length) {
  // handle message arrived
  Serial.println("Message received");
}

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

void setup()
{
   Ethernet.begin(mac, ip);
   Serial.begin(9600);
   Serial.println("Ethernet Begin");

   if (client.connect("arduinoClient")) {
        Serial.println("Client connected");
        client.subscribe("/notification/turnlighton");
   }
   else{
          Serial.println("Client not connected"); 
   }
}

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

client.connect("arduinoClient") return false and "Client not connected" message is printed Serial Monitor. client.connect(“arduinoClient”)返回false并且“客户端未连接”消息打印串行监视器。

I am not sure what should be the value of 我不确定应该是什么价值

byte server[] = { 10, 2, 63, 123};

As an alternate I also tried to connect to the Really Simple Message Broker (RSMB) in intranet. 作为替代方案,我还尝试连接到Intranet中的Really Simple Message Broker(RSMB)。 Still I get same message. 我仍然得到同样的信息。

Can any one help in here? 任何人都可以在这里帮忙吗?

Thanks in advance 提前致谢

SRS SRS

You have it around the wrong way; 你有错误的方法;

byte server[] is your mqtt server's ip address, in your case test.mosquitto.org (85.119.83.194) byte server[]是你的mqtt服务器的ip地址,在你的情况下是test.mosquitto.org(85.119.83.194)

byte ip[] is the static ip address you want the arduino to have on your network. byte ip[]是您希望arduino在您的网络上拥有的静态IP地址。

The other thing to check is that you can connect using a cli client to test.mosquitto.org as it is sometimes down I find. 要检查的另一件事是你可以使用cli客户端连接到test.mosquitto.org,因为我发现它有时会失败。

Have a look at my temperature publishing code, https://github.com/matbor/arduinoTemps2mqtt it might give you a few hints as it was modified from that original example that comes with PubSubClient. 看看我的温度发布代码, https://github.com/matbor/arduinoTemps2mqtt它可能会给你一些提示,因为它是从PubSubClient附带的原始示例中修改的。 In my one I just have the arduino running off DHCP. 在我的一个,我只是让arduino运行DHCP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM