im trying to connect my Broker MQTT Event Grid to ESP32 using .NET nano framework support from cloning repository from Mr. Sander here : https://github.com/sandervandevelde/Nanoframework-Cloud-MQTT-Broker-support/
in the file Program.cs i filled the SSID, password, Broker Hostname, DeviceID, and the topic is just the same and i have configured the TopicsSpaces, permission bindings, and all of them.
but still get this error :
Visual Studio Debug
What should i do to solve this technical problem?
using nanoFramework.M2Mqtt.Messages;
using nanoFramework.M2Mqtt;
using System;
using System.Diagnostics;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Net.NetworkInformation;
using System.Text;
namespace NFMqttClientApp
{
public class Program
{
private const string c_SSID = "MjHome";
private const string c_AP_PASSWORD = "17102002";
private static MqttClient device = null;
private const string c_DEVICEID = "nanoTestDevice";
private const string c_USERNAME = c_DEVICEID;
private const string c_BROKERHOSTNAME = "trjtbroker.southeastasia-1.ts.eventgrid.azure.net";
private const int c_PORT = 8883;
private const string c_PUB_TOPIC = "message/fromnanoframework";
// private const string c_PUB_TOPIC_LWT = "message/fromnanoframework/alert";
private const string c_SUB_TOPIC = "message/device/#";
public static void Main()
{
//// ROOT TLS cert is taken from DigiCert Global Root G3: https://www.digicert.com/kb/digicert-root-certificates.htm
Debug.WriteLine("Hello from nanoFramework MQTT cLient!");
// Wait for Wifi/network to connect (temp)
SetupAndConnectNetwork();
var caCert = new X509Certificate(ca_cert);
var clientCert = new X509Certificate2(client_cert, client_key, string.Empty);
device = new MqttClient(c_BROKERHOSTNAME, c_PORT, true, caCert, clientCert, MqttSslProtocols.TLSv1_2);
device.ProtocolVersion = MqttProtocolVersion.Version_5;
TryToConnect();
device.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;
device.MqttMsgSubscribed += Client_MqttMsgSubscribed;
device.ConnectionOpened += Client_ConnectionOpened;
device.ConnectionClosed += Client_ConnectionClosed;
device.ConnectionClosedRequest += Client_ConnectionClosedRequest;
device.MqttMsgUnsubscribed += Client_MqttMsgUnsubscribed;
device.Subscribe(new[] { c_SUB_TOPIC }, new[] { MqttQoSLevel.AtLeastOnce });
var counter = 0;
while (true)
{
string payload = $"{{"counter":{counter++}}}";
var result = device.Publish(c_PUB_TOPIC, Encoding.UTF8.GetBytes(payload), "application/json; charset=utf-8", null);
Debug.WriteLine($"Message sent ({result}): {payload}");
Thread.Sleep(10000);
}
}
private static void TryToConnect()
{
bool connected = false;
while (!connected)
{
try
{
// Regular connect
var resultConnect = device.Connect(c_DEVICEID, c_USERNAME, "");
// Connect with Last will & testament. At this moment, willRetain is not supported yet by the EventGrid so keep that at 'false'.
// var resultConnect = device.Connect(c_DEVICEID, c_USERNAME, "", false, MqttQoSLevel.AtLeastOnce, true, c_PUB_TOPIC_LWT, "unexpected disconnect", true, 30);
if (resultConnect != MqttReasonCode.Success)
{
Debug.WriteLine($"MQTT ERROR connecting: {resultConnect}");
// device.Disconnect();
Thread.Sleep(1000);
}
else
{
Debug.WriteLine(">>> Device is connected");
connected = true;
}
}
catch (Exception ex)
{
Debug.WriteLine($"MQTT ERROR Exception '{ex.Message}'");
// device.Disconnect();
Thread.Sleep(1000);
}
}
}
private static void Client_ConnectionClosedRequest(object sender, ConnectionClosedRequestEventArgs e)
{
Debug.WriteLine("Client_ConnectionClosedRequest");
}
private static void Client_ConnectionClosed(object sender, EventArgs e)
{
Debug.WriteLine("Client_ConnectionClosed");
// TryToConnect();
}
private static void Client_ConnectionOpened(object sender, ConnectionOpenedEventArgs e)
{
Debug.WriteLine("Client_ConnectionOpened");
}
private static void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
Debug.WriteLine("Client_MqttMsgPublishReceived");
Debug.WriteLine($"Topic: {e.Topic}");
var message = Encoding.UTF8.GetString(e.Message, 0, e.Message.Length);
Debug.WriteLine(message);
}
private static void Client_MqttMsgSubscribed(object sender, MqttMsgSubscribedEventArgs e)
{
Debug.WriteLine("Client_MqttMsgSubscribed");
Debug.WriteLine($"Message identifier: {e.MessageId} of subscribed topic");
}
private static void Client_MqttMsgUnsubscribed(object sender, MqttMsgUnsubscribedEventArgs e)
{
Debug.WriteLine("Client_MqttMsgUnsubscribed");
}
public static void SetupAndConnectNetwork()
{
NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();
if (nis.Length > 0)
{
// get the first interface
NetworkInterface ni = nis[0];
if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
{
// network interface is Wi-Fi
Debug.WriteLine("Network connection is: Wi-Fi");
Wireless80211Configuration wc = Wireless80211Configuration.GetAllWireless80211Configurations()[ni.SpecificConfigId];
if (wc.Ssid != c_SSID && wc.Password != c_AP_PASSWORD)
{
// have to update Wi-Fi configuration
wc.Ssid = c_SSID;
wc.Password = c_AP_PASSWORD;
wc.SaveConfiguration();
}
else
{ // Wi-Fi configuration matches
}
}
else
{
// network interface is Ethernet
Debug.WriteLine("Network connection is: Ethernet");
ni.EnableDhcp();
}
// wait for DHCP to complete
WaitIP();
}
else
{
throw new NotSupportedException("ERROR: there is no network interface configured.rnOpen the 'Edit Network Configuration' in Device Explorer and configure one.");
}
}
static void WaitIP()
{
Debug.WriteLine("Waiting for IP...");
while (true)
{
NetworkInterface ni = NetworkInterface.GetAllNetworkInterfaces()[0];
if (ni.IPv4Address != null && ni.IPv4Address.Length > 0)
{
if (ni.IPv4Address[0] != '0')
{
Debug.WriteLine($"We have an IP: {ni.IPv4Address}");
break;
}
}
Thread.Sleep(500);
}
}
#region Certificates (include BEGIN and END)
private const string client_cert = @"-----BEGIN CERTIFICATE-----
MIIB9DCCAZmgAwIBAgIQY9/VCX7KyUAT5nAZo5uZ5DAKBggqhkjOPQQDAjBOMR0w
GwYDVQQKExRNcXR0QmxvZ0FwcFNhbXBsZXNDQTEtMCsGA1UEAxMkTXF0dEJsb2dB
cHBTYW1wbGVzQ0EgSW50ZXJtZWRpYXRlIENBMB4XDTI0MDYwMjE2MzMxM1oXDTI0
MDkxMDE2MzMxMFowGTEXMBUGA1UEAxMObmFub1Rlc3REZXZpY2UwWTATBgcqhkjO
PQIBBggqhkjOPQMBBwNCAAQ9kDNYJzBfPonixJeoCQDfHgT9H3elhtft0ibSNzDA
Z4bhzYAfh1QO0lyzQUg4+AxExGYQFSYXd3QV+wDn7Uomo4GNMIGKMA4GA1UdDwEB
/wQEAwIHgDAdBgNVHSUEFjAUBggrBgEFBQcDAQYIKwYBBQUHAwIwHQYDVR0OBBYE
FPh34Lqdm++7AzMK76xS2jA7zBAqMB8GA1UdIwQYMBaAFLK49M9EsK2kgNpccO0h
D1AW1FssMBkGA1UdEQQSMBCCDm5hbm9UZXN0RGV2aWNlMAoGCCqGSM49BAMCA0kA
MEYCIQDfqSZVXl6wD5WFwQ9fqeb7jzj0PYgjt/k5WDequ6bnLQIhAN9aJb2xOwkr
bU2yIyPngtJlhy2LiemxQKLOp2SEKOlh
-----END CERTIFICATE-----";
private const string client_key = @"-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIAe+q67Rbm7BT9mF4TkujC3lUK5xqhFh6SF9y/t8HVgWoAoGCCqGSM49
AwEHoUQDQgAEPZAzWCcwXz6J4sSXqAkA3x4E/R93pYbX7dIm0jcwwGeG4c2AH4dU
DtJcs0FIOPgMRMRmEBUmF3d0FfsA5+1KJg==
-----END EC PRIVATE KEY-----";
private const string ca_cert = @"-----BEGIN CERTIFICATE-----
MIIB0DCCAXWgAwIBAgIQDftM4qqQ3c6WoVGVAA3zoDAKBggqhkjOPQQDAjBGMR0w
GwYDVQQKExRNcXR0QmxvZ0FwcFNhbXBsZXNDQTElMCMGA1UEAxMcTXF0dEJsb2dB
cHBTYW1wbGVzQ0EgUm9vdCBDQTAeFw0yNDA0MjgxNDQ2MzdaFw0zNDA0MjYxNDQ2
MzdaMEYxHTAbBgNVBAoTFE1xdHRCbG9nQXBwU2FtcGxlc0NBMSUwIwYDVQQDExxN
cXR0QmxvZ0FwcFNhbXBsZXNDQSBSb290IENBMFkwEwYHKoZIzj0CAQYIKoZIzj0D
AQcDQgAEnvA/p0MI15C+ylXzB6r2qVKGhw3BW6RVFBZPmq8k8NPKrvnu8XiLQPSe
aE7ntG0RF3TMU4AGiammOqBPaxnhLaNFMEMwDgYDVR0PAQH/BAQDAgEGMBIGA1Ud
EwEB/wQIMAYBAf8CAQEwHQYDVR0OBBYEFBwpRK7caklVWZTo0WtQbSbWoVO0MAoG
CCqGSM49BAMCA0kAMEYCIQCZ/BhNeK3muvQYs6FntCeLikgcsZsFHOm9sE1EWeqx
ygIhALfuCbSoSX+zYL49bA+pxkRG2mr/4Vr4vW2lzs9tmyEm
-----END CERTIFICATE-----";
#endregion
}
}
here’s the full code i’ve used, the certificate im using is pem,key, and for ca_Cert im using root_ca when creating the Device Client (nanoTestDevice)