page_type | description | languages | products | ||||
---|---|---|---|---|---|---|---|
sample |
Connecting Arduino Nano RP2040 Connect to Azure IoT Central using the Azure SDK for C Arduino library |
|
|
Getting started with the Arduino Nano RP2040 Connect and Azure IoT Central with Azure SDK for C Arduino library
Total completion time: 30 minutes
- Getting started with the Arduino Nano RP2040 Connect 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 Nano RP2040 Connect 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 Nano RP2040 Connect
- 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
NOTE: Device keys are used to automatically generate a SAS token for authentication, which is only valid for one hour.
-
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 Nano Boards support in the Arduino IDE. Full instructions can be found here.
-
Navigate to Tools > Board > Boards Manager.
-
Search for 'RP2040' and install the Arduino Mbed OS Nano Boards core.
-
Install the latest version.
Note: This process may take several minutes.
-
-
Nagivate to Tools > Board > Arduino Mbed OS Nano Boards and select 'Arduino Nano RP2040 Connect'.
-
Install WiFiNINA library for the Nano RP2040 Embedded C SDK sample.
-
Navigate to Tools > Manage Libraries.
-
Search for the 'WiFiNINA' library.
-
Install the latest version.
Note: This process may take several minutes.
-
-
If this is your first time using the Nano RP2040 Connect, follow these instructions to update the WiFi firmware on the Nano RP2040 Connect.
-
Install the ArduinoBearSSL, ArduinoMqttClient, and ArduinoECCX08 libraries.
- Navigate to Tools > Manage Libraries.
- Search for the 'ArduinoBearSSL' library. Install the latest version.
- Search for the 'ArduinoMqttClient' library. Install the latest version.
- Search for the 'ArduinoECCX08' library. Install the latest version.
-
You may need to restart the Arduino IDE for changes to show up.
-
Open the Arduino Nano RP2040 Connect sample.
- In the Arduino IDE, navigate to File > Examples > Azure SDK For C
- Select 'Azure_IoT_Central_Arduino_Nano_RP2040_Connect' 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 Nano RP 2040 Connect to your USB port.
-
On the Arduino IDE, select the port.
- Navigate to Tools > Port.
- Select the port to which the Nano RP2040 Connect is connected.
-
Upload the sketch.
-
Navigate to Sketch > Upload.
Note: This process may take several minutes.
Expected output of the upload:
Sketch uses 207740 bytes (1%) of program storage space. Maximum is 16777216 bytes. Global variables use 66812 bytes (24%) of dynamic memory, leaving 203524 bytes for local variables. Maximum is 270336 bytes. .
-
-
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:28:16 [INFO] Connecting to WIFI wifi_ssid <ssid> .. 2106-02-06 23:28:16 [INFO] WiFi connected, IP address: 536923696 2106-02-06 23:28:16 [INFO] Setting time using SNTP .... 2022-06-16 16:03:03 [INFO] Time initialized! 2022-06-16 16:03:03 [INFO] Azure IoT client initialized (state=2) 2022-06-16 16:03:04 [INFO] MQTT Client ID: <device id> 2022-06-16 16:03:04 [INFO] MQTT Username: <scope id>/registrations/<device id>/api-version=2019-03-31 2022-06-16 16:03:04 [INFO] MQTT Password: *** 2022-06-16 16:03:04 [INFO] MQTT client address: global.azure-devices-provisioning.net 2022-06-16 16:03:04 [INFO] MQTT client port: 8883 2022-06-16 16:03:08 [INFO] MQTT client connected. 2022-06-16 16:03:09 [INFO] MQTT client subscribing to '$dps/registrations/res/#' 2022-06-16 16:03:09 [INFO] MQTT topic subscribed 2022-06-16 16:03:09 [INFO] MQTT client publishing to '$dps/registrations/PUT/iotdps-register/?$rid=1' 2022-06-16 16:03:10 [INFO] MQTT message received. 2022-06-16 16:03:11 [INFO] MQTT client publishing to '$dps/registrations/GET/iotdps-get-operationstatus/?$rid=1&operationId=4.36e237c8db462f45.7081bb47-3e8f-4e1e-af40-da3477582335' 2022-06-16 16:03:11 [INFO] MQTT message received. 2022-06-16 16:03:14 [INFO] MQTT client publishing to '$dps/registrations/GET/iotdps-get-operationstatus/?$rid=1&operationId=4.36e237c8db462f45.7081bb47-3e8f-4e1e-af40-da3477582335' 2022-06-16 16:03:14 [INFO] MQTT message received. 2022-06-16 16:03:15 [INFO] MQTT client being disconnected. 2022-06-16 16:03:15 [INFO] MQTT Client ID: <device id> 2022-06-16 16:03:15 [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 16:03:15 [INFO] MQTT Password: *** 2022-06-16 16:03:15 [INFO] MQTT client address: <provisioned iot hub fqdn>.azure-devices.net 2022-06-16 16:03:15 [INFO] MQTT client port: 8883 2022-06-16 16:03:18 [INFO] MQTT client connected. 2022-06-16 16:03:18 [INFO] MQTT client subscribing to '$iothub/methods/POST/#' 2022-06-16 16:03:19 [INFO] MQTT topic subscribed 2022-06-16 16:03:19 [INFO] MQTT client subscribing to '$iothub/twin/res/#' 2022-06-16 16:03:19 [INFO] MQTT topic subscribed 2022-06-16 16:03:20 [INFO] MQTT client subscribing to '$iothub/twin/PATCH/properties/desired/#' 2022-06-16 16:03:20 [INFO] MQTT topic subscribed 2022-06-16 16:03:20 [INFO] MQTT client publishing to '$iothub/twin/PATCH/properties/reported/?$rid=0' 2022-06-16 16:03:20 [INFO] MQTT client publishing to 'devices/<device id>/messages/events/' 2022-06-16 16:03:20 [INFO] MQTT message received. 2022-06-16 16:03:20 [INFO] Properties update request completed (id=0, status=204) 2022-06-16 16:03:29 [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 Nano RP2040 Connect 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.