संपादित करें

इसके माध्यम से साझा किया गया


Tutorial: Connect an ESPRESSIF ESP32-Azure IoT Kit to IoT Hub

In this tutorial, 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 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 CLI to create and manage an Azure IoT hub that the ESP32 DevKit connects to
  • Use Azure IoT Explorer to register a device with your IoT hub, view device properties, view device telemetry, and call direct commands on the device

Prerequisites

  • A PC running Windows 10 or Windows 11
  • Git for cloning the repository
  • Hardware
  • An active Azure subscription. If you don't have an Azure subscription, create a free account before you begin.

Prepare the development environment

Install the tools

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:

  1. Download and launch the ESP-IDF v5.0 Offline-installer.
  2. When the installer lists components to install, select all components and complete the installation.

Clone the repo

Clone the following repo to download all sample device code, setup scripts, and SDK documentation. If you previously cloned this repo, you don't need to do it again.

To clone the repo, run the following command:

git clone --recursive  https://github.com/Azure-Samples/iot-middleware-freertos-samples.git

For Windows 10 and 11, make sure long paths are enabled.

  1. To enable long paths, see Enable long paths in Windows 10.

  2. In git, run the following command in a terminal with administrator permissions:

    git config --system core.longpaths true
    

Create the cloud components

Create an IoT hub

You can use Azure CLI to create an IoT hub that handles events and messaging for your device.

To create an IoT hub:

  1. Launch your CLI app. To run the CLI commands in the rest of this quickstart, copy the command syntax, paste it into your CLI app, edit variable values, and press Enter.

    • If you're using Cloud Shell, right-click the link for Cloud Shell, and select the option to open in a new tab.
    • If you're using Azure CLI locally, start your CLI console app and sign in to Azure CLI.
  2. Run az extension add to install or upgrade the azure-iot extension to the current version.

    az extension add --upgrade --name azure-iot
    
  3. Run the az group create command to create a resource group. The following command creates a resource group named MyResourceGroup in the centralus region.

    Note

    You can optionally set an alternate location. To see available locations, run az account list-locations.

    az group create --name MyResourceGroup --location centralus
    
  4. Run the az iot hub create command to create an IoT hub. It might take a few minutes to create an IoT hub.

    YourIotHubName. Replace this placeholder in the code with the name you chose for your IoT hub. An IoT hub name must be globally unique in Azure. This placeholder is used in the rest of this quickstart to represent your unique IoT hub name.

    The --sku F1 parameter creates the IoT hub in the Free tier. Free tier hubs have a limited feature set and are used for proof of concept applications. For more information on IoT Hub tiers, features, and pricing, see Azure IoT Hub pricing.

    az iot hub create --resource-group MyResourceGroup --name {YourIoTHubName} --sku F1 --partition-count 2
    
  5. After the IoT hub is created, view the JSON output in the console, and copy the hostName value to use in a later step. The hostName value looks like the following example:

    {Your IoT hub name}.azure-devices.net

Configure IoT Explorer

In the rest of this quickstart, you use IoT Explorer to register a device to your IoT hub, to view the device properties and telemetry, and to send commands to your device. In this section, you configure IoT Explorer to connect to the IoT hub you created, and to read plug and play models from the public model repository.

To add a connection to your IoT hub:

  1. Install Azure IoT Explorer. This tool is a cross-platform utility to monitor and manage Azure IoT resources.

  2. In your CLI app, run the az iot hub connection-string show command to get the connection string for your IoT hub.

    az iot hub connection-string  show --hub-name {YourIoTHubName}
    
  3. Copy the connection string without the surrounding quotation characters.

  4. In Azure IoT Explorer, select IoT hubs on the left menu.

  5. Select + Add connection.

  6. Paste the connection string into the Connection string box.

  7. Select Save.

    Screenshot of adding a connection in IoT Explorer.

If the connection succeeds, IoT Explorer switches to the Devices view.

To add the public model repository:

  1. In IoT Explorer, select Home to return to the home view.

  2. On the left menu, select IoT Plug and Play Settings.

  3. Confirm that there's an existing Public Repository entry with an endpoint of https://devicemodels.azure.com.

    Note

    If there's not an entry the public repository, select +Add, select Public repository from the drop-down menu, specify the https://devicemodels.azure.com endpoint value, and then select Save.

    The completed entry for the public repository looks like the following screenshot:

    Screenshot of adding the public model repository in IoT Explorer.

Register a device

In this section, you create a new device instance and register it with the IoT hub you created. You use the connection information for the newly registered device to securely connect your physical device in a later section.

