Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

CPP Usage

Mikkel Mork Hegnhoj edited this page Feb 17, 2016 · 2 revisions

Get started with Azure IoT Hub and Visual Studio connected services (C++ Classic Apps)

Congratulations on setting up an IoT Hub and connecting your device using Visual Studio Connected Services!

Now that you have connected your device to an IoT Hub, you're ready to start enjoying the flow of data and control of your device through Azure IoT Hub.

Changes In Your Visual Studio Project

New files azure_iot_hub.cpp and `azure_iot_hub.h' have been added to your Visual Studio project. This files contain the boiler-plate code that you can immediately invoke in your application.

Using azure_iot_hub.cpp

The file azure_iot_hub.cpp contains two methods that you can start using right away from your own classes:

  • A method to send messages to the IoT Hub - send_device_to_cloud_message()
  • A method to start listening to messages from the IoT Hub - receive_cloud_to_device_message()

The methods are declared in azure_iot_hub.h header file. You can #include the header file and call these methods from elsewhere in your project.

Protect Your Connection Details!

For your convenience, the Azure IoT Hub Connected Service has inserted the connection_string variable that contains the access key required to connect to your instance of IoT Hub on behalf of the chosen device. Keep in mind that anyone who comes into the possession of this information will be able to send and receive messages on behalf of that device. Therefore, it is recommended that you remove this string from the source code before committing your code into a source control. Consider storing it in a configuration file, an environment variable, or other solutions.

Verifying Connectivity and Messages

In order to verify your devices connectivity, you can use a tool called IoT-Hub Explorer. This is a cross-platform CLI tool based on Node.js. If Node.js is not installed on your system you should install it now https://nodejs.org/en/download/.

Then, get the tool via npm:

npm install -g iothub-explorer@latest

The IoT-Hub Explorer tool needs the connection string to connect to your instance of the Azure IoT Hub. The Azure IoT Hub Connected Services has already generated the full command line string for monitoring messages sent to your instance of IoT Hub. You can find it at the beginning of the generated azure_iot_hub.cpp file:

//
// To monitor messages sent to device "test" use iothub-explorer as follows:
//    iothub-explorer <connection string> monitor-events "test"
//

Now paste that string into your command line. You will see a message that says events from your device are being monitored. Now run your application. You should see an event received in the console.

To learn about the additional capabilities of the IoT Hub Explorer, refere to this article: https://github.com/Azure/azure-iot-sdks/blob/master/tools/iothub-explorer/readme.md

Note On UWP C++ Apps

To receive messages from Azure IoT Hub in UWP C++ application, you need to enable C++ await feature. This feature can be enabled with the /await compiler switch.

Further reading

You can find out more about IoT Hub here: