page_type | description | languages | products | ||||
---|---|---|---|---|---|---|---|
sample |
Connecting ESPRESSIF ESP32-Azure IoT Kit to Azure IoT Central using the Azure SDK for C Arduino library |
|
|
Getting started with the ESPRESSIF ESP32-Azure IoT Kit and Azure IoT Central with Azure SDK for C Arduino library
Total completion time: 30 minutes
In this tutorial you use the Azure SDK for C to connect the ESPRESSIF ESP32-Azure IoT Kit (hereafter, the ESP32 DevKit) 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 ESP32 DevKit
- Use Azure IoT Central to create cloud components, view properties, view device telemetry, and call direct commands
The following was run on Windows 11, with Arduino IDE 2.1.0 and ESP32 board library version 2.0.9.
-
Have the latest Arduino IDE installed.
-
Have the ESP32 board support installed on Arduino IDE.
- ESP32 boards are not natively supported by Arduino IDE, so you need to add them manually.
- Follow the instructions in the official ESP32 repository.
- If your ESP32 board is not recognized and a COM port is not mapped by your computer, try installing the serial port drivers recommended by ESPRESSIF.
-
Hardware
- ESPRESSIF ESP32-Azure IoT Kit
- Wi-Fi 2.4 GHz
- USB 2.0 A male to Micro USB male data cable
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:
-
From Azure IoT Central portal, select My apps on the side navigation menu.
-
Select + New application.
-
Select Custom apps.
-
Add Application Name and a URL.
-
Choose the pricing plan of your preference.
-
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 use the IoT Central application dashboard to create a new device. You will use the connection information for the newly created device to securely connect your physical device in a later section.
To create a device:
-
From the application dashboard, select Devices on the side navigation menu.
-
Select + New to bring up the Create a new device window.
-
Leave Device template as Unassigned.
-
Fill in the desired Device name and Device ID.
-
Select the Create button.
-
The newly created device will appear in the All devices list. Select on the device name to show details.
-
Select Connect in the top right menu bar to display the connection information used to configure the device in the next section.
The information provided above will create a device with Symmetric Key authentication. If you prefer, you can also use X.509 certificates. In order to do that:
-
In the
Authentication type
box, selectindividual enrollment
-
In the
Authentication method
box, selectCertificates (X.509)
-
Click on the blue folder icon for both primary and secondary and select your device Certificate.
If needed, instructions on how to create an X.509 cert for tests can be found here (Step 1). Please note that you might need to install some of the prerequisites like OpenSSL.
-
-
Note the connection values for the following connection string parameters displayed in Connect dialog. You'll use these values during the following configuration step:
ID scope
Device ID
Primary key
(only if you choose Symmetric Key authentication)
-
Run the Arduino IDE.
-
Install the Azure SDK for Embedded C library.
- On the Arduino IDE, go to menu
Sketch
,Include Library
,Manage Libraries...
. - Search for and install
Azure SDK for C
.
- On the Arduino IDE, go to menu
-
Install ESPRESSIF ESP32 Sensors library for Arduino
- On the Arduino IDE, go to menu
Sketch
,Include Library
,Manage Libraries...
. - Search for and install
Espressif ESP32 Azure IoT Kit Sensors
(by Ewerton Scaboro da Silva).
- On the Arduino IDE, go to menu
-
Open the ESPRESSIF ESP32 DevKit sample.
- On the Arduino IDE, go to menu
File
,Examples
,Azure SDK for C
. - Click on
Azure_IoT_Central_ESP32_AzureIoTKit
to open the sample.
- On the Arduino IDE, go to menu
-
Configure the sample.
Enter your Azure IoT Hub and device information into the sample's
iot_configs.h
:- Add your Wi-Fi SSID to
IOT_CONFIG_WIFI_SSID
- Add your Wi-Fi password to
IOT_CONFIG_WIFI_PASSWORD
- Add you Device Provisioning Scope ID to
DPS_ID_SCOPE
- Add your Device ID to
IOT_CONFIG_DEVICE_ID
- If using X.509 Cert:
- Uncomment the
#define IOT_CONFIG_USE_X509_CERT
- Add your cert to
IOT_CONFIG_USE_X509_CERT
- Add your cert private key to
IOT_CONFIG_DEVICE_CERT_PRIVATE_KEY
- Uncomment the
- If using Symmetric Key:
- Add your device key to
IOT_CONFIG_DEVICE_KEY
- Add your device key to
- Add your Wi-Fi SSID to
-
Connect the ESP32 Azure IoT Kit microcontroller to your USB port.
-
On the Arduino IDE, select the board and port.
- Go to menu
Tools
,Board
and selectESP32 Wrover Module
. - Go to menu
Tools
,Port
and select the port to which the microcontroller is connected.
- Go to menu
-
Upload the sketch.
- Go to menu
Sketch
and click onUpload
.
- Go to menu
-
Monitor the MCU (microcontroller) locally via the Serial Port.
-
Go to menu
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:
... 1970/1/1 00:00:03 [INFO] WiFi connected, IP address: 192.168.1.228 1970/1/1 00:00:03 [INFO] Setting time using SNTP ... 2022/1/18 23:53:17 [INFO] Time initialized! 2022/1/18 23:53:18 [INFO] Azure IoT client initialized (state=1) 2022/1/18 23:53:18 [INFO] MQTT client target uri set to 'mqtts://global.azure-devices-provisioning.net' 2022/1/18 23:53:18 [INFO] MQTT client connecting. 2022/1/18 23:53:19 [INFO] MQTT client connected (session_present=0). 2022/1/18 23:53:19 [INFO] MQTT client subscribing to '$dps/registrations/res/#' 2022/1/18 23:53:19 [INFO] MQTT topic subscribed (message id=48879). 2022/1/18 23:53:19 [INFO] MQTT client publishing to '$dps/registrations/PUT/iotdps-register/?$rid=1{"modelId":"dtmi:azureiot:devkit:freertos:Esp32AzureIotKit;1"}{"registrationId":"myDeviceId","payload":{"modelId":"dtmi:azureiot:devkit:freertos:Esp32AzureIotKit;1"}}' 2022/1/18 23:53:19 [INFO] MQTT message received. 2022/1/18 23:53:19 [INFO] MQTT client publishing to '$dps/registrations/GET/iotdps-get-operationstatus/?$rid=1&operationId=4.8a7f95e72373290a.f936a9b2-12ff-4b7d-8189-4c250236c141' 2022/1/18 23:53:19 [INFO] MQTT message received. 2022/1/18 23:53:22 [INFO] MQTT client publishing to '$dps/registrations/GET/iotdps-get-operationstatus/?$rid=1&operationId=4.8a7f95e72373290a.f936a9b2-12ff-4b7d-8189-4c250236c141' 2022/1/18 23:53:22 [INFO] MQTT message received. 2022/1/18 23:53:22 [INFO] MQTT client being disconnected. 2022/1/18 23:53:22 [INFO] MQTT client target uri set to 'mqtts://myProvisionedIoTHubFqdn.azure-devices.net' 2022/1/18 23:53:22 [INFO] MQTT client connecting. 2022/1/18 23:53:23 [INFO] MQTT client connected (session_present=0). 2022/1/18 23:53:23 [INFO] MQTT client subscribing to '$iothub/methods/POST/#' 2022/1/18 23:53:23 [INFO] MQTT topic subscribed (message id=556). 2022/1/18 23:53:23 [INFO] MQTT client subscribing to '$iothub/twin/res/#' 2022/1/18 23:53:23 [INFO] MQTT topic subscribed (message id=10757). 2022/1/18 23:53:23 [INFO] MQTT client subscribing to '$iothub/twin/PATCH/properties/desired/#' 2022/1/18 23:53:23 [INFO] MQTT topic subscribed (message id=15912). 2022/1/18 23:53:23 [INFO] MQTT client publishing to '$iothub/twin/PATCH/properties/reported/?$rid=0' 2022/1/18 23:53:23 [INFO] MQTT client publishing to 'devices/myDeviceId/messages/events/' 2022/1/18 23:53:23 [INFO] MQTT message received. 2022/1/18 23:53:23 [INFO] Properties update request completed (id=0, status=204) 2022/1/18 23:53:33 [INFO] MQTT client publishing to 'devices/myDeviceId/messages/events/'
-
To view the device status in IoT Central portal:
-
From the application dashboard, select Devices on the side navigation menu.
-
Check the Device status of the device is updated to Provisioned.
-
Check the Device template of the device has updated to Espressif ESP32 Azure IoT Kit.
With IoT Central, you can view the flow of telemetry from your device to the cloud.
To view telemetry in IoT Central portal:
-
From the application dashboard, select Devices on the side navigation menu.
-
Select the device from the device list.
-
View the telemetry as the device sends messages to the cloud in the Overview tab.
You can also use IoT Central to send a command to your device. In this section, you can call a command to toggle LEDs or write to the screen.
To write to the screen:
- Select the Command tab from the device page.
- Locate the Display Text command.
- In the Content textbox, enter the text to be displayed on the screen.
- Select Run.
- The screen on the device will update with the desired text.
To toggle an LED:
- Select the Command tab from the device page.
- Locate the Toggle LED 1 or Toggle LED 2 command
- Select Run.
- An LED light on the device will toggle state.
You can view the device information from IoT Central.
Select About tab from the device page.
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:
- Select the Devices tab for your application.
- Select the device from the device list.
- Select Delete.
To remove the entire Azure IoT Central sample application and all its devices and resources:
- Select Administration > Your application.
- Select Delete.
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.