DEV Community

Yokogawa-BrigitteZacharia for Yokogawa Technologies Solutions India

Posted on • Updated on

Deploying a sample Python module on an Edge Device installed with Azure IoT Edge Runtime

Introduction

Azure IoT Runtime enables you to collect information and execute commands on Edge devices remotely. When installed on e-RT3 Plus, the features of both e-RT3 Plus and Azure Runtime Environment can be utilized to perform various operations.

This is part one of a five-part series that demonstrates how to use Azure Runtime Environment with e-RT3 Plus. In this article, we explore how to deploy a sample Python module on e-RT3 Plus and Raspberry Pi 4 Model B and update the values from Azure Portal.

We deploy two IoT Modules; a Simulated Temperature Sensor module that generates sample temperature values, and a Python module that uploads the temperature values to the IoT Hub whenever the temperature value crosses a threshold. We can view the uploaded temperature values on Visual Studio Code.

This demonstration is based on two Microsoft tutorials:

However, it is customized to show the procedure for the ARM 32-bit device.

Environment

Supported devices(OS)

  • e-RT3 Plus F3RP70-2L(Ubuntu 18.04 32-bit)
  • Raspberry Pi 4 Model B (Ubuntu Server 18.04 32-bit)

The armhf architecture package runs on these devices.
The modules are developed on a computer installed with Windows 10.

Workflow

The following figure shows the workflow for deploying a sample python module on e-RT3 Plus using Azure IoT runtime.

workflow

Prerequisites

Azure Runtime environment must be installed on the e-RT3 Plus or the Edge device that you are using. For more information about deploying Azure Runtime Environment on e-RT3 Plus, refer to Deploying Azure Runtime Environment on e-RT3 Plus.

The following software must be installed and configured before you start creating the sample Python module.

  1. Container Engine (Docker Desktop)
  2. Python
  3. Visual Studio Code

Setting up the Container Engine

You must set up a container engine on the computer in which you want to develop the Python module. In this demonstration, we install the Docker Desktop container engine.

Note: If your computer is in an environment that requires proxies to connect to the internet, you must configure the Proxy settings.

Follow these steps to set up Docker Desktop on your computer:

  1. Download Docker Desktop. For Windows Home and other operating systems, select the corresponding OS from the menu on the left and continue the installation. Ensure that you enable Hyper-V backend during installation.

    For more information on how to install Docker Desktop, refer to Install Docker Desktop on Windows.

    Note: Ensure that your computer meets the system requirements specified on Install Docker Desktop on Windows before installing Docker Desktop.

  2. After the installation is complete, launch Docker Desktop from the Start menu.
    A notification appears, indicating that Docker Desktop is running and the container engine setup is complete. Ensure that Docker Desktop is successfully launched before proceeding.

    Note:

    1. As we are using a Linux container, ensure that the notification "Linux Containers Hyper-V backend is launching..." is displayed.
    2. Docker Desktop may fail to launch if the computer load is high. In this case, retry launching.

Installing Python

Follow these steps to install Python on your computer:

  1. Download the Python installer.

    Note: To maintain compatibility with the Python module template, download Python 3.7.

  2. Open the installer and click Install Now.
    Python is installed on your computer.

    Note: In the installer dialog box, select the Add Python 3.7 to PATH check box.

Setting up Visual Studio Code

Visual Studio Code is used for developing the IoT Edge module.

