无法将Arduino ClearCore设备连接到树莓派上托管的MQTT服务器

时间:2020-08-18 17:38:50

标签: arduino raspberry-pi mqtt mosquitto

我的目标:

我正在尝试与我使用Arduino编程的ClearCore设备与树莓派上托管的MQTT服务器(我正在使用mosquitto)进行通信。

我的问题:

我在网上发现了许多示例,这些示例可以使用类似的设备和技术来实现我想要的功能。但是,我无法使用Arduino软件从ClearCore设备连接到树莓派上托管的MQTT代理。

我的设置:

我正在使用从透明核心设备到树莓派的以太网连接。我正在使用 Teknic CLCR-4-13 。我没有使用DHCP。我每次重新启动时都要设置rapiberry pi的IP地址,所以我总是知道它是什么(请参阅下面的命令)。我创建了mosquitto.conf文件(端口:1883)和password_file,其中定义了“用户名”和“密码”。

每当我重新启动pi时,我都会运行此命令,因此不必创建静态IP。

sudo ifconfig eth0 192.168.1.23 netmask 255.255.255.0

我尝试过的事情:

  • 在我的PC上-使用以太网连接和python脚本,我可以使用树莓派的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设备和树莓派传输数据,我已经使用Arduino程序成功发送了UDP包。我使用相同的Mac和IP地址。
  • 我试图通过在Arduino程序中将其定义为旧版本来更改MQTT版本。
  • 运行程序以验证是否进行连接尝试时,我曾使用Wireshark监视以太网流量。

我的Arduino程序:

注意:一切都可以编译并且程序可以成功运行,但是无法连接到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”作为服务器名称,但是因为我在树莓派上配置了自己的MQTT服务器,所以我改用树莓派的IP地址作为服务器名称。当我使用python脚本从PC连接时,此方法有效,但我不知道这是否是Arduino程序中的问题。

我希望我提供了足够的信息。请让我知道是否还有其他您可能想帮忙的地方-感谢您提供的所有反馈。

1 个答案:

答案 0 :(得分:3)

您似乎将设备的IP地址设置为与树莓派服务器相同。

IPAddress ip(192, 168, 1, 23);
 
const char* server = "192.168.1.23";

那是行不通的。使设备IP有所不同,例如IPAddress ip(192, 168, 1, 24)