AWS IoT With .NET Core (MQTT)

Introduction

We can publish and subscribe messages on AWS IoT core by establishing a connection to it.

There are multiple options available for publishing and subscribing messages with AWS IoT Core. The message broker supports the use of the MQTT protocol to publish and subscribe and the HTTPS protocol to publish. You can check this link for information about all the protocols supported by AWS IoT.

In this post, we’ll cover the option of leveraging ClientCertificate and MQTT protocol to work with AWS IoT Core.

Following are the Items we’ll cover in this post:

  • .NET Core Console Application with Nuget package for MQTT Client.
  • Convert certificate(s) to pfx format and use in .NET Core application.
  • Connect to AWS IoT from our application.
  • Publish Messages to AWS IoT.
  • Subscribe to Topics and receive messages.

Like previous post, we will not be touching the physical device (thing) but instead see the process in an application.

Setting The Scene

In previous post, we setup an AWS IoT thing, learn about different certificates and then created a Nodejs application to push data to AWS IoT.

I will be using same device (Thing), thing certificates and same AWS IoT setup. So nothing we will change on AWS Side, we will reuse the same setup from previous post. If you are new to these topics, I will suggest to first check the previous post for AWS IoT introduction, things setup and certificates part.

However, instead of Nodejs, this time, we will use a .NET Core Console application as a client.

Create .NET Core Console Application

I started by creating a .NET Core Console Application and added the M2Mqtt nuget package as shown below:

Certificates Setup

As mentioned before, I have these certificates from previous post.

An initial test (optional), we can do, to confirm that we are able to connect to AWS IoT using these certificates is by using opessl tool as shown below:

openssl s_client -CAfile AmazonRootCA1.pem -cert certificate.pem.crt -key private.pem.key -connect a2upl3efg7dvex-ats.iot.eu-central-1.amazonaws.com:8883

We will see some output similar to following showing that connection is successfull.

Converting Device Certificate from .pem to .pfx

In Nodejs demo application, we were able to use these certificates straight-forward. However, there is some extra work involved in .NET.

In order to establish an MQTT connection with the AWS IoT platform, the root CA certificate, private key of the thing and the certificate of the thing are needed. The .NET Cryptographic APIs can understand root CA (.crt) and device private key (.key) out-of-the-box. It expects the device certificate to be in the .pfx format, not in the .pem format. Hence we need to convert the device certificate from .pem to .pfx:

openssl pkcs12 -export -in certificate.pem.crt -inkey private.pem.key -out certificate.cert.pfx -certfile AmazonRootCA1.pem

This will ask for an export password as well:

I will not go into details of these command. You can check this post for more details about openssl.

MqttClient Code to Connect to AWS

The following screenshot shows a very simple implementation of C# code for MQTT connection to AWS IoT:

As you can see, code is very straight-forward and easy to follow. We are using MqTTClient object to connect to AWS IoT by providing it certificates and once connection is made, we can simply publish to the topic.

You can download the source-code from this git-repository.

Testing

With above code in place, we can simply run the .NET Core Console application and it will shows us following output:

on the AWS Side, we can go to Test section on AWS IoT Dashboard and subscribe to the topic (topic_2) where we are sending messages to and as you can see that messages from our .NET Core application are being received as expected:

Subscribing to Messages

Similar to publishing, the library provides a nice way to subscribe to messages.

First, I added following two event-handlers:

As name implies these handler methods will be executed when we subscribed to a topic or when we received a message.

Next, I wired-up these handlers with MQTT client as shown below:

and finally we can subscribe to topic as shown below:

With all these changes, we can execute the application and following output shows that event-handler code related to subscription was fired as expected.

Now, if our application is listening for messages on the topic. We can go to AWS IoT Test page and publish a message to this topic as shown below:

and as our .NET Core application is listening for this topic, we will see that corresponding Event was fired and handler code was executed as expected:

Summary

In this post, we learned how to connect to AWS IoT Core from a .NET Core Application by using MQTT protocol. We also saw basic examples of publishing messages from our application to AWS IoT and also subscribing to the topics and receive and handles messages.

M2MQTT is the NuGet package which we can used for this purposes. You can also check this GitHub link for more examples using this library.

There is also another popular MQTTNet client library, however I had difficulty in using it with AWS IoT.

We will explore AWS IoT more in coming posts. Let me know if you have some questions or comments. Till next time, Happy Coding.

My Recent Books


Discover more from Hex Quote

Subscribe to get the latest posts sent to your email.

1 thought on “AWS IoT With .NET Core (MQTT)”

Comments are closed.

Discover more from Hex Quote

Subscribe now to keep reading and get access to the full archive.

Continue reading