Opta™ with Azure IoT

Learn to connect your Opta™ to the Microsoft Azure Cloud

Introduction

This tutorial demonstrates the integration of Opta™ with Azure IoT. You will proceed to create a reliable and secure IoT environment, harnessing the power of these two technologies.

Goals

  • Initial Configuration: Guide the user through the initial setup steps for Azure IoT and IoT Hub to establish a connection between Opta™ and Azure IoT.

  • Reliable Transmission: How to reliably transmit data from Opta™ to Azure IoT.

Required Hardware and Software

Required Hardware

Required Software

Azure IoT

Azure IoT is a set of advanced Cloud services provided by the Microsoft Azure platform, designed to facilitate the development, management, and connectivity of IoT solutions. The goal of Azure IoT is to enable developers to build scalable, secure, and reliable IoT applications, transforming device data into meaningful insights and intelligent actions.

One of the key components of Azure IoT is IoT Hub, which plays a crucial role in managing connectivity between IoT devices and Cloud applications. The core features of IoT Hub include:

  • Connection Management: IoT Hub facilitates secure and bidirectional connection management between IoT devices and the Cloud. It supports various communication protocols to adapt to the diverse needs of devices.

  • Automatic Scaling: IoT Hub can easily handle a large number of devices dynamically and automatically, ensuring reliable connectivity even with a vast network of devices.

  • Advanced Security: It provides robust security mechanisms, including end-to-end encryption, device authentication, and key management, to protect data transmitted between devices and Azure.

  • Remote Control: Enables Cloud applications to send commands to devices and receive responses, facilitating remote control and automation of connected devices.

  • Device Management: Provides device management functionalities such as registration, state monitoring, and lifecycle management, simplifying the operational management of IoT devices.

In summary, Azure IoT and IoT Hub provide a solid and secure foundation for building advanced IoT applications, allowing developers to focus on creating innovative solutions rather than dealing with the complexity of managing IoT device connections and data.

Instructions

Arduino IDE Configuration

If you haven't already installed the Arduino IDE, you can install it from this page.

Open the Library Manager from the menu Sketch->Include Library->Manage Libraries.

Search for the "Azure SDK for C" library using the Library Manager's search feature.

Opta™ Azure IoT Tutorial Library Manager
Opta™ Azure IoT Tutorial Library Manager

Azure IoT Instructions

IoT Hub Deployment

Sign in to your Microsoft account from the Azure portal.

You will be presented with the home page where you can add resources to your subscription.

Home page of Azure IoT Portal
Home page of Azure IoT Portal

You'll need to add an IoT Hub to your subscription, so click on Create a resource, and the Azure resource store will appear.

Create a resource page inside Azure IoT Portal
Create a resource page inside Azure IoT Portal

You can use the search function to search for the keyword "iot hub".

Marketplace inside sAzure IoT Portal
Marketplace inside sAzure IoT Portal

Among the search results, the resource we're interested in, IoT Hub, will appear as the first result. Click on Create to start creating your IoT Hub.

Click on IoT hub and fill the required information
Click on IoT hub and fill the required information

To create the new IoT Hub, fill in all the required fields on the page.

  • Select your account's subscription; in this tutorial, it's Azure subscription 1.
  • Create a new Resource Group; for this tutorial, the group OptaGroup was created.
  • In the IoT hub name field, for this tutorial, MyOptaHub was chosen. Remember that the hub name will be used to indicate the connection host in the code that you'll need to customize later.
  • In the Region field, for this tutorial, Germany West Central was used.
  • In the Tier field, select Free or a plan that suits your needs.

The daily message limit for the Free plan is 8,000 and cannot be changed otherwise.

After entering all the necessary information, you should have something similar to the following images:

IoT hub configuration - Create a new Resource group
IoT hub configuration - Create a new Resource group
Iot hub configuration - Be sure to check the Tier plan more convenient to you
Iot hub configuration - Be sure to check the Tier plan more convenient to you

After entering all the necessary information, you can click on Review + create, skipping the other configurations.

A final summary prompts you to check that the entered information is correct, after which you can click on Create to complete the Hub creation and move on to the next steps.

Final summary showing the information filled
Final summary showing the information filled
Overview page of your new IoT Hub subscription
Overview page of your new IoT Hub subscription

You will be redirected to the creation and registration status page of the new IoT Hub; the operation takes a few moments.

Deployment page
Deployment page

Subsequently, you will receive a message confirming that the IoT Hub is created and configured.

Deployment complete screen
Deployment complete screen

You can click on Go to resource to access the IoT Hub dashboard.

IoT Hub Dashboard
IoT Hub Dashboard

Now you can add a device to your Hub; the new device will allow the connection to your Opta™. To add a new device, click on Devices under the Device management section on the left side of your Hub.

Devices screen
Devices screen

On the page that appears, click on Add Device.

  • In this tutorial, the device ID MyOpta was used.
  • Keep Symmetric key for Authentication type.
  • Keep the Auto generate keys and Connect this device to an IoT hub settings enabled.

You will then have a page with fields filled in similar to this:

Creation of a new device
Creation of a new device

By clicking on Save, you will be redirected back to the list of devices.

New device created is now listed
New device created is now listed

Click on the name of the newly created device in the list of devices associated with your Hub. The device configuration page will be displayed. You don't need to make any changes to this page, but you will need to use the information provided on this page in the sketch to be uploaded to Opta™.

From the current page, the only information needed for the configuration is the Device ID and the Primary key. Once requested, you can easily copy them using the 'Copy' button next to each corresponding field.

Device ID and Primary key credentials
Device ID and Primary key credentials

The next step of the tutorial is dedicated to customizing and understanding the sample code.

Sketch

Introduction