To register a device:

  1. From the home view in IoT Explorer, select IoT hubs.

  2. The connection you previously added should appear. Select View devices in this hub below the connection properties.

  3. Select + New and enter a device ID for your device; for example, mydevice. Leave all other properties the same.

  4. Select Create.

    Screenshot of Azure IoT Explorer device identity.

  5. Use the copy buttons to copy the Device ID and Primary key fields.

Before continuing to the next section, save each of the following values retrieved from earlier steps, to a safe location. You use these values in the next section to configure your device.

  • hostName
  • deviceId
  • primaryKey

Prepare the device

To connect the ESP32 DevKit to Azure, you modify configuration settings, build the image, and flash the image to the device.

Set up the environment

To launch the ESP-IDF environment:

  1. Select Windows Start, find ESP-IDF 5.0 CMD, and run it.

  2. In ESP-IDF 5.0 CMD, navigate to the iot-middleware-freertos-samples directory that you cloned previously.

  3. Navigate to the ESP32-Azure IoT Kit project directory demos\projects\ESPRESSIF\aziotkit.

  4. Run the following command to launch the configuration menu:

    idf.py menuconfig
    

Add configuration

To add wireless network configuration:

  1. In ESP-IDF 5.0 CMD, select Azure IoT middleware for FreeRTOS Sample Configuration --->, and press Enter.

  2. 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}
  3. Select Esc to return to the previous menu.

To add configuration to connect to Azure IoT Hub:

  1. Select Azure IoT middleware for FreeRTOS Main Task Configuration --->, and press Enter.

  2. Set the following Azure IoT configuration settings to the values that you saved after you created Azure resources.

    Setting Value
    Azure IoT Hub FQDN {Your host name}
    Azure IoT Device ID {Your Device ID}
    Azure IoT Device Symmetric Key {Your primary key}

    Note

    In the setting Azure IoT Authentication Method, confirm that the default value of Symmetric Key is selected.

  3. Select Esc to return to the previous menu.

To save the configuration:

  1. Select Shift+S to open the save options. This menu lets you save the configuration to a file named skconfig in the current .\aziotkit directory.
  2. Select Enter to save the configuration.
  3. Select Enter to dismiss the acknowledgment message.
  4. Select 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:

  1. In ESP-IDF 5.0 CMD, 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 
    
  2. 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:

  1. On the ESP32 DevKit, locate the Micro USB port, which is highlighted in the following image:

    Photo of the ESP32-Azure IoT Kit board.

  2. Connect the Micro USB cable to the Micro USB port on the ESP32 DevKit, and then connect it to your computer.

  3. Open Windows Device Manager, and view Ports to find out which COM port the ESP32 DevKit is connected to.

    Screenshot of Windows Device Manager displaying COM port for a connected device.

  4. In ESP-IDF 5.0 CMD, 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
    
  5. 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:

  1. In ESP-IDF 5.0 CMD, 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
    
  2. 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...
    

View device properties

You can use Azure IoT Explorer to view and manage the properties of your devices. In the following sections, you use the Plug and Play capabilities that are visible in IoT Explorer to manage and interact with the ESP32 DevKit. These capabilities rely on the device model published for the ESP32 DevKit in the public model repository. You configured IoT Explorer to search this repository for device models earlier in this tutorial. In many cases, you can perform the same action without using plug and play by selecting IoT Explorer menu options. However, using plug and play often provides an enhanced experience. IoT Explorer can read the device model specified by a plug and play device and present information specific to that device.

To access IoT Plug and Play components for the device in IoT Explorer:

  1. From the home view in IoT Explorer, select IoT hubs, then select View devices in this hub.

  2. Select your device.

  3. Select IoT Plug and Play components.

  4. Select Default component. IoT Explorer displays the IoT Plug and Play components that are implemented on your device.

    Screenshot of the device's default component in IoT Explorer.

  5. On the Interface tab, view the JSON content in the device model Description. The JSON contains configuration details for each of the IoT Plug and Play components in the device model.

    Each tab in IoT Explorer corresponds to one of the IoT Plug and Play components in the device model.

    Tab Type Name Description
    Interface Interface Espressif ESP32 Azure IoT Kit Example device model for the ESP32 DevKit
    Properties (writable) Property telemetryFrequencySecs The interval that the device sends telemetry
    Commands Command ToggleLed1 Turn the LED on or off
    Commands Command ToggleLed2 Turn the LED on or off
    Commands Command DisplayText Displays sent text on the device screen

