【发布时间】:2015-05-13 13:58:08
【问题描述】:
大家好,我有一个问题,我无法通过以太网屏蔽与 arduino-uno 客户端在我的计算机(windows 7 x64)上通信 MQTT 代理(mosquitto),我已经从 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”,我不知道问题出在哪里以及如何解决。 提前致谢;
【问题讨论】:
-
您的 Arduino 和 Win 计算机真的在不同的子网上吗?导致服务器和 Arduino IP 看起来很奇怪。
-
你能从 Win 电脑 PING 你的 Arduino 吗?
-
嗨@MichalFoksa,问题是通过更改我的arduino 的id 地址来解决的,但是arduino 客户端上的mqtt 库中的当前问题,因为发布方法很好,但订阅没有你有关于这个的想法??
-
这个问题中提到的代码已经解决了stackoverflow.com/questions/29740560/…,除了我的回答中解决的网络问题。尝试结合它。
-
嗨@MichalFoksa,这并不能解决我的问题,我想在我的arduino上订阅主题并从安装在树莓派上的蚊子经纪人那里收到消息,但它仍然是错误的,我想在其中运行此指令我的覆盆子: mosquitto_pub -t inTopic -m test -h 192.168.1.25 (这是arduino的IP地址)。所以我运行这个后没有结果只是“错误:连接超时”,请问是什么问题
标签: ethernet mqtt arduino-uno mosquitto