In this section, some parts of the sample code provided by Microsoft are analyzed and explained, modified to be used correctly on Opta™.

The original project example can be found in the official library repository, accessible from this page.

The example modified for Opta™ is available here.

Sketch Configuration for Connection

To establish a connection to Azure IoT, you don't need to make any changes to your sketch. All the configurations necessary to make the transmission of data from Opta™ to the Microsoft Cloud work are enclosed in a few lines of code.

In particular, the information to be customized for the connection is represented by various #define in the iot_configs.h file.

Opta™ Azure IoT Tutorial Sketch- Defines section
Opta™ Azure IoT Tutorial Sketch- Defines section

You will need some of the information present on the device configuration page on Azure. As mentioned earlier, you can copy the necessary information and modify the code accordingly:

1// Wifi
2#define IOT_CONFIG_WIFI_SSID "MyWifi" // Change it to your Wi-Fi network name
3#define IOT_CONFIG_WIFI_PASSWORD "12345678" // Change it to your Wi-Fi network password
4#define IOT_CONFIG_WIFI_CONNECT_RETRY_MS 10000
5
6// Azure IoT
7#define IOT_CONFIG_IOTHUB_FQDN "MyOptaHub.azure-devices.net" // Host
8#define IOT_CONFIG_DEVICE_ID "MyOpta" // Device ID, chosen by you during device creation
9#define IOT_CONFIG_DEVICE_KEY "RAK5epqOa5i6FncyOz6PkYdwYKMrWDGdTAIoTJXJ1lc=" // Primary key, yours will be similar to this but not this

The IOT_CONFIG_WIFI_SSID definition assumes the value of your Wi-Fi network's SSID, while IOT_CONFIG_WIFI_PASSWORD represents the password associated with that network.

Note that IOT_CONFIG_IOTHUB_FQDN contains the hostname, easily obtainable by copying the name of your IoT Hub. In the context of this tutorial, the chosen name is MyOptaHub.

The hostname that facilitates the connection of Opta™ to Azure IoT is consistently azure-devices.net. You will need to add the name of your hub before this hostname as follows: MyHubID.azure-devices.net. Consequently, for this tutorial, it becomes MyOptaHub.azure-devices.net.

The IOT_CONFIG_DEVICE_ID definition specifies the device identifier, and for this tutorial, MyOpta was selected.

Finally, IOT_CONFIG_DEVICE_KEY requires entering the device's private key.

Other Code Customizations

In the iot_configs.h file, you find a definition that allows indicating in milliseconds the time that must elapse between sending one telemetry message and the next. The relevant definition is IOT_CONFIG_TELEMETRY_FREQUENCY_MS on line 19.

In the code, it is set to 300,000 milliseconds or 5 minutes:

Opta™ Azure IoT Tutorial Sketch - Telemetry frequency
Opta™ Azure IoT Tutorial Sketch - Telemetry frequency
1// Publish 1 message every 5 minutes
2#define IOT_CONFIG_TELEMETRY_FREQUENCY_MS 300000

You can customize the value in milliseconds to change the time interval between sending messages.

In the Azure_IoT_Hub_Opta.ino file, you will find a portion of code represented by the generateTelemetry() function at line 329, which is called to allow sending the message to Azure IoT:

Opta™ Azure IoT Tutorial Sketch - Telemetry payload
Opta™ Azure IoT Tutorial Sketch - Telemetry payload
1/*
2 * generateTelemetry:
3 * Simulated telemetry.
4 * In your application, this function should retrieve real telemetry data from the device and format
5 * it as needed.
6 */
7static char* generateTelemetry()
8{
9 telemetryPayload = String("{ \"msgCount\": ") + telemetrySendCount + " }";
10 telemetrySendCount++;
11
12 return (char*)telemetryPayload.c_str();
13}

You can modify this function by changing the value of telemetryPayload and send values from your sensors or custom messages. In the example, a counter that increments its value with each send is sent.

You can modify this function to send a different message, for example, a classic "Hello, World":

1static char* generateTelemetry()
2{
3 telemetryPayload = String("Hello, World!");
4 return (char*)telemetryPayload.c_str();
5}

Uploading the Sketch

If the modifications made to the code meet your requirements, you can upload the code to your Opta™ and check if a message confirming the successful sending of telemetry data is displayed in the serial monitor:

Serial monitor messages
Serial monitor messages

You can verify the actual reception of messages from the dashboard of our IoT Hub:

IoT Hub Dashboard receiving data from Opta™
IoT Hub Dashboard receiving data from Opta™

Conclusion

In conclusion to this tutorial, we have explored and customized a sample sketch provided by Microsoft to enable the connection of Opta™ to Azure IoT. Here's a summary of the activities performed:

  • Analysis of the Sample Sketch: We examined a sample sketch provided by Microsoft for connecting to Azure IoT, available in the official library repository.
  • Modifications for Opta™: We made the necessary changes to the sketch to adapt it and use it correctly with Opta™, customizing definitions and configurations.
  • Azure IoT Configuration: We explained how to obtain the necessary information from the device configuration on Azure IoT, including the hostname of your hub and the device key.
  • Integration with Azure IoT Hub: We illustrated the process of configuring the definitions in the
    iot_configs.h
    file, which manages Wi-Fi connection and communication with Azure IoT Hub.
  • Code Customizations: We provided guidance on further customizing the code, including adjusting the telemetry message sending interval and modifying the simulated data sent to Azure IoT.

The tutorial provides a detailed guide to connect Opta™ to Azure IoT, allowing the secure and reliable transmission of data. This is just a starting point, and further customizations can be made based on the specific needs of the project. In case you want to dig deeper into the Azure IoT technology, check the Azure IoT official documentation.

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.