To view and edit device properties using Azure IoT Explorer:

  1. Select the Properties (writable) tab. It displays the interval that telemetry is sent.

  2. Change the telemetryFrequencySecs value to 5, and then select Update desired value. Your device now uses this interval to send telemetry.

    Screenshot of setting telemetry interval on the device in IoT Explorer.

  3. IoT Explorer responds with a notification.

To use Azure CLI to view device properties:

  1. Run the az iot hub device-twin show command.

    az iot hub device-twin show --device-id mydevice --hub-name {YourIoTHubName}
    
  2. Inspect the properties for your device in the console output.

Tip

You can also use Azure IoT Explorer to view device properties. In the left navigation select Device twin.

View telemetry

With Azure IoT Explorer, you can view the flow of telemetry from your device to the cloud. Optionally, you can do the same task using Azure CLI.

To view telemetry in Azure IoT Explorer:

  1. From the IoT Plug and Play components (Default Component) pane for your device in IoT Explorer, select the Telemetry tab. Confirm that Use built-in event hub is set to Yes.

  2. Select Start.

  3. View the telemetry as the device sends messages to the cloud.

    Screenshot of device telemetry in IoT Explorer.

  4. Select the Show modeled events checkbox to view the events in the data format specified by the device model.

    Screenshot of modeled telemetry events in IoT Explorer.

  5. Select Stop to end receiving events.

To use Azure CLI to view device telemetry:

  1. Run the az iot hub monitor-events command. Use the names that you created previously in Azure IoT for your device and IoT hub.

    az iot hub monitor-events --device-id mydevice --hub-name {YourIoTHubName}
    
  2. View the JSON output in the console.

    {
        "event": {
            "origin": "mydevice",
            "module": "",
            "interface": "dtmi:azureiot:devkit:freertos:Esp32AzureIotKit;1",
            "component": "",
            "payload": "{\"temperature\":28.6,\"humidity\":25.1,\"light\":116.66,\"pressure\":-33.69,\"altitude\":8764.9,\"magnetometerX\":1627,\"magnetometerY\":28373,\"magnetometerZ\":4232,\"pitch\":6,\"roll\":0,\"accelerometerX\":-1,\"accelerometerY\":0,\"accelerometerZ\":9}"
        }
    }
    
  3. Select CTRL+C to end monitoring.

Call a direct method on the device

You can also use Azure IoT Explorer to call a direct method that you implemented on your device. Direct methods have a name, and can optionally have a JSON payload, configurable connection, and method timeout. In this section, you call a method that turns an LED on or off. Optionally, you can do the same task using Azure CLI.

To call a method in Azure IoT Explorer:

  1. From the IoT Plug and Play components (Default Component) pane for your device in IoT Explorer, select the Commands tab.

  2. For the ToggleLed1 command, select Send command. The LED on the ESP32 DevKit toggles on or off. You should also see a notification in IoT Explorer.

    Screenshot of calling a method in IoT Explorer.

  3. For the DisplayText command, enter some text in the content field.

  4. Select Send command. The text displays on the ESP32 DevKit screen.

To use Azure CLI to call a method:

  1. Run the az iot hub invoke-device-method command, and specify the method name and payload. For this method, setting method-payload to true means the LED toggles to the opposite of its current state.

    az iot hub invoke-device-method --device-id mydevice --method-name ToggleLed2 --method-payload true --hub-name {YourIoTHubName}
    

    The CLI console shows the status of your method call on the device, where 200 indicates success.

    {
      "payload": {},
      "status": 200
    } 
    
  2. Check your device to confirm the LED state.

Troubleshoot and debug

If you experience issues building the device code, flashing the device, or connecting, see Troubleshooting.

For debugging the application, see Debugging with Visual Studio Code.

Clean up resources

If you no longer need the Azure resources created in this quickstart, you can use the Azure CLI to delete the resource group and all of its resources.

Important

Deleting a resource group is irreversible. The resource group and all the resources contained in it are permanently deleted. Make sure that you do not accidentally delete the wrong resource group or resources.

To delete a resource group by name:

  1. Run the az group delete command. This command removes the resource group, the IoT Hub, and the device registration you created.

    az group delete --name MyResourceGroup
    
  2. Run the az group list command to confirm the resource group is deleted.

    az group list
    

Next steps

In this tutorial, 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 connected the ESP32 DevKit to Azure IoT Hub, and carried out tasks such as viewing telemetry and calling methods on the device.

As a next step, explore the following article to learn more about embedded development options.