Follow these steps to install and configure Visual Studio Code:

  1. Download the installer for Visual Studio Code from the official website.

    Note: Ensure that you select a version that is compatible with your computer.

  2. Start the installer and follow the steps in the installation wizard to complete the installation.
    If the installation is completed successfully, a message is displayed indicating the same.

  3. To install Azure IoT Tools and Python extension features, on the left pane, click Extensions and perform the following operations:

    1. In the search bar, type Azure IoT Tools and click Install on the corresponding search result.

      Azure IoT Tools extension is downloaded and installed on Visual Studio Code.

      01_IntsallAzureExtension

    2. In the search bar, type Python and click Install on the corresponding search result.

      Python extension is downloaded and installed on Visual Studio Code.

      02_InstallPythonExtension

  4. To sign in to Azure, in the Command Palette, type Azure: Sign In and select the corresponding search result.

    03_SignIn

  5. Follow the instructions displayed to sign in.

    If you have successfully signed in, your account information is displayed at the bottom of the Visual Studio Code window.

    04_Accountdetails

  6. To select the IoT Hub, in the Command Palette, type Azure IoT Hub: Select IoT Hub and select the corresponding search result.

    05_SelectIoTHub

  7. Follow the instructions displayed and select Azure subscription and IoT Hub.

    06_SelectSubscription

    07_iotHub

  8. To view the IoT Hub and connected devices, on the left pane, select the file explorer and expand AZURE IOT HUB.

    The IoT Hub is displayed and the connected devices are listed under the Devices section.

    08_viewDevices

Creating and deploying a module

To create, deploy and test an IoT Edge module, you must complete the following steps:

  1. Create container registry
  2. Create project
  3. Add registry credentials
  4. Select target architecture
  5. Update module with custom code
  6. Build and push module
  7. Deploy Simulated Temperature Sensor module
  8. Deploy Python module
  9. Verify the module operation

Creating container registry

To store the docker images, any registry that is compatible with docker can be used. In this demonstration, we use the Azure Container Registry.

Note: Azure Container Registry is a paid service. For the pricing details, click here.

Follow these steps to create a container registry:

  1. Sign-in to Azure Portal.
  2. In the upper-left corner of the page, click + Create a resource.
  3. From the left panel, select Containers.

    09_ContainerRegistry

  4. From the list of displayed services, select Container Registry.

  5. In the Information tab, specify the following settings information:

    1. Subscription
    2. Resource group
    3. Registry name
    4. Location
    5. SKU >Note: SKU must be set to Basic.
  6. Click the Review + create tab, and verify the setting information.

  7. If the settings are configured correctly, click Create.

Creating a new project

Follow these steps to create a new Edge IoT solution in Visual Studio Code:

  1. Open Visual Studio Code.
  2. In the Command Palette, type Azure IoT Edge: New IoT Edge solution and select the same from the search results.

    The folder selection box appears.

  3. Specify the location where you want to save the project.

  4. In the Provide a Solution Name box that appears in the Command Palette, specify a solution name.

  5. From the Select Module Template drop-down list that appears in the Command Palette, specify Python Module.

  6. In the Provide a Module Name box that appears in the Command Palette, specify a name for the module.

    The module name specified is reflected within the template and is considered as the module name in IoT Edge. Here, we have specified the name PythonModule.

    Note: The module name of IoT Edge can also be modified individually before building the images.

  7. In the Provide Docker Image Repository box that appears in the Command Palette, specify the address of the Container Registry login server.

    By default localhost:5000 appears.

    To obtain the login server information, open Azure Portal and navigate to the Container registry that you created in previous step. On the left pane, under the Settings section, select Access keys. The login server information is displayed on the right.

    Note: Ensure to save this information as it is required at a later stage.

    10_AccessKeys
    13_AccessKeys

  8. Copy the Login server information <registry_name>.azurecr.io and paste it in the Command Palette in the format <registry_name>.azurecr.io/<module_name>.

    11_ModuleName

    The project template is created with the folder structure as shown below.

    12_ProjectFolderStructure

Adding registry credentials

Follow these steps to add the registry credentials:

  1. To add the registry credentials, on the left pane, select the file explorer and open the .env file under the PythonModule folder.

    .env
    CONTAINER_REGISTRY_USERNAME_<registry name>=
    CONTAINER_REGISTRY_PASSWORD_<registry name>=
    
  2. In the CONTAINER_REGISTRY_USERNAME_<registry name> and CONTAINER_REGISTRY_PASSWORD_<registry name> fields, specify the username and password, i.e.: the Access keys of the Container Registry.

    .env
    
    CONTAINER_REGISTRY_USERNAME_<registry name>=<username>
    CONTAINER_REGISTRY_PASSWORD_<registry name>=<password or password2>
    

    13_AccessKeys

