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:
- Develop IoT Edge modules with Linux containers
- Develop and deploy a Python IoT Edge module using Linux containers
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.
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.
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:
-
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.
-
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:
- As we are using a Linux container, ensure that the notification "Linux Containers Hyper-V backend is launching..." is displayed.
- 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:
-
Download the Python installer.
Note: To maintain compatibility with the Python module template, download Python 3.7.
-
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:
-
Download the installer for Visual Studio Code from the official website.
Note: Ensure that you select a version that is compatible with your computer.
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.-
To install Azure IoT Tools and Python extension features, on the left pane, click Extensions and perform the following operations:
-
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.
-
In the search bar, type
Python
and click Install on the corresponding search result.Python extension is downloaded and installed on Visual Studio Code.
-
-
To sign in to Azure, in the Command Palette, type
Azure: Sign In
and select the corresponding search result. -
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.
-
To select the IoT Hub, in the Command Palette, type
Azure IoT Hub: Select IoT Hub
and select the corresponding search result. -
Follow the instructions displayed and select Azure subscription and IoT Hub.
-
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.
Creating and deploying a module
To create, deploy and test an IoT Edge module, you must complete the following steps:
- Create container registry
- Create project
- Add registry credentials
- Select target architecture
- Update module with custom code
- Build and push module
- Deploy Simulated Temperature Sensor module
- Deploy Python module
- 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:
- Sign-in to Azure Portal.
- In the upper-left corner of the page, click + Create a resource.
-
From the left panel, select Containers.
From the list of displayed services, select Container Registry.
-
In the Information tab, specify the following settings information:
- Subscription
- Resource group
- Registry name
- Location
- SKU
>Note: SKU must be set to
Basic
.
Click the Review + create tab, and verify the setting information.
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:
- Open Visual Studio Code.
-
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.
Specify the location where you want to save the project.
In the Provide a Solution Name box that appears in the Command Palette, specify a solution name.
From the Select Module Template drop-down list that appears in the Command Palette, specify Python Module.
-
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.
-
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.
-
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>
.The project template is created with the folder structure as shown below.
Adding registry credentials
Follow these steps to add the registry credentials:
-
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>=
-
In the
CONTAINER_REGISTRY_USERNAME_<registry name>
andCONTAINER_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>
Note: The password provided can be either password or password2.
Selecting target architecture
Follow these steps to select the target architecture:
-
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.
-
Since we are using the
armhf
architecture, selectarm32v7
.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.
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:
-
From the menu bar, select View > Terminal to display the terminal pane.
-
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. -
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.
-
To verify that the images are pushed to the Container Registry, perform these steps:
- Open Azure Portal.
- Navigate to the Container Registry.
- On the left pane, click Repositories.
The repositories page appears, displaying a list of images.
- Verify that your image is present in this list.
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:
- Open Azure Portal.
- Navigate to the IoT Hub that you created.
- From the left pane, under the Device management section, click IoT Edge.
-
On the right pane, click the device ID of the target device on which you want to deploy the module.
-
Click Set modules tab.
The Set modules on device page appears.
-
In the IoT Edge Modules section, click the Add drop-down menu, and select Marketplace Module.
The IoT Edge Module Marketplace page appears.
-
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
. -
Click the Review + create tab.
The deployment settings in the standard JSON format is displayed.
-
In the upper-left corner of the page, ensure that the message
Validation passed
is displayed and then click Create.
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:
- Open Azure Portal.
- Navigate to the IoT Hub that you created.
- From the left pane, under the Device management section, click IoT Edge.
- On the right pane, click the device ID of the target device on which you want to deploy the module.
-
Click Set modules tab.
-
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 To obtain the Registry name, Login Server details, username and password, refer to step 7 in Creating a new project.
-
To configure the settings of the IoT Edge module, click +Add and then select IoT Edge Module from the drop-down list.
-
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) -
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’) -
Click Review + create and verify the configuration information.
In the upper-left corner of the screen, the message "Validation passed" is displayed.
-
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.
-
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:
-
Open the project created in Creating a new project in Visual Studio Code.
-
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.
-
To stop receiving messages, in the lower-left corner of the window, click Stop Monitoring built-in event endpoint.
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:
- Open Azure Portal and navigate to the IoT Hub that you created.
- From the Device management category, display the IoT Edge device.
- Click the device ID of the target device.
- Click Set modules.
-
From the list of IoT Edge modules, select the Python module that you deployed.
The Update IoT Edge Module page appears.
Click the Module Twin Settings tab.
In the editor, enter the
TemperatureThreshold
details in JSON format.-
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.
-
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.
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:
- Navigate to the images stored in the container registry as shown in step 4 of Build and Push modules.
-
Select the ID of the module you want to deploy.
The repository details are displayed.
-
From the Docker pull command box, the content displayed after
docker pull
is the image URI.
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
.
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.
- Open Visual Studio Code.
-
From the menu bar, select File > User settings > Settings.
The Settings page is displayed.
-
To display the proxy settings, in the search box, type
proxy
.The
Http:Proxy
search result is displayed. -
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:
-
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.
Specify the proxy information in
config.json
by following the procedure mentioned in Configure the Docker Client.-
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
andhttpsProxy
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. -
To configure the proxy settings in Docker Desktop, perform these steps:
- On the toolbar, click the arrow to open the tool tray
- Right-click the Docker Desktop icon and select Settings.
- Expand Resources and select PROXIES.
- In the Web Server (HTTP) box, specify the proxy URL.
- In the Secure Web Server (HTTPS) box, specify the proxy URL.
- Click Apply & Restart.
The proxy settings are configured.
Top comments (0)