Skip to content

Develop and Debug with IoT Edge Simulator tool (iotedgehubdev) with C#

Wei Shen edited this page Dec 16, 2019 · 6 revisions

Prerequisites

  • Docker CE

  • Visual Studio Code configured with Azure IoT Tools

    iotedgehubdev

  • .NET Core 2.1 SDK

  • C# for Visual Studio Code extension

Create an Edge Solution Project

  1. View > Command Palette
  2. In the command palette, enter Azure IoT Edge: New IoT Edge solution. FOllow the prompts in the command palette to create your C# solution.
Field Value
Select folder Choose the location on your development machine for VS Code to create the solution files.
Provide a solution name Enter a descriptive name for your solution or accept the default EdgeSolution.
Select module template Choose C# Module.
Provide a module name Name your module CSharpModule.
Provide Docker image repository for the module An image repository includes the name of your container registry and the name of your container image. Your container image is prepopulated from the name you provided in the last step. Replace localhost:5000 with the login server value from your Azure container registry. You can retrieve the login server from the Overview page of your container registry in the Azure portal. The final image repository looks like <registry name>.azurecr.io/csharpmodule.

Folder structure

Add your registry credentials

The environment file(.env) stores the credentials for your container registry. The credential is needed for IoT Edge runtime to get the module from container registry after deploying the solution to Edge devices.

  1. In the VS Code explorer, open the .env file.
  2. Update the fields with the username and password values from your Azure container registry.
  3. Save this file

Select your target architecture

Currently, Visual Studio Code can develop C# modules for Linux AMD64 and Linux ARM32v7 devices. You need to select which architecture you're targeting with each solution, because the container is built and run differently for each architecture type. The default is Linux AMD64.

  1. Open the command palette and search for Azure IoT Edge: Set Default Target Platform for Edge Solution, or select the shortcut icon in the side bar at the bottom of the window.
  2. In the command palette, select the target architecture from the list of options. Here we will use AMD64 as sample.

If you are targeting the Linux ARM32v7, please refer to this doc to setup the remote docker host to an ARM32v7 machine.

Update the module with custom code

  1. Open modules > CSharpModule > Program.cs. This file contains the sample methods to communicate with edgeHub.
  2. Edit the source code or add more source files.
  3. Update modules > CSharpModule > Dockerfile.amd64 and modules > CSharpModule > Dockerfile.amd64.debug if necessary.
  4. If docker build arguments needs to be updated please update modules > CSharpModule > module.json. Please refer to the doc for the description of module.json file.
  5. Add or update the module twin of CSharpModule in deployment.template.json and deployment.debug.template.json

Build and run the solution project with simulator

Setup IoT Edge Simulator

  1. In device explorer on the left side, right-click on your IoT Edge device ID, and then select Setup IoT Edge Simulator to start the simulator with the device connection string.
  2. You can see the IoT Edge Simulator has been successfully set up by reading the progress detail in the integrated terminal.

Build and run

In the Visual Studio Code Explorer view, right-click the deployment.debug.template.json file for your solution and then select Build and Run IoT Edge solution in Simulator. You can watch all the module container logs in the same window. You can also navigate to the Docker view to watch container status. The logs are print in the terminal. If you would like to check the log of each module container, "docker logs" command could be used. logs in terminal

Debug

Attach mode to debug the module running with whole solution

  1. Make sure the module is running under debug mode. (deployment.debug.template.json is used to run the solution which reference the debug version of module image)
  2. Navigate to the Visual Studio Code Debug view and select the debug configuration file for your module. The debug option name should be similar to <your module name> Remote Debug (.Net Core)
  3. Select Start Debugging or press F5. Select the process to attach to.
  4. In Visual Studio Code Debug view, you'll see the variables in the left panel.
  5. To stop the debugging session, first select the Stop button or press Shift + F5, and then select Azure IoT Edge: Stop IoT Edge Simulator from the command palette.

Launch mode to debug single module

  1. Run the command Azure IoT Edge: Start IoT Edge Hub Simulator for Single Module from the Visual Studio Code command palette. When prompted, use the value input1 from the default module code (or the equivalent value from your custom module code) as the input name for your module application. The command starts the edgeHubDev container and a testing utility module container. You can see the outputs below in the integrated terminal if the simulator has been started in single module mode successfully. You can also see a curl command to help send message through. You will use it later.

The edgeHubDev container is the core of the local IoT Edge simulator. It can run on your development machine without the IoT Edge security daemon and provides environment settings for your native module app or module containers. The input container exposes REST APIs to help bridge messages to the target input channel on your module.

  1. Build the module project. In the Visual Studio Code integrated terminal, change the directory to the folder, and then run dotnet build to build .NET Core application.

  2. Add breakpoint in the source code

  3. Navigate to the Visual Studio Code Debug view by selecting View > Debug. Select the debug configuration <your module name> Local Debug (.NET Core) from the dropdown.

Note: If your .NET Core TargetFramework is not consistent with your program path in launch.json, you'll need to manually update the program path in launch.json to match the TargetFramework in your .csproj file so that Visual Studio Code can successfully launch this program.