From 0a4de8bf403ccbe2a7f08e3e3b92259ca09589e3 Mon Sep 17 00:00:00 2001 From: Peace Date: Fri, 9 Aug 2024 11:23:16 +0900 Subject: [PATCH] mqtt sample --- MQTTSample/MQTTSample.sln | 37 ++++++++++++++ MQTTSample/MQTTSample/MQTTSample.csproj | 10 ++++ MQTTSample/MQTTSample/Program.cs | 10 ++++ MQTTSample/MqttBrokerApp/MqttBrokerApp.csproj | 14 ++++++ MQTTSample/MqttBrokerApp/Program.cs | 27 ++++++++++ .../MqttPublisherApp/MqttPublisherApp.csproj | 14 ++++++ MQTTSample/MqttPublisherApp/Program.cs | 42 ++++++++++++++++ .../MqttSubscriberApp.csproj | 14 ++++++ MQTTSample/MqttSubscriberApp/Program.cs | 49 +++++++++++++++++++ 9 files changed, 217 insertions(+) create mode 100644 MQTTSample/MQTTSample.sln create mode 100644 MQTTSample/MQTTSample/MQTTSample.csproj create mode 100644 MQTTSample/MQTTSample/Program.cs create mode 100644 MQTTSample/MqttBrokerApp/MqttBrokerApp.csproj create mode 100644 MQTTSample/MqttBrokerApp/Program.cs create mode 100644 MQTTSample/MqttPublisherApp/MqttPublisherApp.csproj create mode 100644 MQTTSample/MqttPublisherApp/Program.cs create mode 100644 MQTTSample/MqttSubscriberApp/MqttSubscriberApp.csproj create mode 100644 MQTTSample/MqttSubscriberApp/Program.cs diff --git a/MQTTSample/MQTTSample.sln b/MQTTSample/MQTTSample.sln new file mode 100644 index 0000000..0a8a0e8 --- /dev/null +++ b/MQTTSample/MQTTSample.sln @@ -0,0 +1,37 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.10.35013.160 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MqttBrokerApp", "MqttBrokerApp\MqttBrokerApp.csproj", "{5A2FFC49-DB71-4852-A60F-B6437AF06C44}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MqttPublisherApp", "MqttPublisherApp\MqttPublisherApp.csproj", "{724A5227-244E-4257-A5B1-1688DCDF48F3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MqttSubscriberApp", "MqttSubscriberApp\MqttSubscriberApp.csproj", "{B8BC0A58-4F77-4B94-83B4-880C42960EF9}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5A2FFC49-DB71-4852-A60F-B6437AF06C44}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5A2FFC49-DB71-4852-A60F-B6437AF06C44}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5A2FFC49-DB71-4852-A60F-B6437AF06C44}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5A2FFC49-DB71-4852-A60F-B6437AF06C44}.Release|Any CPU.Build.0 = Release|Any CPU + {724A5227-244E-4257-A5B1-1688DCDF48F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {724A5227-244E-4257-A5B1-1688DCDF48F3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {724A5227-244E-4257-A5B1-1688DCDF48F3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {724A5227-244E-4257-A5B1-1688DCDF48F3}.Release|Any CPU.Build.0 = Release|Any CPU + {B8BC0A58-4F77-4B94-83B4-880C42960EF9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B8BC0A58-4F77-4B94-83B4-880C42960EF9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B8BC0A58-4F77-4B94-83B4-880C42960EF9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B8BC0A58-4F77-4B94-83B4-880C42960EF9}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {0007151D-F00A-4DD3-893C-73CF978E88D3} + EndGlobalSection +EndGlobal diff --git a/MQTTSample/MQTTSample/MQTTSample.csproj b/MQTTSample/MQTTSample/MQTTSample.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/MQTTSample/MQTTSample/MQTTSample.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/MQTTSample/MQTTSample/Program.cs b/MQTTSample/MQTTSample/Program.cs new file mode 100644 index 0000000..e0ba987 --- /dev/null +++ b/MQTTSample/MQTTSample/Program.cs @@ -0,0 +1,10 @@ +namespace MQTTSample +{ + internal class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello, World!"); + } + } +} diff --git a/MQTTSample/MqttBrokerApp/MqttBrokerApp.csproj b/MQTTSample/MqttBrokerApp/MqttBrokerApp.csproj new file mode 100644 index 0000000..b451e02 --- /dev/null +++ b/MQTTSample/MqttBrokerApp/MqttBrokerApp.csproj @@ -0,0 +1,14 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + diff --git a/MQTTSample/MqttBrokerApp/Program.cs b/MQTTSample/MqttBrokerApp/Program.cs new file mode 100644 index 0000000..fa96533 --- /dev/null +++ b/MQTTSample/MqttBrokerApp/Program.cs @@ -0,0 +1,27 @@ +using MQTTnet; +using MQTTnet.Server; + +namespace MqttBrokerApp +{ + internal class Program + { + private static readonly int PORT = 1883; + + static async Task Main(string[] args) + { + var mqttFactory = new MqttFactory(); + var mqttServerOption = new MqttServerOptionsBuilder() + .WithDefaultEndpoint() + .WithDefaultEndpointPort(PORT) + .Build(); + + var mqttServer = mqttFactory.CreateMqttServer(mqttServerOption); + await mqttServer.StartAsync(); + + Console.WriteLine("MQTT Broker started. Press any key to exit..."); + Console.ReadLine(); + + await mqttServer.StopAsync(); + } + } +} diff --git a/MQTTSample/MqttPublisherApp/MqttPublisherApp.csproj b/MQTTSample/MqttPublisherApp/MqttPublisherApp.csproj new file mode 100644 index 0000000..b451e02 --- /dev/null +++ b/MQTTSample/MqttPublisherApp/MqttPublisherApp.csproj @@ -0,0 +1,14 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + diff --git a/MQTTSample/MqttPublisherApp/Program.cs b/MQTTSample/MqttPublisherApp/Program.cs new file mode 100644 index 0000000..5c2d0f1 --- /dev/null +++ b/MQTTSample/MqttPublisherApp/Program.cs @@ -0,0 +1,42 @@ +using MQTTnet.Server; +using MQTTnet; +using MQTTnet.Client; +using System.Text; + +namespace MqttPublisherApp +{ + internal class Program + { + private static string CLIENT_ID = "PublisherClient"; + private static string SERVER_IP = "localhost"; + private static readonly int PORT = 1883; + private static readonly string TOPIC = "home/kitchen/temperature"; + + static async Task Main(string[] args) + { + var mqttFactory = new MqttFactory(); + var mqttClient = mqttFactory.CreateMqttClient(); + + var options = new MqttClientOptionsBuilder() + .WithClientId(CLIENT_ID) + .WithTcpServer(SERVER_IP, PORT) + .Build(); + + await mqttClient.ConnectAsync(options); + while (true) + { + Console.WriteLine("Enter a message to publish: "); + var message = Console.ReadLine(); + + var mqttMessage = new MqttApplicationMessageBuilder() + .WithTopic(TOPIC) + .WithPayload(Encoding.UTF8.GetBytes(message)) + .WithQualityOfServiceLevel(MQTTnet.Protocol.MqttQualityOfServiceLevel.AtLeastOnce) + .Build(); + + await mqttClient.PublishAsync(mqttMessage); + Console.WriteLine($"Message '{message}' published."); + } + } + } +} diff --git a/MQTTSample/MqttSubscriberApp/MqttSubscriberApp.csproj b/MQTTSample/MqttSubscriberApp/MqttSubscriberApp.csproj new file mode 100644 index 0000000..b451e02 --- /dev/null +++ b/MQTTSample/MqttSubscriberApp/MqttSubscriberApp.csproj @@ -0,0 +1,14 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + diff --git a/MQTTSample/MqttSubscriberApp/Program.cs b/MQTTSample/MqttSubscriberApp/Program.cs new file mode 100644 index 0000000..65b49bd --- /dev/null +++ b/MQTTSample/MqttSubscriberApp/Program.cs @@ -0,0 +1,49 @@ +using MQTTnet.Client; +using MQTTnet; +using System.Text; + +namespace MqttSubscriberApp +{ + internal class Program + { + private static IMqttClient _mqttClient; + private static string CLIENT_ID = $"SubscriberClient_{Random.Shared.Next(1, 1000)}"; + private static string SERVER_IP = "localhost"; + private static readonly int PORT = 1883; + private static readonly string TOPIC = "home/kitchen/temperature"; + + static async Task Main(string[] args) + { + var mqttFactory = new MqttFactory(); + _mqttClient = mqttFactory.CreateMqttClient(); + + var options = new MqttClientOptionsBuilder() + .WithClientId(CLIENT_ID) + .WithTcpServer(SERVER_IP, PORT) + .Build(); + + _mqttClient.ConnectedAsync += MqttClient_ConnectedAsync; + _mqttClient.ApplicationMessageReceivedAsync += MqttClient_ApplicationMessageReceivedAsync; + + await _mqttClient.ConnectAsync(options); + Console.ReadLine(); + } + + private static async Task MqttClient_ApplicationMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs arg) + { + var message = Encoding.UTF8.GetString(arg.ApplicationMessage.PayloadSegment); + Console.WriteLine($"Received message: {message}"); + } + + private static async Task MqttClient_ConnectedAsync(MqttClientConnectedEventArgs arg) + { + Console.WriteLine("Connected to broker."); + + await _mqttClient.SubscribeAsync(new MqttTopicFilterBuilder() + .WithTopic(TOPIC) + .Build()); + + Console.WriteLine($"Subscribed to topic '{TOPIC}'"); + } + } +}