mqtt经纪人窗口与arduino客户端

时间:2015-05-13 13:58:08

标签: ethernet mqtt arduino-uno mosquitto

嘿大家我有一个问题,我无法通过以太网屏蔽与我的计算机(Windows 7 x64)上的MQTT代理(mosquitto)与arduino-uno客户端通信,我已经从knolleary安装了PubSubClient库并导入它是我的arduino IDE,这是我的代码来源:

 /*
  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>

 int ledPin = 5;


// Update these with values suitable for your network.
byte mac[]    = { 0x74,0xD0,0x2B,0xEC,0xC3,0xEE };
//@ mac of the arduino in my computer
byte server[] = {192,168,1,4};
//the ip @ of the broker installed in mylocalhost
byte ip[] = {192.168.1.25};
//ip @ of the arduino in my computer



void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
Serial.print(topic);

}


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.publish("outTopic","hello world");
//client.subscribe("inTopic");
}
else{
      Serial.println("Client not connected"); 
}
}


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

运行此代码后,显示“Ethernet Begin”和“Client not connected”,我无法解决问题以及如何解决问题。 提前致谢;

2 个答案:

答案 0 :(得分:0)

没有与Ardino合作但是 你有没有关闭运行代理的Win7计算机上的防火墙,它可能拒绝1883端口。

https://github.com/francoisvdm/TT3是一个很好的mqtt客户端,用于测试代理是否可以在其他Windows计算机上运行。

答案 1 :(得分:0)

问题得到解决,因为当我用我的arduino客户端连接到代理时,我打开与我的覆盆子pi上安装的代理的会话,所以非常简单地发布到arduino是subsribe的主题只是在这样的代理中运行这个命令: mosquitto_pub -t inTopic -m test;没有-h 192.168.1.25而且效果很好,最后非常感谢@Michal Foska