How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

Reading Time: 12 minutes

AWS IoT provides cloud services that connect your IoT devices to other devices and AWS cloud services. If your devices can connect to AWS IoT, AWS IoT can connect them to the cloud services that AWS provides. AWS IoT lets you select the most appropriate and up-to-date technologies for your solution. In this blog, we will follow an interactive demo tutorial provided by Amazon IoT core service to get started with the usage of the service. We will then navigate to the quick connect dashboard thereby creating a new thing object, selecting a platform and a device SDK. After the configuration, we will download the files, and using a few commands, we will connect to the local machine using the AWS IoT dashboard to send and receive messages thereby enabling an MQTT – Message Queuing Telemetry Transport stream of messages. Finally, we will look at the various interaction options, the policies, the security aspects, and the certificates created for the newly created thing object.

Refer to Part 1 of the blog here: How to connect smart devices to the Amazon IoT Core service and watch it send MQTT messages (Part 1)?

Hands-on

In this hands-on, we will see how we can make use of the Amazon IoT Core service to connect to your local device. AWS IoT provides cloud services that connect your IoT devices to other devices and AWS cloud services. AWS IoT provides device software that can help you integrate your IoT devices into AWS IoT-based solutions. We will explore the various services offered by the Amazon IoT Core service. Navigating to the Amazon IoT Core learning dashboard, we will follow a 6-step process tutorial to understand the working using a bulb example. We will navigate to the Connect to the device via AWS IoT quick connect dashboard. We will then create a thing object. After the creation of a thing object, we will then choose a platform for our connection and an AWS IoT Device SDK. Downloading the files on the local machine and unzipping them, we will execute the various commands to connect the AWS IoT to the local machine where the files exist. Finally, we will set up an MQTT – Message Queuing Telemetry Transport stream by sending messages from the AWS IoT dashboard and receiving them on the local machine thereby testing the connection. On success, we will then explore the various details of the thing object and look at the various interaction possibilities.

To implement this, we will do the following:

  • Login to your AWS console and navigate to the AWS console.
  • Navigate to the Amazon IoT Core dashboard using the link provided.
  • Explore the Amazon IoT Core dashboard for its various features and services.
  • Navigate to the Learning dashboard of Amazon IoT Core.
  • Start the tutorial following a 6-step process.
  • Try out the Device Gateway, Rules Engine, Rule Actions, Device Shadows and Build solutions using the Bulb example.
  • Navigate to the quick connect dashboard and get to the Connect to AWS IoT.
  • Create a thing object.
  • Select a platform and an AWS IoT Device SDK.
  • Download files onto your local device.
  • Unzip the files and execute the various connection commands provided by the Amazon IoT core service.
  • Send messages via the Amazon IoT core dashboard and check the connection by receiving the messages on your Local device on which you executed the various commands and the script.
  • Explore the edit thing object dashboard.
  • Look at the thing object ARN that can be used for integration and the Security aspect as well as the certificates used for your thing object.
  • Explore the policies attached and edit the policy (if needed) as per your requirements.
  • Explore the shadows created and edit a shadow (if needed) as per your requirements.
  • Finally, look at the HTTPS API Endpoint interaction using which we can update the thing shadow.

Login to your AWS console and navigate to the dashboard.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

Now, navigate to https://console.aws.amazon.com/iot/home using the link.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

Click on Learn in the left navigation pane and you will see the dashboard as shown in the image below. First, let’s navigate to the AWS IoT tutorial to have a look at how it works.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

You can read the steps you’ll perform to start with this tutorial. Now, click on Start Tutorial.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

You will then see a 6-step process for the tutorial. The first step is connecting to the Device Gateway. You can read about Device Gateway on the right side of the page.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

Now, to test it out, click on the G button to check the effect. Once you click on it, the color of the bulb will change to green which shows that the device was successfully connected.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

Now, if you click on B, the color of the bulb will change to blue. Similarly, you can try out the R button as well.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

To proceed, scroll down and click on Next.

You will then be navigated to level 2 ie. Rules Engine. You will see that there is a switch to toggle.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

Keep the switch on and then click on the B button. You’ll notice that the color will change to Green and not Blue. That’s because the switch is on and the switch is configured to change the Blue color on pressed to be Green.

Now toggle the switch off and then press B and you’ll see the bulb colour changing to blue.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

Now navigate to the third section of the tutorial. Over here, you will see two circuits connected along with two switches to be toggled on and off.

Initially, we will keep both the connections on. The second connection on the right is for sending messages to a mobile device and to store the output in a database.

Click on the R button and then the bulb will turn to Red and along with that, you will see the current flowing on the other end of the second circuit as well.

Now, since there second switch is on too, click on the B button and you will see the flow 

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

The bulb will then turn green because of the second switch being turned on.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

Now, navigate to the 4th section of the tutorial. ie. Device Shadows.

Click on the G button and you will see the current flow on both ways. ie. On the shadow bulb as well as the normal bulb.

Now, click on the R button. You will see the current flowing into both the circuits as well as the shadow bulb.

The bulbs will now turn to Red and the database and the notification connected circuit will show a transition effect.

Keeping the switches on, click on the B button.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

