mqtt sample
This commit is contained in:
37
MQTTSample/MQTTSample.sln
Normal file
37
MQTTSample/MQTTSample.sln
Normal file
@@ -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
|
10
MQTTSample/MQTTSample/MQTTSample.csproj
Normal file
10
MQTTSample/MQTTSample/MQTTSample.csproj
Normal file
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
10
MQTTSample/MQTTSample/Program.cs
Normal file
10
MQTTSample/MQTTSample/Program.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace MQTTSample
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
}
|
||||
}
|
||||
}
|
14
MQTTSample/MqttBrokerApp/MqttBrokerApp.csproj
Normal file
14
MQTTSample/MqttBrokerApp/MqttBrokerApp.csproj
Normal file
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MQTTnet" Version="4.3.6.1152" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
27
MQTTSample/MqttBrokerApp/Program.cs
Normal file
27
MQTTSample/MqttBrokerApp/Program.cs
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
14
MQTTSample/MqttPublisherApp/MqttPublisherApp.csproj
Normal file
14
MQTTSample/MqttPublisherApp/MqttPublisherApp.csproj
Normal file
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MQTTnet" Version="4.3.6.1152" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
42
MQTTSample/MqttPublisherApp/Program.cs
Normal file
42
MQTTSample/MqttPublisherApp/Program.cs
Normal file
@@ -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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
14
MQTTSample/MqttSubscriberApp/MqttSubscriberApp.csproj
Normal file
14
MQTTSample/MqttSubscriberApp/MqttSubscriberApp.csproj
Normal file
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MQTTnet" Version="4.3.6.1152" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
49
MQTTSample/MqttSubscriberApp/Program.cs
Normal file
49
MQTTSample/MqttSubscriberApp/Program.cs
Normal file
@@ -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}'");
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user