다음 내용은 Raspberry Pi에서 Mosquitto MQTT 브로커를 설치하고, C#의 MQTTnet 라이브러리를 이용하여 MQTT 클라이언트를 설정하여 메시지를 송수신하는 과정입니다. 이 작업을 통해 Raspberry Pi는 MQTT 브로커 역할을 하며, 다른 디바이스와의 통신을 중계하게 됩니다.
1. Mosquitto MQTT 브로커 설치 및 설정
설치 과정
패키지 목록 업데이트
sudo apt-get update
패키지 목록을 최신 상태로 업데이트하여 Mosquitto 설치에 필요한 파일을 준비합니다.
Mosquitto 설치
sudo apt-get install mosquitto mosquitto-clients
Mosquitto 브로커와 클라이언트 도구를 설치합니다. Mosquitto는 MQTT 프로토콜을 사용하는 브로커이고, mosquitto-clients는 mosquitto_pub과 mosquitto_sub 등의 명령줄 도구를 제공하여 테스트할 수 있게 합니다.
start 명령으로 Mosquitto 서비스를 시작하고, enable 명령으로 부팅 시 자동으로 Mosquitto가 실행되도록 설정합니다.
설정 파일 수정
Mosquitto 설정 파일을 수정하여 모든 사용자가 접근할 수 있도록 익명 액세스를 허용합니다.
설정 파일에서 allow_anonymous를 true로 변경합니다. 이렇게 하면, 별도의 사용자 인증 없이 모든 사용자가 브로커에 연결할 수 있게 됩니다.
변경 사항 적용
Mosquitto 서비스 다시 시작
sudo systemctl restart mosquitto
설정 파일 변경 후 Mosquitto 서비스를 다시 시작하여 변경 사항을 적용합니다.
MQTT 통신 테스트
구독 테스트
mosquitto_sub -t home/temperature
mosquitto_sub 명령으로 home/temperature 주제를 구독합니다. 이 명령을 실행하면, 해당 주제에서 발행된 메시지를 실시간으로 확인할 수 있습니다.
2. C# MQTT 클라이언트 코드 설명
C#의 MQTTnet 라이브러리를 사용하여 MQTT 클라이언트를 구현하는 코드입니다. 이 클라이언트는 특정 주제(home/temperature, home/humidity)에 구독하여 메시지를 수신합니다.
코드 설명
네임스페이스 및 라이브러리 로드
using System;using System.Text;using System.Threading.Tasks;using MQTTnet;using MQTTnet.Client;
System, System.Text, System.Threading.Tasks: 기본적인 C# 기능을 사용하기 위해 추가됩니다.
MQTTnet, MQTTnet.Client: MQTT 클라이언트 라이브러리로, MQTT 메시지를 송수신하는 데 필요한 기능을 제공합니다.
MQTT 클라이언트 생성 및 옵션 설정
var factory =newMqttFactory();var mqttClient = factory.CreateMqttClient();var options =newMqttClientOptionsBuilder() .WithClientId("RaspberryPiClient") .WithTcpServer("Raspberry Pi IP", 1883) // Replace with your Raspberry Pi IP .Build();
MqttFactory: MQTT 클라이언트를 생성하기 위한 팩토리 클래스입니다.
mqttClient: 생성된 MQTT 클라이언트 인스턴스로, 이후 브로커와의 연결 및 메시지 수신/발행 기능을 수행합니다.
MqttClientOptionsBuilder: MQTT 연결 옵션을 설정하는 빌더입니다.
.WithClientId("RaspberryPiClient"): 클라이언트 ID를 설정합니다. 고유한 ID를 지정하여 클라이언트를 식별할 수 있습니다.
.WithTcpServer("Raspberry Pi IP", 1883): 브로커의 IP 주소와 포트 번호를 설정합니다. 1883은 기본 MQTT 포트입니다. Raspberry Pi의 IP 주소로 바꿔야 합니다.
이벤트 핸들러 정의
성공적인 연결 이벤트
mqttClient.ConnectedAsync +=asynce=>{ Console.WriteLine("Connected to MQTT Broker.");// 주제 구독 설정await mqttClient.SubscribeAsync(newMqttClientSubscribeOptionsBuilder() .WithTopicFilter("home/temperature") .WithTopicFilter("home/humidity") .Build()); Console.WriteLine("Subscribed to topics: home/temperature, home/humidity");};
ConnectedAsync: 브로커에 연결되었을 때 호출되는 비동기 이벤트 핸들러입니다.
연결이 성공하면 "Connected to MQTT Broker." 메시지를 출력하고, 주제 home/temperature와 home/humidity에 구독을 요청합니다.
연결 종료 이벤트
mqttClient.DisconnectedAsync +=e=>{ Console.WriteLine("Disconnected from MQTT Broker.");return Task.CompletedTask;};
DisconnectedAsync: 브로커에서 연결이 종료될 때 호출되는 비동기 이벤트 핸들러입니다. "Disconnected from MQTT Broker." 메시지를 출력합니다.
ApplicationMessageReceivedAsync: 구독한 주제에서 메시지를 수신할 때 호출되는 비동기 이벤트 핸들러입니다.
수신된 메시지의 주제와 내용을 출력하여, "Received message from {topic}: {message}" 형식으로 표시합니다.
브로커 연결 및 종료 처리
await mqttClient.ConnectAsync(options);Console.WriteLine("Press any key to exit...");Console.ReadLine();await mqttClient.DisconnectAsync();
ConnectAsync(options): 설정된 옵션을 사용하여 MQTT 브로커에 연결합니다.
Console.ReadLine(): 사용자가 키를 입력할 때까지 프로그램을 대기 상태로 유지합니다.
DisconnectAsync(): 사용자가 종료 명령을 입력하면 브로커와 연결을 종료합니다.
MQTT 메시지 송수신 예시
브로커에 연결: Raspberry Pi의 IP 주소를 가진 Mosquitto 브로커에 연결합니다.
주제 구독: home/temperature, home/humidity 주제를 구독하여 해당 주제로 메시지가 발행될 때마다 수신할 준비를 합니다.
메시지 수신: 구독한 주제에서 메시지가 수신되면, 주제와 메시지 내용을 출력합니다.
using System;using System.Text;using System.Threading.Tasks;using MQTTnet;using MQTTnet.Client;namespaceRaspberryPiMQTTClient{classProgram {staticasync Task Main (string[] args) {var factory =newMqttFactory();var mqttClient = factory.CreateMqttClient();var options =newMqttClientOptionsBuilder() .WithClientId("RaspberryPiClient") .WithTcpServer("Raspberry Pi IP", 1883) // Replace with your Raspberry Pi IP .Build();// Event handler for successful connection mqttClient.ConnectedAsync +=asynce=> { Console.WriteLine("Connected to MQTT Broker.");// Subscribe to the topicsawait mqttClient.SubscribeAsync(newMqttClientSubscribeOptionsBuilder() .WithTopicFilter("home/temperature") .WithTopicFilter("home/humidity") .Build()); Console.WriteLine("Subscribed to topics: home/temperature, home/humidity"); };// Event handler for when the client disconnects mqttClient.DisconnectedAsync +=e=> { Console.WriteLine("Disconnected from MQTT Broker.");return Task.CompletedTask; };// Event handler for received messages mqttClient.ApplicationMessageReceivedAsync +=e=> {var topic = e.ApplicationMessage.Topic;var message = Encoding.UTF8.GetString(e.ApplicationMessage.Payload); Console.WriteLine($"Received message from {topic}: {message}");return Task.CompletedTask; };// Connect to the MQTT brokerawait mqttClient.ConnectAsync(options); Console.WriteLine("Press any key to exit..."); Console.ReadLine();// Disconnect from the MQTT brokerawait mqttClient.DisconnectAsync(); } }}
이렇게 설정하면, Raspberry Pi의 Mosquitto 브로커에서 home/temperature와 home/humidity 주제로 메시지가 발행될 때마다 C# 클라이언트에서 이를 받아 Serial Console에 표시합니다.