The bulb’s color will now change to green because of the colour transition switch turned on.

Finally, moving to the last section of the tutorial. Here, we have an external device connected to the circuits.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

Initially, clicking on the B button from the generator will change the colour of the bulb to Green because of the switch toggled on.

Now, when we click on the R button from the mobile device that’s connected, you will see that the current flow direction changes and is moving to all the circuits.

On success, you will see the colour of the bulb changed to Red.

Now, when you click on the B button from the device, the bulb colour will change to blue since one switch is toggled off.

Note, if you toggle the other switch to off as well, you will see that connection to the notification carrier and database will disappear, which means no current will flow through that circuit now.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

Click on the R button from the device, the colour of all the bulbs will change to red.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

Now, since we are done with the tutorial, scroll down and click on Get Started.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

You can register a thing object by entering a name for the object or you can follow the quick connect process as shown after this step.

Navigate to the learning dashboard and click on View connection options under Connect to AWS IoT.

On the next dashboard, click on get started under onboard a device.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

Over here, you can see the various steps we will perform. You can have a look at the various details for each step.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

In the next step, choose a platform and an AWS IoT device SDK.

Scroll down, click on Next to proceed to the next step.

Over here, we need to register a thing. This thing object will be used to connect the IoT service to your device.

Enter a name for your thing object. It could be anything as per your choice.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

You can expand the optional configuration option on the dashboard and make the changes required as per your need.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

On success, you will see the message was shown in the image below and a thing object will be created.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

Click on the download the kit option to get the kit on your device. On success, you will see the downloaded kit as shown in the image below.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

If you open the zip file, you will see the corresponding files in the file structure.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

Scroll down on the dashboard and click on the next step.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

Now, we need to use the below commands to connect our device to the service.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

Let’s start by using the first command to unzip the files in a folder firstly. Copy the first command.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

Open Powershell or command prompt in a folder wherein you will store the files. Paste the command into the shell and press enter.

On success, you will see the files extracted in the folder structure as shown in the image below.

Now, copy the second command from the AWS IoT service dashboard.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

Paste the command into the shell since we need to provide the executable writes to the start.sh script for its execution.

Finally, copy the third command from the dashboard.

This command is used to initiate the script execution. On success, you will see that the connection setup initiates successfully.

After the AWS SDK installation, you will see a message stating the running status of the sample application.

Now if you navigate back to the folder, you will see the file structure as shown in the image below.

Once the script execution is successful, you will see that the connection to your device will succeed on the dashboard. Now you will see a new text box that appears for us to send messages to the device.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

Enter a message in the text box as shown in the image below. Then click on Send me.

If you notice, you’ll see the message sent from the console being received on your device as shown in the image below.

Let’s send a new message from the console now and check its receival status.

As shown in the image below, you will see the message being received by your device.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

Once tested, click on done.

Finally, on success, you will see the dashboard as shown in the image below.

Scroll down and click on Done to exit from the dashboard.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

Once done, you will see your new thing object in the dashboard as shown in the image below.

Click on the thing object to look at its details. Over here you can find the ARN for your thing object.

Click on security in the left navigation pane. You will see a certificate already existing. You can also create new certificates. Click on the existing certificate.

You will find the ARN for the certificate which you can use for the integration. You will also find other details as shown in the image below. Now click on policies on the left navigation pane.

You will see a new policy that is attached to your thing object. You can even edit the policy document.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

If you click on versions, you will see the various versions created for your thing object.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

If you click on shadows, you will give a default shadow created. You can even edit or as a new shadow. Click on the existing shadow.

Over here you will find the shadow ARN as well as the shadow document.

Finally, in the last section, if you navigate to interact, you will see the HTTPS API endpoint which you can use to recite your shadow.

How to connect a device to the Amazon IoT Core service and watch it send MQTT messages?

Conclusion

In this blog, we saw how we can make use of the Amazon IoT Core service to connect to the local device. We even explored the various services offered by the Amazon IoT Core service. Navigating to the Amazon IoT Core learning dashboard, we followed a 6-step process tutorial to understand the working using a bulb example. We then navigated to the Connect to the device via AWS IoT quick connect dashboard and created a thing object. After the creation of a thing object, we chose a platform for our connection and an AWS IoT Device SDK. Downloading the files on the local machine and unzipping them, we then executed the various commands to connect the AWS IoT to the local machine where the files exist. Finally, we set up an MQTT – Message Queuing Telemetry Transport stream by sending messages from the AWS IoT dashboard and receiving them on the local machine thereby testing the connection. On success, we then explored the various details of the thing object and had a look at the various interaction possibilities. We will discuss more use cases of Amazon IoT Core in our upcoming blogs. Stay tuned to keep getting all updates about our upcoming new blogs on AWS and relevant technologies.

Meanwhile …

Keep Exploring -> Keep Learning -> Keep Mastering

This blog is part of our effort towards building a knowledgeable and kick-ass tech community. At Workfall, we strive to provide the best tech and pay opportunities to AWS-certified talents. If you’re looking to work with global clients, build kick-ass products while making big bucks doing so, give it a shot at workfall.com/partner today.

Back To Top