page_type | description | languages | products | ||||
---|---|---|---|---|---|---|---|
sample |
Connecting Arduino Portenta H7 to Azure IoT Central using the Azure SDK for C Arduino library |
|
|
Getting started with the Arduino Portenta H7 and Azure IoT Central with Azure SDK for C Arduino library
Total completion time: 30 minutes
- Getting started with the Arduino Portenta H7 and Azure IoT Central with Azure SDK for C Arduino library
In this tutorial you will use the Azure SDK for C to connect the Arduino Portenta H7 to Azure IoT Central. The article is part of the series IoT Device Development. The series introduces device developers to the Azure SDK for C, and shows how to connect several device evaluation kits to Azure IoT.
You will complete the following tasks:
- Install the Azure SDK for C library on Arduino
- Build the image and flash it onto the Arduino Portenta H7
- Use Azure IoT Central to create cloud components, view properties, view device telemetry, and call direct commands
The following was run on Windows 10 and WSL1 Ubuntu Desktop 20.04 environments, with Arduino IDE 1.8.19 and Arduino Arduino Nano RP2040 Connect with headers.
-
Have an Azure account created.
-
Have the latest Arduino IDE installed.
There are several ways to connect devices to Azure IoT. In this section, you learn how to connect a device by using Azure IoT Central. IoT Central is an IoT application platform that reduces the cost and complexity of creating and managing IoT solutions.
To create a new application:
-
Go to Azure IoT Central portal.
-
On the left side menu, select 'My apps'.
-
Select '+ New application'.
-
In the 'Custom app' box, select 'Create app'.
-
Create a custom Application name and a URL.
-
Under 'Pricing plan', select 'Free' to activate a 7-day trial.
-
Select 'Create'.
-
After IoT Central provisions the application, it redirects you automatically to the new application dashboard.
Note: If you have an existing IoT Central application, you can use it to complete the steps in this article rather than create a new application.
In this section, you will use the IoT Central application dashboard to create a new logical device.
To create a device:
-
On the left side menu, under 'Connect', select 'Devices'.
-
Select '+ New'. A 'Create a new device' window will appear.
-
Fill in the desired 'Device name' and 'Device ID'.
-
Leave Device template as 'Unassigned'.
-
Select 'Create'. The newly created device will appear in the 'All devices' list.
-
Under 'Device name', select your newly created device name.
-
In the top menu bar, select 'Connect'. A 'Device connection groups' window will appear.
-
We will need the following information from this window:
- ID scope
- Device ID
- Primary key
-
Open the Arduino IDE.
-
Install the Azure SDK for Embedded C library.
- Navigate to Tools > Manage Libraries.
- Search for the 'azure-sdk-for-c' library.
- Install the latest version.
-
Install Arduino Mbed OS Portenta Boards support in the Arduino IDE. Full instructions can be found here.
-
Navigate to Tools > Board > Boards Manager.
-
Search for 'Portenta' and install the Arduino Mbed OS Portenta Boards core.
-
Install the latest version.
Note: This process may take several minutes.
-
-
Nagivate to Tools > Board > Arduino Mbed OS Portenta Boards and select 'Arduino Portenta H7 (M7 core)'.
-
In Tools > Flash split, set the flash split to be '2MB M7 + M4 in SDRAM'.
-
If this is your first time using the Portenta, follow these instructions to update the WiFi firmware on the Portenta.
-
Install additional libraries for the Portenta Embedded C SDK sample.
-
This process is more involved than typical because we are using the NTP Client Generic library which has circular dependencies so we must do a special install to only grab the what we need.
-
There are two ways to do this:
- Download the NTP Client Generic library manually from its repository, or
- Use the Arduino CLI.
-
This tutorial will use the CLI approach because it is faster and easier to describe.
-
Using the Arduino CLI, type and run the following command to install the NTP Client :
arduino-cli lib install --no-deps NTPClient_Generic
-
Since we're already in the Arduino CLI, let's install remaining libraries (can also install these from Library Manager):
arduino-cli lib install "Azure SDK for C" ArduinoBearSSL Time ArduinoMqttClient ArduinoECCX08
-
-
You may need to restart the Arduino IDE for changes to show up.
-
Open the Arduino Portenta H7 sample.
- In the Arduino IDE, navigate to File > Examples > Azure SDK For C
- Select Azure_IoT_Central_Arduino_Portenta_H7 to open the sample.
-
Navigate to the 'iot_configs.h' file
-
In the 'iot_configs.h' file, fill in your credentials.
- Add in your WiFi SSID and password.
- Paste your ID Scope for the
IOT_CONFIG_DPS_ID_SCOPE
variable. - Paste your Device ID for the
IOT_CONFIG_DEVICE_ID
variable. - Finally, paste your Primary Key for the
IOT_CONFIG_DEVICE_KEY
variable.
-
Connect the Arduino Portenta H7 to your USB port.
-
On the Arduino IDE, select the port.
- Navigate to Tools > Port.
- Select the port to which the Portenta H7 is connected.
-
Upload the sketch.
-
Navigate to Sketch > Upload.
Note: This process may take several minutes.
Expected output of the upload:
Sketch uses 431236 bytes (21%) of program storage space. Maximum is 1966080 bytes. Global variables use 95208 bytes (18%) of dynamic memory, leaving 428416 bytes for local variables. Maximum is 523624 bytes. dfu-util 0.10-dev Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc. Copyright 2010-2021 Tormod Volden and Stefan Schmidt This program is Free Software and has ABSOLUTELY NO WARRANTY Please report bugs to http://sourceforge.net/p/dfu-util/tickets/ Opening DFU capable USB device... Device ID 2341:035b Device DFU version 011a Claiming USB DFU Interface... Setting Alternate Interface #0 ... Determining device status... DFU state(2) = dfuIDLE, status(0) = No error condition is present DFU mode device DFU version 011a Device returned transfer size 4096 DfuSe interface name: "Internal Flash " Downloading element to address = 0x08040000, size = 435916 Erase [=========================] 100% 435916 bytes Erase done. Download [=========================] 100% 435916 bytes Download done. File downloaded successfully Transitioning to dfuMANIFEST state
-
-
While the sketch is uploading, open the Serial Monitor to monitor the MCU (microcontroller) locally via the Serial Port.
-
Navigate to Tools > Serial Monitor.
If you perform this step right away after uploading the sketch, the serial monitor will show an output similar to the following upon success:
2106-02-06 23:32:04 [INFO] Connecting to WIFI wifi_ssid <ssid> ... 2106-02-06 23:32:44 [INFO] WiFi connected, IP address: 604051168 2106-02-06 23:32:44 [INFO] Setting time using SNTP 2022-06-16 13:04:50 [INFO] Time initialized! 2022-06-16 13:04:50 [INFO] Azure IoT client initialized (state=2) 2022-06-16 13:04:50 [INFO] MQTT Client ID: <device id> 2022-06-16 13:04:50 [INFO] MQTT Username: <id scope>/registrations/<device id>/api-version=2019-03-31 2022-06-16 13:04:50 [INFO] MQTT Password: *** 2022-06-16 13:04:50 [INFO] MQTT client address: global.azure-devices-provisioning.net 2022-06-16 13:04:50 [INFO] MQTT client port: 8883 2022-06-16 13:04:52 [INFO] MQTT client connected. 2022-06-16 13:04:53 [INFO] MQTT client subscribing to '$dps/registrations/res/#' 2022-06-16 13:04:53 [INFO] MQTT topic subscribed 2022-06-16 13:04:54 [INFO] MQTT client publishing to '$dps/registrations/PUT/iotdps-register/?$rid=1' 2022-06-16 13:04:55 [INFO] MQTT message received. 2022-06-16 13:04:55 [INFO] MQTT client publishing to '$dps/registrations/GET/iotdps-get-operationstatus/?$rid=1&operationId=4.36e237c8db462f45.1209aff3-9c58-4c56-b070-0fd4359286d1' 2022-06-16 13:04:56 [INFO] MQTT message received. 2022-06-16 13:04:58 [INFO] MQTT client publishing to '$dps/registrations/GET/iotdps-get-operationstatus/?$rid=1&operationId=4.36e237c8db462f45.1209aff3-9c58-4c56-b070-0fd4359286d1' 2022-06-16 13:04:59 [INFO] MQTT message received. 2022-06-16 13:05:00 [INFO] MQTT client being disconnected. 2022-06-16 13:05:00 [INFO] MQTT Client ID: <device id> 2022-06-16 13:05:00 [INFO] MQTT Username: <provisioned iot hub fqdn>.azure-devices.net/<device id>/?api-version=2020-09-30&DeviceClientType=c%2F1.3.1(ard;portentaH7)&model-id=dtmi%3Aazureiot%3Adevkit%3Afreertos%3AEsp32AzureIotKit%3B1 2022-06-16 13:05:00 [INFO] MQTT Password: *** 2022-06-16 13:05:00 [INFO] MQTT client address: <provisioned iot hub fqdn>.azure-devices.net 2022-06-16 13:05:00 [INFO] MQTT client port: 8883 2022-06-16 13:05:02 [INFO] MQTT client connected. 2022-06-16 13:05:02 [INFO] MQTT client subscribing to '$iothub/methods/POST/#' 2022-06-16 13:05:03 [INFO] MQTT topic subscribed 2022-06-16 13:05:03 [INFO] MQTT client subscribing to '$iothub/twin/res/#' 2022-06-16 13:05:03 [INFO] MQTT topic subscribed 2022-06-16 13:05:04 [INFO] MQTT client subscribing to '$iothub/twin/PATCH/properties/desired/#' 2022-06-16 13:05:04 [INFO] MQTT topic subscribed 2022-06-16 13:05:04 [INFO] MQTT client publishing to '$iothub/twin/PATCH/properties/reported/?$rid=0' 2022-06-16 13:05:04 [INFO] MQTT client publishing to 'devices/<device id>/messages/events/' 2022-06-16 13:05:04 [INFO] MQTT message received. 2022-06-16 13:05:04 [INFO] Properties update request completed (id=0, status=204) 2022-06-16 13:05:14 [INFO] MQTT client publishing to 'devices/<device id>/messages/events/'
-
With IoT Central, you can view the device status and information, observe telemetry, and send commands.
- Go to your IoT Central application portal.
- Select your application.
- On the left side menu, under 'Connect', select 'Devices'.
To view the device status in IoT Central portal:
-
Find your device in the devices list.
-
Confirm the 'Device status' of the device is updated to 'Provisioned'.
-
Confirm the 'Device template' of the device has updated to 'Espressif ESP32 Azure IoT Kit'.
Note: The 'Espressif ESP32 Azure IoT Kit' device template is used in this Arduino Portenta H7 sample for simplicity. It is a published template available from IoT Central. For more information on creating a custom device template, view these instructions.
To view the device information in IoT Central portal:
To view telemetry in IoT Central portal:
-
Click on your device's name in the device list.
-
Select the 'Overview' tab.
-
View the telemetry as the device sends messages to the cloud.
To send a command to the device:
-
Select the 'Commands' tab.
-
Locate the 'Display Text' box.
-
In the 'Content' textbox, enter the text to be displayed on the screen.
-
Select 'Run'.
-
Because this is a simulated screen, the text will print to the log.
2022-06-16 13:31:50 [INFO] OLED display: <text>
To toggle an LED:
-
Select the 'Commands' tab.
-
Locate the 'Toggle LED 1' or 'Toggle LED 2' box.
-
Select 'Run'.
-
Because these are simulated LEDs, the following will print to the log.
2022-06-16 13:31:46 [INFO] LED <#> state: <ON/OFF>
If you no longer need the Azure resources created in this tutorial, you can delete them from the IoT Central portal. Optionally, if you continue to another tutorial in this Getting Started guide, you can keep the resources you've already created and reuse them.
To keep the Azure IoT Central sample application but remove only specific devices:
- On the left side menu, under 'Connect', select 'Devices'.
- Hover over your device's name and click on the circle that appears to the left. The circle will turn blue.
- Select 'Delete'. A box will appear to confirm deletion.
- Select 'Delete' again.
To remove the entire Azure IoT Central sample application and all its devices and resources:
- On the left side menu, under 'Settings', select 'Application'.
- Select the 'Management' tab.
- Scroll to the bottom of the page.
- Select 'Delete'. A box will appear to confirm deletion.
- Select 'Delete' again.
The Azure IoT service certificates presented during TLS negotiation shall be always validated, on the device, using the appropriate trusted root CA certificate(s).
The Azure SDK for C Arduino library automatically installs the root certificate used in the United States regions, and adds it to the Arduino sketch project when the library is included.
For other regions (and private cloud environments), please use the appropriate root CA certificate.
For important information and additional guidance about certificates, please refer to this blog post from the security team.
- The error policy for the Embedded C SDK client library is documented here.
- File an issue via Github Issues.
- Check previous questions or ask new ones on StackOverflow using the
azure
andc
tags.
This project welcomes contributions and suggestions. Find more contributing details here.
Azure SDK for Embedded C is licensed under the MIT license.