Click here to Skip to main content
15,879,535 members
Articles / Hosted Services / Azure
Article

MQTT Publication with Microsoft Azure

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
26 Apr 2018CPOL3 min read 7.1K   2  
This tutorial takes you from setting up a device in Microsoft Azure* all the way to viewing your data (sent via MQTT).

This article is for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers

Programming language C++ (Arduino*)
Skill level Intermediate
Time to complete 5 minutes
Hardware needed UP Squared* Grove* IoT Development Kit
Target Operating System Ubuntu 16.04

This tutorial takes you from setting up a device in Microsoft Azure* all the way to viewing your data (sent via MQTT). The sketch is setup to read data from the Grove* Rotary Angle Sensor (at a given interval) and send it to the Microsoft Azure* cloud platform. The final step where you view your data is done in the Device Explorer* tool provided by Microsoft*. It is only supported on Windows* , see https://github.com/Azure/iothub-explorer for instructions on how to use the iot-hubexplorer node.js command line tool for other platforms.

What you'll learn

  • How to set up an IoT Hub
  • How to get the credentials for your device and enter them into the "Secrets" tab in Arduino Create*
  • How to view your data using the Device Explorer tool

Gather your materials

Software dependencies

  • libcrypto
  • libssl

These will already be installed if you are using the OS that was shipped with the UP Squared* Grove* IoT Development Kit. Otherwise you need to install them via apt-get.

$sudo apt-get install libssl-dev

To check if they are already installed :

$ldconfig –p | grep libssl

$ldd $(which ssh) | grep libcrypto

Plug in your sensor

Connect the Grove Rotary Angle Sensor to pin A0 on the Grove Pi+

Create an account

If you don't have a Microsoft Azure account, sign up for a free trial https://azure.microsoft.com/en-us/free/

Create an IoT Hub

You need an IoT Hub to enable communication between Microsoft Azure and your device. The easiest way to create an IoT Hub is through the Microsoft Azure portal. Follow the steps in https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-create-through-portal.

The portal is located at https://portal.azure.com

  1. Click "New" and search for "iot"

Image 1

  1. Name your hub something unique, for example my-iot-hub###, where ### is some number. It is recommended to use the default settings for the IoT hub creation.

Image 2

When it's done being created you should see it appear in the Dashboard.

Image 3

Create a shared access policy

  1. Open your hub and select Shared access policies, under SETTINGS.

Image 4

  1. Click Add.

Image 5

  1. Select read, service connect, and device connect permissions. Give it a name such as "my-policy".

Image 6

  1. The policy you created should now show up under POLICY.

Image 7

  1. Click on your policy and copy the Connection string - primary key (also known as the Hub connection string). You will need it later.

Image 8

Setup device explorer

View your data using either the Device Explorer tool or the iot-hubexplorer node.js command line tool (supported on Windows, macOS, and Linux*).

This tutorial will you walk you through using the Device Explorer GUI on Windows. See https://github.com/Azure/iothub-explorer for instructions on how to use the iot-hubexplorer node.js command line tool.

  1. Download and install the SetupDeviceExplorer.msi from https://github.com/Azure/azure-iot-sdk-csharp/releases.

  2. Enter the Connection string - primary key you copied from step #5 of the above section into the IoT Hub Connection String field in the Configuration tab of the application. Then click Update. You should see a popup confirming that it was successful.

Image 9

  1. Go to the Management tab. Image 10

  2. Click Create and enter a Device ID, for example "test-device". The Primary and Secondary keys should have populated automatically.

Image 11

  1. Right click on the device you just created, and select Copy connection string for selected device

Image 12

Get the code

  1. Open the example Libraries>AZURE IOT DEVICE SDK>Examples>ClientMqtt

Image 13

  1. Copy the connection string from step #7 above into the Secret tab of the sketch.

Image 14

  1. Build and Upload the sketch.

View the data

Go to the Data tab and click Monitor. You should see a message saying "Receiving events" or similar. This is where you will see messages once your application is running on your target.

Image 15

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
You may know us for our processors. But we do so much more. Intel invents at the boundaries of technology to make amazing experiences possible for business and society, and for every person on Earth.

Harnessing the capability of the cloud, the ubiquity of the Internet of Things, the latest advances in memory and programmable solutions, and the promise of always-on 5G connectivity, Intel is disrupting industries and solving global challenges. Leading on policy, diversity, inclusion, education and sustainability, we create value for our stockholders, customers and society.
This is a Organisation

42 members

Comments and Discussions

 
-- There are no messages in this forum --