I would like to get some advice regarding MQTT.
I have one AWS IoT account and one device registered
I tested the Publish and Subscribe using MQTT Fx and it works just fine.
But can’t get it work with tinyCLR
Connection seems ok.
I can subscribe to the topic
The first time I tried to publish, I do not get any exception but data never appear in AWS.
The second attempt is done less than 2 min later. At this time, i get this exception in the console
The thread ‘’ (2) has exited with code 0 (0x0).
ConnectedChanged
Connected to MeteoDevice
Data published to MeteoData topic
Connected to AWS MQTT Broker
#### Exception System.IO.IOException - 0x00000000 (13) ####
#### Message:
#### System.Net.Security.SslStream::Write [IP: 00e4] ####
#### GHIElectronics.TinyCLR.Networking.Mqtt.MqttStream::Send [IP: 0012] ####
#### GHIElectronics.TinyCLR.Networking.Mqtt.Mqtt::Send [IP: 0008] ####
#### GHIElectronics.TinyCLR.Networking.Mqtt.Mqtt::ProcessPacketsThread [IP: 0089] ####
#### Exception System.Exception - 0x00000000 (13) ####
#### Message: Sending failed.
#### GHIElectronics.TinyCLR.Networking.Mqtt.Mqtt::Send [IP: 0026] ####
#### GHIElectronics.TinyCLR.Networking.Mqtt.Mqtt::ProcessPacketsThread [IP: 0089] ####
The thread ‘’ (13) has exited with code 0 (0x0).
ConnectedChanged
The thread ‘’ (12) has exited with code 0 (0x0).
The thread ‘’ (11) has exited with code 0 (0x0).
Data published to MeteoData topic
Code used is below.
Any advice ?
Mqtt iotClient;
var iotEndPoint = "************.iot.us-west-2.amazonaws.com";
var iotPort = 8883;
var deviceId = "MeteoDevice";
var caCertSource = Resources.GetBytes(Resources.BinaryResources.AmazonRootCA1);
var clientCertSource = Resources.GetBytes(Resources.BinaryResources._*ee1794677_certificate_pem);
var privateKeyData = Resources.GetBytes(Resources.BinaryResources._*aee1794677_private_pem);
X509Certificate CaCert = new X509Certificate(caCertSource);
X509Certificate ClientCert = new X509Certificate(clientCertSource)
{
PrivateKey = privateKeyData
};
var clientSetting = new MqttClientSetting
{
BrokerName = iotEndPoint,
BrokerPort = iotPort,
CaCertificate = CaCert,
ClientCertificate = ClientCert,
SslProtocol = SslProtocols.Tls12
};
iotClient = new Mqtt(clientSetting);
iotClient.PublishReceivedChanged += _iotClient_PublishReceivedChanged;
iotClient.SubscribedChanged += IotClient_SubscribedChanged;
iotClient.ConnectedChanged += _iotClient_ConnectedChanged;
iotClient.PublishedChanged += OnIotClientOnPublishedChanged;
;
var connectSetting = new MqttConnectionSetting
{
ClientId = deviceId,
KeepAliveTimeout = 200
};
iotClient.Connect(connectSetting);
long packetId = 1;
iotClient.Subscribe(new string[] { "MeteoData" }, new QoSLevel[] { QoSLevel.ExactlyOnce },
(ushort)packetId++);
if (iotClient.IsConnected)
{
var Message = "{\"timestamp\": \"2025-03-04T12:55:28Z\", \"temperature\": 21.9, \"humidity\": 37.1,\"pressure\": 101.7}";
iotClient.Publish("MeteoData", Encoding.UTF8.GetBytes(Message), QoSLevel.MostOnce, false,
(ushort)packetId++);
Debug.WriteLine("Data published to MeteoData topic");
}
}