How to implement AWS IoT core communication in .NET Framework application using MQTT?
How to implement AWS IoT core communication in .NET Framework application using MQTT?
Posted in by Tudipat 05:03Comments Off on How to implement AWS IoT core communication in .NET Framework application using MQTT?
How to implement AWS IoT core communication in .NET Framework application using MQTT?
11 March 2021
We can publish and subscribe messages on AWS IoT core by establishing a connection to it. In .Net Framework we have multiple options for connecting to AWS IoT core.
Below are the multiple protocols and the ports they support for communicating with AWS IoT core.
Here, we will use MQTT with WebSocket protocol for handshake with AWS IoT core using Signv4 authentication. We will use MQTTnet library for the publishing and subscribing messages on AWS IoT core.
Benefits of using MQTTnet Library
It is a high performance .Net library for MQTT based communication.
Provides both MQTT client and a MQTT broker (server).
It provides an auto-reconnect facility if the connection is aborted with MQTT Broker.
Has an improved behavior incase of multiple connections over a session.
Creating an MQTT client instance and then allowing connection through MOTT client to the MQTT server.
Referring to above code, for creating an instance of MQTT client, CreateMqttClient() function is invoked using MqttFactory object.
Inorder to connect to broker the MQTT client needs the signedRequestUrl.
This signedRequestUrl is generated using 4 things: Host (Iot Host Url), Region, AccessKey and Secret Key.
Code for generating signedRequest Url:
Using signedRequestUrl and the clientId the MqttClient is connected to the Mqtt Broker on port 443.
After connecting, we can publish messages to Mqtt Broker. Code for publishing messages to Mqtt Broker: For publishing the message, we need the topic on which the message is to be published. Here payload is the message.
We can also subscribe to messages from the Mqtt Broker. Code used for subscribing messages:
Handle Events for MqttClients:
Connected Handler:
Disconnected Handler:
Handler for message received after subscribing to messages.
All content provided on this blog is for informational purposes only. Tudip Technologies provides no endorsement and makes no representations as to accuracy, reliability, completeness, suitability or validity of any information or content on, distributed through or linked, downloaded or accessed from this site. Tudip Technologies will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use of the information on this site. All information is provided on an as-is basis without any obligation to make improvements or to correct errors or omissions. This site may contain links to other websites. Tudip Technologies makes no guarantees or promises regarding these websites and does not necessarily endorse or approve of their content. You may not modify any part of the blog. The inclusion of any part of this blog in another work, whether in printed or electronic or other form, or inclusion of any part of the blog in another website by linking, framing or otherwise without the express permission of Tudip Technologies is prohibited. This site may not be used for any illegal or illicit purpose and Tudip Technologies reserves the right, at its sole discretion and without notice of any kind, to remove anything posted to this site. By using this site, you hereby acknowledge that any reliance upon any materials shall be at your sole risk.