我正在使用knolleary库将Arduino UNO板连接到MQTT服务器。对于经纪人我使用的是test.mosquitto.org(85.119.83.194),但我无法连接。
这是我的代码:
/*
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”)返回false并且“客户端未连接”消息显示为Serial Monitor。
我不确定
的价值是什么byte server[] = { 10, 2, 63, 123};
作为替代方案,我还尝试连接到Intranet中的Really Simple Message Broker(RSMB)。我仍然得到同样的信息。
任何人都可以帮忙吗?
提前致谢
SRS
答案 0 :(得分:3)
你有错误的方法;
byte server[]
是你的mqtt服务器的ip地址,在你的情况下是test.mosquitto.org(85.119.83.194)
byte ip[]
是您希望arduino在您的网络上拥有的静态IP地址。
要检查的另一件事是你可以使用cli客户端连接到test.mosquitto.org,因为我发现它有时会失效。
看看我的温度发布代码,https://github.com/matbor/arduinoTemps2mqtt它可能会给你一些提示,因为它是从PubSubClient附带的原始示例中修改的。在我的一个,我只是让arduino运行DHCP。