Skip to content

Commit 9e80013

Browse files
authored
Merge pull request #1599 from mirkopac/main
Arduino Opta on Azure IoT Tutorial
2 parents 438cc72 + 3cc7a91 commit 9e80013

25 files changed

+268
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
---
2+
title: 'Arduino Opta with Azure IoT'
3+
description: "Learn to connect your Arduino Opta to the Microsoft Azure cloud"
4+
author: 'Mirko Pacioni'
5+
libraries:
6+
- name: 'Azure SDK for C'
7+
url: https://www.arduino.cc/reference/en/libraries/azure-sdk-for-c/
8+
difficulty: intermediate
9+
tags:
10+
- Getting-started
11+
- Azure IoT
12+
software:
13+
- arduino-ide
14+
hardware:
15+
- hardware/07.opta/opta-family/opta
16+
---
17+
18+
![Arduino Opta Azure IoT Tutorial Hero Banner](hero-banner.png)
19+
20+
## Introduction
21+
22+
This tutorial demonstrates the integration of Arduino Opta with Azure IoT. You will proceed to create a reliable and secure IoT environment, harnessing the power of these two technologies.
23+
24+
## Goals
25+
26+
- **Initial Configuration:** Guide the user through the initial setup steps for Azure IoT and IoT Hub to establish a connection between Arduino Opta and Azure IoT.
27+
28+
- **Reliable Transmission:** How to reliably transmit data from Arduino Opta to Azure IoT.
29+
30+
## Required Hardware and Software
31+
32+
### Required Hardware
33+
34+
- [Opta™](https://store.arduino.cc/collections/pro-family) (x1)
35+
- USB-C® cable (x1)
36+
37+
### Required Software
38+
39+
- [Arduino IDE](https://www.arduino.cc/en/software)
40+
- [Azure SDK for C library](https://www.arduino.cc/reference/en/libraries/azure-sdk-for-c/) (installable from the IDE, see next steps)
41+
42+
## Azure IoT
43+
44+
[Azure IoT](https://portal.azure.com) 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.
45+
46+
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:
47+
48+
- **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.
49+
50+
- **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.
51+
52+
- **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.
53+
54+
- **Remote Control:** Enables cloud applications to send commands to devices and receive responses, facilitating remote control and automation of connected devices.
55+
56+
- **Device Management:** Provides device management functionalities such as registration, state monitoring, and lifecycle management, simplifying the operational management of IoT devices.
57+
58+
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.
59+
60+
## Instructions
61+
62+
### Arduino IDE Configuration
63+
64+
If you haven't already installed the Arduino IDE, you can install it from [this page](https://www.arduino.cc/en/software).
65+
66+
Open the Library Manager from the menu **Sketch->Include Library->Manage Libraries**.
67+
68+
Search for the "Azure SDK for C" library using the Library Manager's search feature.
69+
70+
![Arduino Opta Azure IoT Tutorial Library Manager](assets/opta-azure-iot-tutorial-213345.png)
71+
72+
73+
## Azure IoT Instructions
74+
75+
### IoT Hub Deployment
76+
77+
Sign in to your Microsoft account from the [Azure portal](https://portal.azure.com).
78+
79+
You will be presented with the home page where you can add resources to your subscription.
80+
81+
![Arduino Opta Azure IoT Tutorial 1](assets/opta-azure-iot-tutorial-194248.png)
82+
83+
You'll need to add an IoT Hub to your subscription, so click on **Create a resource**, and the Azure resource store will appear.
84+
85+
![Arduino Opta Azure IoT Tutorial 2](assets/opta-azure-iot-tutorial-194314.png)
86+
87+
You can use the search function to search for the keyword **"iot hub"**.
88+
89+
![Arduino Opta Azure IoT Tutorial 3](assets/opta-azure-iot-tutorial-194413.png)
90+
91+
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.
92+
93+
![Arduino Opta Azure IoT Tutorial 4](assets/opta-azure-iot-tutorial-194518.png)
94+
95+
To create the new IoT Hub, fill in all the required fields on the page.
96+
97+
- Select your account's subscription; in this tutorial, it's **Azure subscription 1**.
98+
- Create a new Resource Group; for this tutorial, the group **OptaGroup** was created.
99+
- 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.
100+
- In the Region field, for this tutorial, **Germany West Central** was used.
101+
- In the Tier field, select **Free** or a plan that suits your needs.
102+
103+
The daily message limit for the **Free** plan is 8,000 and cannot be changed otherwise.
104+
105+
After entering all the necessary information, you should have something similar to this:
106+
107+
![Arduino Opta Azure IoT Tutorial 5](assets/opta-azure-iot-tutorial-194559.png)
108+
109+
![Arduino Opta Azure IoT Tutorial 5](assets/opta-azure-iot-tutorial-195158.png)
110+
111+
After entering all the necessary information, you can click on **Review + create**, skipping the other configurations.
112+
113+
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.
114+
115+
![Arduino Opta Azure IoT Tutorial 6](assets/opta-azure-iot-tutorial-195244.png)
116+
117+
![Arduino Opta Azure IoT Tutorial 6](assets/opta-azure-iot-tutorial-194452.png)
118+
119+
You will be redirected to the creation and registration status page of the new IoT Hub; the operation takes a few moments.
120+
121+
![Arduino Opta Azure IoT Tutorial 7](assets/opta-azure-iot-tutorial-195326.png)
122+
123+
Subsequently, you will receive a message confirming that the IoT Hub is created and configured.
124+
125+
![Arduino Opta Azure IoT Tutorial 8](assets/opta-azure-iot-tutorial-195443.png)
126+
127+
You can click on **Go to resource** to access the IoT Hub dashboard.
128+
129+
![Arduino Opta Azure IoT Tutorial 9](assets/opta-azure-iot-tutorial-195509.png)
130+
131+
Now you can add a device to your Hub; the new device will allow the connection to your Arduino Opta. To add a new device, click on **Devices** under the **Device management** section on the left side of your Hub.
132+
133+
![Arduino Opta Azure IoT Tutorial 10](assets/opta-azure-iot-tutorial-195552.png)
134+
135+
On the page that appears, click on **Add Device**.
136+
137+
- In this tutorial, the device ID **MyOpta** was used.
138+
- Keep **Symmetric key** for **Authentication type**.
139+
- Keep the **Auto generate keys** and **Connect this device to an IoT hub** settings enabled.
140+
141+
You will then have a page with fields filled in similar to this:
142+
143+
![Arduino Opta Azure IoT Tutorial 11](assets/opta-azure-iot-tutorial-195652.png)
144+
145+
By clicking on **Save**, you will be redirected back to the list of devices.
146+
147+
![Arduino Opta Azure IoT Tutorial 12](assets/opta-azure-iot-tutorial-195718.png)
148+
149+
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 Arduino Opta.
150+
151+
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.
152+
153+
![Arduino Opta Azure IoT Tutorial 13](assets/opta-azure-iot-tutorial-195821.png)
154+
155+
![Arduino Opta Azure IoT Tutorial 13](assets/opta-azure-iot-tutorial-201212.png)
156+
157+
The next step of the tutorial is dedicated to customizing and understanding the sample code.
158+
159+
## Sketch
160+
161+
### Introduction
162+
163+
In this section, some parts of the sample code provided by Microsoft are analyzed and explained, modified to be used correctly on Arduino Opta.
164+
165+
The original project example can be found in the official library repository, accessible from [this page](https://github.com/Azure/azure-sdk-for-c-arduino/blob/main/examples/Azure_IoT_Hub_PortentaH7/Azure_IoT_Hub_PortentaH7.ino).
166+
167+
The example modified for Arduino Opta is available [here](assets/opta-azure-iot-tutorial-sketch.zip).
168+
169+
### Sketch Configuration for Connection
170+
171+
To establish a connection to Azure IoT, you don't need to make many changes to your sketch. All the configurations necessary to make the transmission of data from Arduino Opta to the Microsoft cloud work are enclosed in a few lines of code.
172+
173+
In particular, the information to be customized for the connection is represented by various **#define** in the **iot_configs.h** file.
174+
175+
![Arduino Opta Azure IoT Tutorial Sketch 1](assets/opta-azure-iot-tutorial-201329.png)
176+
177+
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:
178+
179+
```cpp
180+
// Wifi
181+
#define IOT_CONFIG_WIFI_SSID "MyWifi" // Change it to your Wi-Fi network name
182+
#define IOT_CONFIG_WIFI_PASSWORD "12345678" // Change it to your Wi-Fi network password
183+
#define IOT_CONFIG_WIFI_CONNECT_RETRY_MS 10000
184+
185+
// Azure IoT
186+
#define IOT_CONFIG_IOTHUB_FQDN "MyOptaHub.azure-devices.net" // Host
187+
#define IOT_CONFIG_DEVICE_ID "MyOpta" // Device ID, chosen by you during device creation
188+
#define IOT_CONFIG_DEVICE_KEY "RAK5epqOa5i6FncyOz6PkYdwYKMrWDGdTAIoTJXJ1lc=" // Primary key, yours will be similar to this but not this
189+
```
190+
191+
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.
192+
193+
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**.
194+
195+
The hostname that facilitates the connection of Arduino 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**.
196+
197+
The **IOT_CONFIG_DEVICE_ID** definition specifies the device identifier, and for this tutorial, **MyOpta** was selected.
198+
199+
Finally, **IOT_CONFIG_DEVICE_KEY** requires entering the device's private key.
200+
201+
### Other Code Customizations
202+
203+
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** at line 19.
204+
205+
In the code, it is set to 300,000 milliseconds, or 5 minutes:
206+
207+
![Arduino Opta Azure IoT Tutorial Sketch 1](assets/opta-azure-iot-tutorial-202105.png)
208+
209+
```cpp
210+
// Publish 1 message every 5 minutes
211+
#define IOT_CONFIG_TELEMETRY_FREQUENCY_MS 300000
212+
```
213+
You can customize the value in milliseconds to change the time interval between sending messages.
214+
215+
In the **Azure_IoT_Hub_Opta.ino** file, you find a portion of code represented by the **generateTelemetry()** function at line 329, which is called to allow sending the message to Azure IoT:
216+
217+
![Arduino Opta Azure IoT Tutorial Sketch 1](assets/opta-azure-iot-tutorial-202627.png)
218+
219+
220+
```cpp
221+
/*
222+
* generateTelemetry:
223+
* Simulated telemetry.
224+
* In your application, this function should retrieve real telemetry data from the device and format
225+
* it as needed.
226+
*/
227+
static char* generateTelemetry()
228+
{
229+
telemetryPayload = String("{ \"msgCount\": ") + telemetrySendCount + " }";
230+
telemetrySendCount++;
231+
232+
return (char*)telemetryPayload.c_str();
233+
}
234+
```
235+
236+
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.
237+
238+
You can modify this function to send a different message, for example, a classic "Hello, World":
239+
240+
```cpp
241+
static char* generateTelemetry()
242+
{
243+
telemetryPayload = String("Hello, World!");
244+
return (char*)telemetryPayload.c_str();
245+
}
246+
```
247+
248+
### Uploading the Sketch
249+
250+
If the modifications made to the code meet your requirements, you can upload the code to your Arduino Opta and check if a message confirming the successful sending of telemetry data is displayed in the serial monitor:
251+
252+
![Arduino Opta Azure IoT Tutorial Sketch 1](assets/opta-azure-iot-tutorial-213347.png)
253+
254+
You can verify the actual reception of messages from the dashboard of our IoT Hub:
255+
256+
![Arduino Opta Azure IoT Tutorial Sketch 1](assets/opta-azure-iot-tutorial-201604.png)
257+
258+
### Conclusion
259+
260+
In conclusion to this tutorial, we have explored and customized a sample sketch provided by Microsoft to enable the connection of Arduino Opta to Azure IoT. Here's a summary of the activities performed:
261+
262+
- **Analysis of the Sample Sketch:** We examined a sample sketch provided by Microsoft for connecting to Azure IoT, available in the official library repository.
263+
- **Modifications for Arduino Opta:** We made the necessary changes to the sketch to adapt it and use it correctly with Arduino Opta, customizing definitions and configurations.
264+
- **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.
265+
- **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.
266+
- **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.
267+
268+
The tutorial provides a detailed guide to connect Arduino 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. Hoping that this tutorial has been helpful and has contributed to understanding the integration of Arduino Opta with Azure IoT.

0 commit comments

Comments
 (0)