Quickstart: Connect an ESPRESSIF ESP32-Azure IoT Kit to IoT Central
Applies to: Embedded device development
Total completion time: 30 minutes
In this quickstart, you use the Azure IoT middleware for FreeRTOS to connect the ESPRESSIF ESP32-Azure IoT Kit (from now on, the ESP32 DevKit) to Azure IoT.
You'll complete the following tasks:
- Install a set of embedded development tools for programming an ESP32 DevKit
- Build an 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
Prerequisites
Operating system: Windows 10 or Windows 11
Hardware:
- ESPRESSIF ESP32-Azure IoT Kit
- USB 2.0 A male to Micro USB male cable
- Wi-Fi 2.4 GHz
- An active Azure subscription. If you don't have an Azure subscription, create a free account before you begin.
Prepare the development environment
To set up your development environment, first you install the ESPRESSIF ESP-IDF build environment. The installer includes all the tools required to clone, build, flash, and monitor your device.
To install the ESP-IDF tools:
- Download and launch the ESP-IDF Online installer.
- When the installer prompts for a version, select version ESP-IDF v4.3.
- When the installer prompts for the components to install, select all components.
Create the cloud components
Create the IoT Central application
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 Build on the side navigation menu.
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 case, we recommend that you either create a new device or delete and recreate the device if you want to use an existing device ID.
Select Create app in the Custom app tile.
Add Application Name and a URL.
Select one of the standard pricing plans. Select your Directory, Azure subscription, and Location. To learn about creating IoT Central applications, see Create an IoT Central application. To learn about pricing, see Azure IoT Central pricing.
Select Create. After IoT Central provisions the application, it redirects you automatically to the new application dashboard.
Create a new device
In this section, you use the IoT Central application dashboard to create a new device. You'll 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 Create a device from the All devices pane to open the Create a new device window. (If you're reusing an existing application that already has one or more devices, select + New to open the 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.
Note the connection values for the following connection string parameters displayed in Connect dialog. You'll add these values to a configuration file in the next step:
ID scope
Device ID
Primary key
Prepare the device
To connect the ESP32 DevKit to Azure, you'll modify configuration settings, build the image, and flash the image to the device. You can run all the commands in this section within the ESP-IDF command line.
Set up the environment
To start the ESP-IDF PowerShell and clone the repo:
Select Windows Start, and launch ESP-IDF PowerShell.
Navigate to a working folder where you want to clone the repo.
Clone the repo. This repo contains the Azure FreeRTOS middleware and sample code that you'll use to build an image for the ESP32 DevKit.
git clone --recursive https://github.com/Azure-Samples/iot-middleware-freertos-samples
To launch the ESP-IDF configuration settings:
In ESP-IDF PowerShell, navigate to the iot-middleware-freertos-samples directory that you cloned previously.
Navigate to the ESP32-Azure IoT Kit project directory demos\projects\ESPRESSIF\aziotkit.
Run the following command to launch the configuration menu:
idf.py menuconfig
Add configuration
To add configuration to connect to Azure IoT Central:
In ESP-IDF PowerShell, select Azure IoT middleware for FreeRTOS Main Task Configuration --->, and press Enter.
Select Enable Device Provisioning Sample, and press Enter to enable it.
Set the following Azure IoT configuration settings to the values that you saved after you created Azure resources.
Setting Value Azure IoT Device Symmetric Key {Your primary key value} Azure Device Provisioning Service Registration ID {Your Device ID value} Azure Device Provisioning Service ID Scope {Your ID scope value} Press Esc to return to the previous menu.
To add wireless network configuration:
Select Azure IoT middleware for FreeRTOS Sample Configuration --->, and press Enter.
Set the following configuration settings using your local wireless network credentials.
Setting Value WiFi SSID {Your Wi-Fi SSID} WiFi Password {Your Wi-Fi password} Press Esc to return to the previous menu.
To save the configuration:
- Press S to open the save options, then press Enter to save the configuration.
- Press Enter to dismiss the acknowledgment message.
- Press Q to quit the configuration menu.
Build and flash the image
In this section, you use the ESP-IDF tools to build, flash, and monitor the ESP32 DevKit as it connects to Azure IoT.
Note
In the following commands in this section, use a short build output path near your root directory. Specify the build path after the -B
parameter in each command that requires it. The short path helps to avoid a current issue in the ESPRESSIF ESP-IDF tools that can cause errors with long build path names. The following commands use a local path C:\espbuild as an example.
To build the image:
In ESP-IDF PowerShell, from the iot-middleware-freertos-samples\demos\projects\ESPRESSIF\aziotkit directory, run the following command to build the image.
idf.py --no-ccache -B "C:\espbuild" build
After the build completes, confirm that the binary image file was created in the build path that you specified previously.
C:\espbuild\azure_iot_freertos_esp32.bin
To flash the image:
On the ESP32 DevKit, locate the Micro USB port, which is highlighted in the following image:
Connect the Micro USB cable to the Micro USB port on the ESP32 DevKit, and then connect it to your computer.
Open Windows Device Manager, and view Ports to find out which COM port the ESP32 DevKit is connected to.
In ESP-IDF PowerShell, run the following command, replacing the <Your-COM-port> placeholder and brackets with the correct COM port from the previous step. For example, replace the placeholder with
COM3
.idf.py --no-ccache -B "C:\espbuild" -p <Your-COM-port> flash
Confirm that the output completes with the following text for a successful flash:
Hash of data verified Leaving... Hard resetting via RTS pin... Done
To confirm that the device connects to Azure IoT Central:
In ESP-IDF PowerShell, run the following command to start the monitoring tool. As you did in a previous command, replace the <Your-COM-port> placeholder, and brackets with the COM port that the device is connected to.
idf.py -B "C:\espbuild" -p <Your-COM-port> monitor
Check for repeating blocks of output similar to the following example. This output confirms that the device connects to Azure IoT and sends telemetry.
I (50807) AZ IOT: Successfully sent telemetry message I (50807) AZ IOT: Attempt to receive publish message from IoT Hub. I (51057) MQTT: Packet received. ReceivedBytes=2. I (51057) MQTT: Ack packet deserialized with result: MQTTSuccess. I (51057) MQTT: State record updated. New state=MQTTPublishDone. I (51067) AZ IOT: Puback received for packet id: 0x00000008 I (53067) AZ IOT: Keeping Connection Idle...
Verify the device status
To view the device status in the IoT Central portal:
From the application dashboard, select Devices on the side navigation menu.
Confirm that the Device status of the device is updated to Provisioned.
Confirm that the Device template of the device has updated to Espressif ESP32 Azure IoT Kit.
View telemetry
In IoT Central, you can view the flow of telemetry from your device to the cloud.
To view telemetry in IoT Central:
From the application dashboard, select Devices on the side navigation menu.
Select the device from the device list.
Select the Overview tab on the device page, and view the telemetry as the device sends messages to the cloud.
Send a command to the device
You can also use IoT Central to send a command to your device. In this section, you run commands to send a message to the screen and toggle LED lights.
To write to the screen:
- In IoT Central, select the Commands tab on the device page.
- Locate the Espressif ESP32 Azure IoT Kit / Display Text command.
- In the Content textbox, enter the text you want to send to the device screen.
- Select Run.
- Confirm that the device screen updates with the text.
To toggle an LED:
Select the Command tab on the device page.
Locate the Toggle LED 1 or Toggle LED 2 commands.
Select Run.
Confirm that an LED light on the device toggles on or off.
View device information
You can view the device information from IoT Central.
Select the About tab on the device page.
Tip
To customize these views, edit the device template.
Clean up resources
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 article in this Getting Started content, 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.
Next Steps
In this quickstart, you built a custom image that contains the Azure IoT middleware for FreeRTOS sample code, and then you flashed the image to the ESP32 DevKit device. You also used the IoT Central portal to create Azure resources, connect the ESP32 DevKit securely to Azure, view telemetry, and send messages.
As a next step, explore the following articles to learn more about working with embedded devices and connecting them to Azure IoT.
Feedback
Submit and view feedback for