Arduino ClearCore 장치를 raspberry pi에서 호스팅되는 MQTT 서버에 연결할 수 없습니다.

Aug 19 2020

내 목표:

Arduino를 사용하여 프로그래밍 한 ClearCore 장치의 라즈베리 파이에서 호스팅되는 MQTT 서버 (모기를 사용하고 있음)와 통신하려고합니다.

내 문제 :

비슷한 장치와 기술을 사용하여 원하는 것을 달성하는 수많은 예제를 온라인에서 찾았습니다. 그러나 Arduino 소프트웨어를 사용하여 ClearCore 장치에서 raspberry pi에 호스팅 된 MQTT 브로커에 연결할 수 없습니다.

내 설정 :

클리어 코어 장치에서 라즈베리 파이로 이더넷 연결을 사용하고 있습니다. Teknic CLCR-4-13을 사용하고 있습니다. DHCP를 사용하고 있지 않습니다. 재부팅 할 때마다 라즈베리 파이의 IP 주소를 설정하므로 항상 그것이 무엇인지 알고 있습니다 (아래 명령 참조). mosquitto.conf 파일 (포트 : 1883)과 "username"과 "password"를 정의하는 password_file을 만들었습니다.

파이를 재부팅 할 때마다이 명령을 실행하므로 고정 IP를 만들 필요가 없습니다.

sudo ifconfig eth0 192.168.1.23 netmask 255.255.255.0

내가 시도한 것 :

  • 내 PC에서-이더넷 연결 및 Python 스크립트를 사용하여 MQTT 서버의 이름으로 raspberry pi의 IP 주소를 사용하여 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

파이에서 올 때 메시지를보기 위해 이것을 사용합니다.

실패하는 곳 ...

mqttClient.setServer(server, 1883); 

if (mqttClient.connect("myClientID","user","pass"))
{
    //error message
}

잠재적 인 문제에 대한 생각 :

유사한 프로젝트에 대해 본 대부분의 예-사람들은 "test.mosquitto.org"를 서버 이름으로 사용하고 있지만, 내 라즈베리 파이에 자체 MQTT 서버를 구성했기 때문에 대신 라즈베리 파이의 IP 주소를 서버로 사용합니다. 이름. 이것은 파이썬 스크립트를 사용하여 PC에서 연결했을 때 작동했지만 이것이 내 Arduino 프로그램에서 문제인지 모르겠습니다.

충분한 정보를 제공했으면합니다. 도움이 될만한 다른 내용이 있으면 알려주세요. 모든 의견에 감사드립니다.

답변

3 CB_Ron Aug 19 2020 at 00:47

장치의 IP 주소를 라즈베리 파이 서버와 동일하게 설정하는 것 같습니다.

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

그것은 작동하지 않을 것입니다. 장치 IP를 IPAddress ip(192, 168, 1, 24).