Note: The password provided can be either password or password2.

Selecting target architecture

Follow these steps to select the target architecture:

  1. Click the current architecture at the bottom of the Visual Studio Code window.

    The Select Azure IoT Edge Solution Default Platform box appears in the Command Palette.

    14_targetArchitecture

  2. Since we are using the armhf architecture, select arm32v7.

    The current architecture changes to arm32v7.

    Note: If the current architecture is not displayed at the bottom of the window, type Azure IoT Edge: Set Default Target Platform for Edge Solution and select the same.
    14_1_targetArchitecture

Updating module with custom code

Rewrite the contents of main.py and deployment.template.json by following the steps in Update Modules with Custom Code, and save the file.

The Python module routes temperature values to the IoT Hub only if the value exceeds the defined threshold.

Building and pushing the module

Follow these steps to build the created module and push the image to Container Registry:

  1. From the menu bar, select View > Terminal to display the terminal pane.

    15_Terminal

  2. Run the following command in the terminal to log in to the Container Registry.

    docker login -u <username> -p <password or password2> <login server>
    

    Note: If the required proxy settings are not configured then you will not be able to log into the Container Registry.
    For details about proxy settings, refer to Proxy settings.

  3. On the left pane, select File Explorer, right-click deployment.template.json, and select Build and Push IoT Edge Solution.

    The module is built and if there are no errors the image is pushed to the Container Registry.

    16_BuildandPush

  4. To verify that the images are pushed to the Container Registry, perform these steps:

    1. Open Azure Portal.
    2. Navigate to the Container Registry.
    3. On the left pane, click Repositories.

    The repositories page appears, displaying a list of images.

    1. Verify that your image is present in this list. 17_imageList

Deploying Simulated Temperature Sensor module

The Simulated Temperature Sensor module generates sample temperature readings periodically which increase in value over time. This module is readily available for deployment in Azure Marketplace.

We will deploy this module and route the generated temperature values as input to the Python module.

Follow these steps to deploy the Simulated Temperature Sensor:

  1. Open Azure Portal.
  2. Navigate to the IoT Hub that you created.
  3. From the left pane, under the Device management section, click IoT Edge.
  4. On the right pane, click the device ID of the target device on which you want to deploy the module.

    SetModules

  5. Click Set modules tab.

    The Set modules on device page appears.

    addmoduleMarketplace

  6. In the IoT Edge Modules section, click the Add drop-down menu, and select Marketplace Module.

    The IoT Edge Module Marketplace page appears.

    marketplace

  7. In the search box, type simulated temperature sensor and select the same from the search results.

    The module is added to the IoT Edge Modules section with the Desired Status as running.

    modulerunning

  8. Click the Review + create tab.

    The deployment settings in the standard JSON format is displayed.

  9. In the upper-left corner of the page, ensure that the message Validation passed is displayed and then click Create.

    SetModules

The device details page appears, displaying the deployment status of the Simulated Temperature Sensor on the Modules tab.

Note: If the Edge device is in an environment that requires proxies to connect to the internet, the telemetry sent to IoT Hub may be blocked and not delivered even when the module is correctly deployed and working.
In such cases, you must configure the Proxy settings for $edgeHub module. For more information about the proxy settings refer to Configure proxy support.

Deploying Python module

After pushing the images to the Container Registry, we must deploy it on the Edge device.

