无法将 Arduino ClearCore 设备连接到 raspberry pi 上托管的 MQTT 服务器
Unable to connect Arduino ClearCore device to MQTT server hosted on raspberry pi
我的目标:
我正在尝试通过我使用 Arduino 编程的 ClearCore 设备与托管在 raspberry pi 上的 MQTT 服务器(我正在使用 mosquitto)进行通信。
我的问题:
我在网上找到了很多例子,它们使用类似的设备和技术实现了我想要的。但是,我无法使用我的 Arduino 软件从我的 ClearCore 设备连接到我的 raspberry pi.
上托管的 MQTT 代理
我的设置:
我正在使用从我的透明核心设备到我的 raspberry pi 的以太网连接。我正在使用 Teknic CLCR-4-13。我没有使用 DHCP。每次重新启动时,我都会设置 raspberry pi 的 IP 地址,所以我总是知道它是什么(请参阅下面的命令)。我已经创建了 mosquitto.conf 文件(端口:1883)和 password_file,我在其中定义了“用户名”和“密码”。
我 运行 每当我重新启动我的 pi 时都会执行此命令,因此我不必创建静态 IP。
sudo ifconfig eth0 192.168.1.23 netmask 255.255.255.0
我试过的:
- 从我的 PC - 使用以太网连接和 python 脚本,我可以使用 raspberry pi 的 IP 地址作为名称连接、订阅和发布到我的 MQTT 服务器MQTT 服务器。
import paho.mqtt.publish as pub
MQTT_SERVER = "192.168.1.23"
MQTT_PATH = "dev/test"
credentials = {'username':"user",'password':"pass"}
import time
while True:
pub.single(MQTT_PATH, "Hello Pi!", hostname = MQTT_SERVER, auth = credentials)
time.sleep(3)
print(".")
- 为了验证我可以使用以太网电缆从 ClearCore 设备和 raspberry pi 传输数据,我已经使用 Arduino 程序成功发送了 UDP 包。我使用相同的 mac 和 IP 地址。
- 我尝试通过在 Arduino 程序中将其定义为旧版本来更改 MQTT 版本。
- 当我 运行 验证连接尝试的程序时,我使用 Wireshark 监控以太网流量。
我的Arduino程序:
注意:一切都编译和编程 运行s 成功,但无法连接到 MQTT 服务器
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
//#define MQTT_VERSION MQTT_VERSION_3_1
//#define MQTT_VERSION MQTT_VERSION_3_1_1
//#define MQTT_VERSION MQTT_VERSION_5_0
// Function prototypes
void subscribeReceive(char* topic, byte* payload, unsigned int length);
// Set your MAC address and IP address here
byte mac[] = {0x24, 0x15, 0x10, 0xb0, 0x00, 0x3f};
IPAddress ip(192, 168, 1, 23);
const char* server = "192.168.1.23";
// Ethernet and MQTT related objects
EthernetClient ethClient;
PubSubClient mqttClient(ethClient);
void setup() {
// Useful for debugging purposes
Serial.begin(9600);
// Start the ethernet connection
Ethernet.begin(mac, ip);
// Ethernet takes some time to boot!
delay(3000);
// Set the MQTT server to the server stated above ^
mqttClient.setServer(server, 1883);
// Attempt to connect to the server with the ID "myClientID"
if (mqttClient.connect("myClientID","user","pass"))
{
Serial.println("Connection has been established, well done");
// Establish the subscribe event
mqttClient.setCallback(subscribeReceive);
}
else
{
Serial.println("Looks like the server connection failed...");
}
}
void loop() {
mqttClient.loop();
mqttClient.subscribe("dev/test");
if(mqttClient.publish("dev/test", "Hello World"))
{
Serial.println("Publish message success");
}
else
{
Serial.println("Could not send message :(");
}
// Dont overload the server!
delay(4000);
}
void subscribeReceive(char* topic, byte* payload, unsigned int length)
{
// Print the topic
Serial.print("Topic: ");
Serial.println(topic);
// Print the message
Serial.print("Message: ");
for(int i = 0; i < length; i ++)
{
Serial.print(char(payload[i]));
}
// Print a newline
Serial.println("");
}
来自 Raspberry Pi
的命令
mosquitto_sub -d -u user -P pass -t dev/test
我用它来查看来自 pi 的消息。
失败的地方...
mqttClient.setServer(server, 1883);
if (mqttClient.connect("myClientID","user","pass"))
{
//error message
}
潜在问题的想法:
我见过的大多数类似项目的例子——人们使用“test.mosquitto.org”作为他们的服务器名称,但因为我在我的 raspberry pi 上配置了我自己的 MQTT 服务器,所以我改为使用 IP raspberry pi 的地址作为服务器名称。这在我使用 python 脚本从我的 PC 连接时有效,但我不知道这是否是我的 Arduino 程序中的问题。
希望我提供了足够的信息。如果我还有其他您可能希望看到的内容可能对您有帮助,请告诉我 - 感谢所有反馈。
您似乎将设备的 IP 地址设置为与 raspberry pi 服务器相同。
IPAddress ip(192, 168, 1, 23);
const char* server = "192.168.1.23";
那是行不通的。使设备 IP 不同,例如 IPAddress ip(192, 168, 1, 24)
.
我的目标:
我正在尝试通过我使用 Arduino 编程的 ClearCore 设备与托管在 raspberry pi 上的 MQTT 服务器(我正在使用 mosquitto)进行通信。
我的问题:
我在网上找到了很多例子,它们使用类似的设备和技术实现了我想要的。但是,我无法使用我的 Arduino 软件从我的 ClearCore 设备连接到我的 raspberry pi.
上托管的 MQTT 代理我的设置:
我正在使用从我的透明核心设备到我的 raspberry pi 的以太网连接。我正在使用 Teknic CLCR-4-13。我没有使用 DHCP。每次重新启动时,我都会设置 raspberry pi 的 IP 地址,所以我总是知道它是什么(请参阅下面的命令)。我已经创建了 mosquitto.conf 文件(端口:1883)和 password_file,我在其中定义了“用户名”和“密码”。
我 运行 每当我重新启动我的 pi 时都会执行此命令,因此我不必创建静态 IP。
sudo ifconfig eth0 192.168.1.23 netmask 255.255.255.0
我试过的:
- 从我的 PC - 使用以太网连接和 python 脚本,我可以使用 raspberry pi 的 IP 地址作为名称连接、订阅和发布到我的 MQTT 服务器MQTT 服务器。
import paho.mqtt.publish as pub
MQTT_SERVER = "192.168.1.23"
MQTT_PATH = "dev/test"
credentials = {'username':"user",'password':"pass"}
import time
while True:
pub.single(MQTT_PATH, "Hello Pi!", hostname = MQTT_SERVER, auth = credentials)
time.sleep(3)
print(".")
- 为了验证我可以使用以太网电缆从 ClearCore 设备和 raspberry pi 传输数据,我已经使用 Arduino 程序成功发送了 UDP 包。我使用相同的 mac 和 IP 地址。
- 我尝试通过在 Arduino 程序中将其定义为旧版本来更改 MQTT 版本。
- 当我 运行 验证连接尝试的程序时,我使用 Wireshark 监控以太网流量。
我的Arduino程序:
注意:一切都编译和编程 运行s 成功,但无法连接到 MQTT 服务器
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
//#define MQTT_VERSION MQTT_VERSION_3_1
//#define MQTT_VERSION MQTT_VERSION_3_1_1
//#define MQTT_VERSION MQTT_VERSION_5_0
// Function prototypes
void subscribeReceive(char* topic, byte* payload, unsigned int length);
// Set your MAC address and IP address here
byte mac[] = {0x24, 0x15, 0x10, 0xb0, 0x00, 0x3f};
IPAddress ip(192, 168, 1, 23);
const char* server = "192.168.1.23";
// Ethernet and MQTT related objects
EthernetClient ethClient;
PubSubClient mqttClient(ethClient);
void setup() {
// Useful for debugging purposes
Serial.begin(9600);
// Start the ethernet connection
Ethernet.begin(mac, ip);
// Ethernet takes some time to boot!
delay(3000);
// Set the MQTT server to the server stated above ^
mqttClient.setServer(server, 1883);
// Attempt to connect to the server with the ID "myClientID"
if (mqttClient.connect("myClientID","user","pass"))
{
Serial.println("Connection has been established, well done");
// Establish the subscribe event
mqttClient.setCallback(subscribeReceive);
}
else
{
Serial.println("Looks like the server connection failed...");
}
}
void loop() {
mqttClient.loop();
mqttClient.subscribe("dev/test");
if(mqttClient.publish("dev/test", "Hello World"))
{
Serial.println("Publish message success");
}
else
{
Serial.println("Could not send message :(");
}
// Dont overload the server!
delay(4000);
}
void subscribeReceive(char* topic, byte* payload, unsigned int length)
{
// Print the topic
Serial.print("Topic: ");
Serial.println(topic);
// Print the message
Serial.print("Message: ");
for(int i = 0; i < length; i ++)
{
Serial.print(char(payload[i]));
}
// Print a newline
Serial.println("");
}
来自 Raspberry Pi
的命令mosquitto_sub -d -u user -P pass -t dev/test
我用它来查看来自 pi 的消息。
失败的地方...
mqttClient.setServer(server, 1883);
if (mqttClient.connect("myClientID","user","pass"))
{
//error message
}
潜在问题的想法:
我见过的大多数类似项目的例子——人们使用“test.mosquitto.org”作为他们的服务器名称,但因为我在我的 raspberry pi 上配置了我自己的 MQTT 服务器,所以我改为使用 IP raspberry pi 的地址作为服务器名称。这在我使用 python 脚本从我的 PC 连接时有效,但我不知道这是否是我的 Arduino 程序中的问题。
希望我提供了足够的信息。如果我还有其他您可能希望看到的内容可能对您有帮助,请告诉我 - 感谢所有反馈。
您似乎将设备的 IP 地址设置为与 raspberry pi 服务器相同。
IPAddress ip(192, 168, 1, 23);
const char* server = "192.168.1.23";
那是行不通的。使设备 IP 不同,例如 IPAddress ip(192, 168, 1, 24)
.