Basic IoT Edge Development Workflow for Simulated Devices

These are just some initial notes related to the workflow for developing simulated devices. This requires the use of the following tools:

  • VS Code
    • Azure IoT Tools
    • C#
    • Docker
  • Azure IoT Hub
  • Azure Container Registry

This is the goal. Work locally using VS Code to develop a custom module. Test and debug that module locally using the IoT Edge Simulator. View simulated telemetry in IoT Hub from local code. Publish completed component to Container Registry. Pull code from registry and run in IoT Hub.

The basic flow from system to system looks like this:

Basic flow of an IoT Edge Module from development/testing/debugging to a Docker image to a simulated device.

This won’t be a complete step-by-step. I’m primarily capturing these notes so that I can improve this workflow. However, if you want to walk through the whole process there is a lab that will take you step-by-step here:

AZ-220-Microsoft-Azure-IoT-Developer

Step 1 – Install Azure IoT EdgeHub Dev Tools

You’ll need Python on your development machine. If you don’t have pip installed, you’ll need that as well. You will need to install the IoT EdgeHub tools using this command:

pip install iotedgehubdev

More information about the project is located here:

iotedgehubdev

Step 2 – Create an Azure Container Registry

Azure Container Registry provides the storage of private Docker images for container deployment. We will need it to provide a place to securely store our docker image.

If you do not already have a container registry, create one in Azure by searching the marketplace for Container Registry. Create one and give it a meaningful name. Use a Standard SKU.

Once it is created you will need to go to settings and Access Keys in order to set it for Administrator mode. Under Admin Users click Enable.

Copy the following values, you will need them:

  • Login server
  • Username
  • Password

Log into Docker using your new server and credentials:

docker login <loginserver>

Step 3 – Create a Custom Edge Module

For VS Code there are a few commands you need to get this solution created. In the command palette, enter the command Azure IoT Edge: New and then click the presented Azure IoT Edge: New IoT Edge Solution. Give it a name and select a language (C#, Java, Python). When prompted to name the IoT Edge Module select something meaningful for the device, like RemainingLifeEstimator.

Once this is created, replace the Docker localhost with the address of the Container Registry. It follows a special format:

<acr-name>.azurecr.io/<module-name>

Once the new IoT Edge Solution has been created prepare to configure the solution for work.

  • be sure to set the env variables for your container registry username and password
  • set the default target platform by typing Azure IoT Edge: Set Default Target Platform for Edge Solution*

Step 4 – Debug in Attach Mode with IoT Edge Simulator

To me this is one of the coolest parts of this solution. You can both test this locally and see the results of it on IoT Hub. This allows you to see that the code you are writing is working as expected from both the device side and the cloud side. It’s worth all the effort to get here.

Create an IoT Edge test device by capturing a connection string from IoT Hub. Go to the IoT Edge portion of the Automatic Device Provision section on the navigation blade. Add an IoT Edge device and give it a good test name. Set it to authenticate with the Symmetric key.

Start testing by right clicking on the deployment.debug.template.json document in your solution. Select the Build and Run IoT Edge Solution in Simulator.

If you haven’t set up the iotedgehubdev first, you will be walked through the process here. Once is is set up you can run and build the project.

Once it is up and running, you can see the telemetry you’ve simulated in the terminal window. Additionally, you can use the following command in the Azure Cloud Shell for your IoT Hub:

az iot hub monitor-events --hub-name "<your-iot-hub>"

You can now debug the module using VS Code debugging tools.

Step 5 – Deploy the IoT Edge Solution

Publish the module to the Azure Container Registry. Insure that your Container Registry credentials are stored in the .env file.

Right click on the deployment.template.json and then click Build and Push IoT Edge Solution.

At the Azure portal, open your Azure Container Registry service and click your container server.

You should see your container under the repositories. From here you’ll want to grab the repository name and the tag properties. Save these values in the following format somewhere (Notepad)

<repository-name>:<tag>

Configure an IoT Edge Device to use the module by adding a new IoT Edge device in the IoT Hub. Give a device id that matches the module name. Then set modules and on the modules blade under Container Registry Settings, enter the values for the registry name, address to the login server, and the username and password.

On the Add IoT Edge Module pane, under IoT Edge Module Name, enter the name of your module. Under module settings enter the full uri and tagged name of your module’s docker image:

<container-registry-login-server>/<repository-name>:<tag>

Add that and then set the module routes. Typically you want a route to the module from other modules and a route to IoT Hub from the module. It could look something like this:

Route Settings

Create this and you now have an Edge Module deployed to an IoT Edge Device.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s