Follow these steps to deploy the images from Azure Portal:

  1. Open Azure Portal.
  2. Navigate to the IoT Hub that you created.
  3. From the left pane, under the Device management section, click IoT Edge.
  4. On the right pane, click the device ID of the target device on which you want to deploy the module.
  5. Click Set modules tab.

    18_SetModules

  6. In the Modules tab, specify the details mentioned in the following table.

    Setting items of module Information to be entered
    NAME Registry name
    ADDRESS Login server
    USERNAME User name
    PASSWORD Password or password2

    19_Modules

    To obtain the Registry name, Login Server details, username and password, refer to step 7 in Creating a new project.

  7. To configure the settings of the IoT Edge module, click +Add and then select IoT Edge Module from the drop-down list.

    20_IoTEdgeModule

  8. Configure the parameters of the IoT Edge module as described in the following table and click Add.

    Setting item of module Information to be entered
    IoT Edge Module Name The module name
    Image URI Image URI obtained from the repository of Container Registry. For information about how to obtain the image URI, refer to Image URI.
    Restart Policy Always (retain default settings)
    Desired Status Running (retain default settings)
  9. Click the Routes tab and configure the details as described in the following table.

    NAME VALUE
    PythonModuleToIoTHub FROM /messages/modules/PythonModule/outputs/* INTO $upstream
    sensorToPythonModule FROM /messages/modules/SimulatedTemperatureSensor/outputs/temperatureOutput INTO BrokeredEndpoint(‘/modules/PythonModule/inputs/input1’)

    22_SetModules

  10. Click Review + create and verify the configuration information.

    In the upper-left corner of the screen, the message "Validation passed" is displayed.

    23_reviewCreate1
    24_reviewCreate2
    25_reviewCreate3

  11. After verifying the configuration information, in the lower-left corner of the page, click Create.

    The Device settings page appears, and the module list is displayed. It usually takes some time to complete the deployment.

    26_ModuleList

  12. Click Refresh.

    The Module list is updated and you can see the module that you deployed to the Edge device added to the Modules list.

Verifying module operation

After viewing the telemetry messages received from the Edge device in the IoT Hub, we can confirm that the deployed module is working.

In this section, we describe how you can verify module operation by using Visual Studio Code.

Follow these steps to verify the reception of telemetry messages at the IoT Hub:

  1. Open the project created in Creating a new project in Visual Studio Code.

    27_StartMonitoring

  2. On the left pane, right-click your Azure IoT Device, and select Start Monitoring Built-in Event Endpoint

    The messages received at the IoT Hub are displayed in the OUTPUT tab.

    28_receivedOutput

  3. To stop receiving messages, in the lower-left corner of the window, click Stop Monitoring built-in event endpoint.

    29_StopMonitoring

Note: The maximum number of messages that can be sent in the case of the Simulated Temperature Sensor is 500. If you want to send more messages, you must restart the module by running the following command in the terminal:

      sudo iotedge restart SimulatedTemperatureSensor

You can restart the other module by replacing SimulatedTemperatureSensor with the required module ID.

      sudo iotedge restart <Module ID>

Editing Module Twin

By editing the module twin, the features of the sample module used can be edited without having to build or deploy the module again.
In this example, let us edit the module twin of the deployed Python module to change the threshold value of the temperature of the machine that notifies IoT Hub.

Follow these steps to edit the module twin from Azure Portal:

  1. Open Azure Portal and navigate to the IoT Hub that you created.
  2. From the Device management category, display the IoT Edge device.
  3. Click the device ID of the target device.
  4. Click Set modules.
  5. From the list of IoT Edge modules, select the Python module that you deployed.

    The Update IoT Edge Module page appears.

  6. Click the Module Twin Settings tab.

  7. In the editor, enter the TemperatureThreshold details in JSON format.

  8. In the lower-left corner of the screen, click Apply.

    The following example shows how to set the threshold value of the machine temperature to 40 degrees.

    33_editTwin

  9. Click Review + Create.

    The verification page appears, displaying the configured deployment in standard JSON format. You can view the updated temperature threshold here.

    After confirming the deployment configuration and checking for the "Validation passed" message in the upper-left corner of the screen, click Create.

    34_ReviewEditModule

When you view the data generated by following the same procedure as Verifying module operation, you can see that the new threshold value has come into effect.

For example, if you changed the temperature threshold from 30 to 40, you will see that the Python module stops sending messages until the generated temperature values start exceeding 40℃. Subsequently, on the OUTPUT tab of Visual Studio Code, the messages are displayed only when the temperature values exceed 40℃.

Conclusion

As you have seen, the messages sent by the Edge device are received at the IoT Hub. Additionally, updates to the Twin Module in Azure Portal are reflected in the Edge device.

In our future articles, we will explore how to collect data using a Python module in Azure IoT Edge and send it to an IoT Hub.

Appendix

Image URI

To obtain the image URI, follow these steps:

  1. Navigate to the images stored in the container registry as shown in step 4 of Build and Push modules.
  2. Select the ID of the module you want to deploy.

    The repository details are displayed.

  3. From the Docker pull command box, the content displayed after docker pull is the image URI.
    34_DockerPull

    The image URI is in the format <registry name>.azurecr.io/<module name>:<tag>.

    For example in the following image, the URI is <registry name>.azurecr.io/pythonmodule:0.0.1-arm32v7.

    21_AddIoTEdgeModule

Proxy settings

If you are using a proxy server in your environment then you must configure the proxy settings for Visual Studio Code and Docker Desktop.

The proxy settings described in this section are an example of what was used for this demonstration. Depending on your environment, you must configure the proxy settings as necessary.

Visual Studio Code

Proxy settings for Visual Studio Code are required when logging in from Visual Studio Code to Azure.

Follow these steps to configure the proxy settings in Visual Studio Code.

  1. Open Visual Studio Code.
  2. From the menu bar, select File > User settings > Settings.

    The Settings page is displayed.

  3. To display the proxy settings, in the search box, type proxy.

    The Http:Proxy search result is displayed.

    35_VSCodeProxy

  4. In the Http:Proxy box, specify the proxy URL based on your environment. For example, http://username:password@example.com:port/

    The proxy information is saved in the settings file in the following location.
    C:\Users\username\AppData\Roaming\Code\User\settings.json

    For environment details, contact your network administrator.

Docker

Proxy settings for Docker are required when logging in to Container Registry from the Visual Studio Code Terminal and pushing images.

For more information about configuring proxy settings in Docker, refer to the Docker official document.

Follow these steps to configure the proxy settings:

  1. Use any editor to open the config.json file from the following folder location. C:\Users\username\.docker.

    Note: If the file does not exist, create it.

  2. Specify the proxy information in config.json by following the procedure mentioned in Configure the Docker Client.

  3. If config.json is already created, there is a possibility that settings other than Proxy settings are specified in the file. In such cases, specify the configuration such that the label name is the same as the already existing labels.

    The following shows the httpProxy and httpsProxy configurations used in this demonstration.

       {
            "HttpHeaders":
            {
                "User-Agent":"Docker-Client/19.03.13 (windows)"
            },
            "auths":
            {
                "<registry name 1>.azurecr.io":{},
                "https://index.docker.io/v1/":{},
                "<registry name 2>.azurecr.io":{}
            },
            "credStore":"desktop",
            "credsStore":"desktop",
            "proxies":
            {
                "default":     
                {
                    "httpProxy":"http://username:password@example.com:port/",
                    "httpsProxy":"http://username:password@example.com:port/"
                }
            },
            "stackOrchestrator":"swarm"
        }
    

    As you can see, some of the configurations already existed. Here, we have added the proxies configuration.

  4. To configure the proxy settings in Docker Desktop, perform these steps:

    36_docker

    1. On the toolbar, click the arrow to open the tool tray
    2. Right-click the Docker Desktop icon and select Settings.
    3. Expand Resources and select PROXIES.
    4. In the Web Server (HTTP) box, specify the proxy URL.
    5. In the Secure Web Server (HTTPS) box, specify the proxy URL.
    6. Click Apply & Restart.

The proxy settings are configured.

References

  1. Real-time OS controller e-RT3 Plus F3RP70-2L
  2. Azure Certified Device catalog
  3. Tutorial: Develop IoT Edge module for Linux device
  4. Tutorial: Develop and deploy Python IoT Edge module for Linux device
  5. Configure Docker to use a proxy server

Top comments (0)