-
Notifications
You must be signed in to change notification settings - Fork 2
/
MeadowApp.cs
105 lines (92 loc) · 3.65 KB
/
MeadowApp.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using Meadow;
using Meadow.Devices;
using Meadow.Hardware;
using MQTTnet;
using MQTTnet.Client;
using MQTTnet.Client.Options;
using System;
using System.Security.Authentication;
using System.Threading;
using System.Threading.Tasks;
namespace TLS_Client_Authentication;
public class MeadowApp : App<F7CoreComputeV2>
{
public override Task Initialize()
{
var wifi = Device.NetworkAdapters.Primary<IWiFiNetworkAdapter>();
wifi.NetworkConnected += WiFiAdapter_NetworkConnected;
return Task.CompletedTask;
}
async void WiFiAdapter_NetworkConnected(INetworkAdapter networkAdapter, NetworkConnectionEventArgs e)
{
// IMPORTANT: Steps before using this sample app
// 1. Configure your Device on Azure IoT Hub
// 2. Upload the client_cert.pem, private_key.pem and
// private_key_pass.txt (optional) into your device
// 3. Replace IOT_HUB_NAME and IOT_HUB_DEVICE_ID with your Azure IoT Hub data
var client = new MQTTnetTestClient();
await client.Initialize();
}
}
class MQTTnetTestClient
{
public async System.Threading.Tasks.Task Initialize()
{
// IMPORTANT:
// Replace these variables with your Azure IoT Hub data
string IOT_HUB_NAME = "IOT_HUB_NAME";
string IOT_HUB_DEVICE_ID = "IOT_HUB_DEVICE_ID";
// IMPORTANT:
// Using Client Authentication, keep this value empty
// Using Token-based Authentication, replace it with your SAS token
string IOT_HUB_DEVICE_ID_TOKEN = "";
Console.WriteLine("Attempting to connect to " + IOT_HUB_DEVICE_ID);
var factory = new MqttFactory();
var mqttClient = factory.CreateMqttClient();
// IMPORTANT:
// You don't need to add any code references to the client
// certificate, since OS/Mono will attach your certificate
// to any connection that requires it automatically
var options = new MqttClientOptionsBuilder()
.WithClientId(IOT_HUB_DEVICE_ID)
.WithTcpServer($"{IOT_HUB_NAME}.azure-devices.net", 8883)
.WithCredentials($"{IOT_HUB_NAME}.azure-devices.net/{IOT_HUB_DEVICE_ID}/?api-version=2021-04-12", IOT_HUB_DEVICE_ID_TOKEN)
.WithProtocolVersion(MQTTnet.Formatter.MqttProtocolVersion.V311)
.WithTls(new MqttClientOptionsBuilderTlsParameters
{
UseTls = true,
SslProtocol = SslProtocols.Tls12,
})
.Build();
mqttClient.UseConnectedHandler(async e =>
{
Console.WriteLine("Connected to broker");
});
var connected = false;
while (!connected)
{
try
{
await mqttClient.ConnectAsync(options, CancellationToken.None);
connected = true;
Console.WriteLine("Connected!!");
}
catch (Exception ex)
{
Console.WriteLine("Error! Please check your client certificate and private key files. ", ex);
}
}
string[] messages = { "Accio!", "Aguamenti!", "Alarte Ascendare!", "Expecto Patronum!", "Homenum Revelio!" };
foreach (var message in messages)
{
Console.WriteLine($"Sending message: {message}");
var mqttMessage = new MqttApplicationMessageBuilder()
.WithTopic($"devices/{IOT_HUB_DEVICE_ID}/messages/events/")
.WithPayload(message)
.Build();
await mqttClient.PublishAsync(mqttMessage);
await System.Threading.Tasks.Task.Delay(1000);
}
await mqttClient.DisconnectAsync();
}
}