본문 바로가기

프로젝트/MQTT 다중연결

C#에서 MQTT프로그램 만들기

프로젝트 -> NuGet 패키지 관리 -> M2Mqtt 설치해야 함

예제 소스
  using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;

namespace MQTT
{
    public partial class Form1 : Form
    {
        MqttClient client;
        string clientId;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {            
            string BrokerAddress = "broker.mqtt-dashboard.com"; //  각자 사용할 broker 서버를 기입한다.
            client = new MqttClient(BrokerAddress);

            // register a callback-function
            client.MqttMsgPublishReceived += client_MqttMsgPublishReceived;

            // use a unique id as client id, each time we start the application
            clientId = Guid.NewGuid().ToString();
            client.Connect(clientId);

            // 구독설정    각자 사용할 토픽으로 이름 변경 하세요
            client.Subscribe(new string[] { "토픽1", "토픽2", "토픽3", "토픽/bt01", "토픽/bt02", "토픽/bt03" }
                                                           , new byte[] { 0, 0, 0, 0, 0, 0 });

        }

    
        void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)   // 받은 Topic  처리
        {
            string msg = Encoding.UTF8.GetString(e.Message);    //  Paylod임
 
             switch (e.Topic)
            {
                case "토픽1": label1.Text = msg; break;
                case "토픽2": label2.Text = msg; break;
                case "토픽3": label3.Text = msg; break;
                case "토픽/bt01":
                    if (msg == "0") button1.BackColor = Color.Cyan;
                    else button1.BackColor = Color.White;
                    break;
                case "토픽/bt02":
                    if (msg == "0") button2.BackColor = Color.Red;
                    else button2.BackColor = Color.White;
                    break;
                case "토픽/bt03":
                    if (msg == "0") button3.BackColor = Color.Cyan;
                    else button3.BackColor = Color.White;
                    break;
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            client.Disconnect();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.BackColor == Color.Cyan)
            {
                button1.BackColor = Color.White;
                client.Publish("토픽/bt01", Encoding.UTF8.GetBytes("1"), 0, true);
            }
            else
            {
                button1.BackColor = Color.Cyan;
                client.Publish("토픽/bt01", Encoding.UTF8.GetBytes("0"), 0, true);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (button2.BackColor == Color.Red)
            {
                button2.BackColor = Color.White;
                client.Publish("토픽/bt02", Encoding.UTF8.GetBytes("1"), 0, true);
            }
            else
            {
                button2.BackColor = Color.Red;
                client.Publish("토픽/bt02", Encoding.UTF8.GetBytes("0"), 0, true);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (button3.BackColor == Color.Cyan)
            {
                button3.BackColor = Color.White;
                client.Publish("토픽/bt03", Encoding.UTF8.GetBytes("1"), 0, true);
            }
            else
            {
                button3.BackColor = Color.Cyan;
                client.Publish("토픽/bt03", Encoding.UTF8.GetBytes("0"), 0, true);
            }
        }
    }
}

'프로젝트 > MQTT 다중연결' 카테고리의 다른 글

Wemos D1, R1 (mini) 에서 MQTT 구현하기  (0) 2022.09.14
MQTT 다중 연결  (0) 2022.09.13