Simulate a Module to IoT Edge Runtime via IoT Hub

The IoT Edge Runtime is a set of packages that flip a tool into an IoT Edge device. The primary responsibilities of an IoT Edge Runtime are Module Management and communication between IoT Devices.

In this article, we will try to simulate a module to IoT Edge Runtime via IoT Hub

Pre-requisites

  1. Install the .NET Core SDK.
  2. Install Azure CLI.
  3. Add IoT Extension to Azure CLI
az extension add --name azure-iot

Create Cloud Resources

A resource group to manage all the resources you use in this quickstart.

az group create --name IoTEdgeResources --location westus2

Create IoT Edge Device

We would create a Windows virtual machine to act as your IoT Edge device. A virtual machine can be created using the following command, replacing {password} with a secure password:

az vm create --resource-group IoTEdgeResources --name EdgeVM --image MicrosoftWindowsDesktop:Windows-10:rs5-pro:latest --admin-username azureuser --admin-password {password} --size Standard_DS1_v2 

Create IoT Hub

The below code creates an IoT hub in the resource group IoTEdgeResources. Replace {hub_name} with a unique name for your IoT hub.

az iot hub create --resource-group IoTEdgeResources --name {hub_name} --sku F1 --partition-count 2

Register IoT Edge Device to IoT Hub

In the Azure CLI, execute the following command to create a device named EdgeVM in your hub.

az iot hub device-identity create --device-id EdgeVM --hub-name {hub_name} --edge-enabled

Retrieve the connection string for your device, which links your physical device with its identity in IoT Hub.

az iot hub device-identity show-connection-string --device-id EdgeVM --hub-name {hub_name}

Copy the value of the connectionString key from the JSON output and save it

Install and Start IoT Edge Runtime

Use PowerShell to download and install the IoT Edge runtime. Use the device connection string that you retrieved from IoT Hub to configure your device.

In the virtual machine, run PowerShell as an administrator. Use an AMD64 session of PowerShell to install IoT Edge. To validate, run the following command

(Get-Process -Id $PID).StartInfo.EnvironmentVariables["PROCESSOR_ARCHITECTURE"]

The Deploy-IoTEdge command checks the supported version, turns on the containers feature, and downloads the IoT Edge runtime.

. {Invoke-WebRequest -useb aka.ms/iotedge-win} | Invoke-Expression; 

Deploy the IoT Device by executing below command

Deploy-IoTEdge -ContainerOs Windows

The machine would restart automatically.

Run PowerShell as Administrator. The Initialize-IoTEdge command configures the IoT Edge runtime on your machine.

. {Invoke-WebRequest -useb aka.ms/iotedge-win} | Invoke-Expression;

Initialize the IoT Edge Runtime

Initialize-IoTEdge -ContainerOs Windows

When prompted for a Device Connection String, provide the string that you copied in the previous section.

View IoT Edge Runtime Status

Verify that the runtime was successfully installed and configured. Check the status of the IoT Edge service.

Get-Service iotedge

If you need to troubleshoot the service, retrieve the service logs.

. {Invoke-WebRequest -useb aka.ms/iotedge-win} | Invoke-Expression; Get-IoTEdgeLog

View all the modules running on your IoT Edge device. Since the service just started for the first time, you should only see the edgeAgent module running. The edgeAgent module runs by default and helps to install and start any additional modules that you deploy to your device.

iotedge list

Deploy a Module

To deploy your first module from the Azure Marketplace, use the following steps:

  • Sign in to the Azure portal and navigate to your IoT hub.
  • From the menu on the left pane, under Automatic Device Management, select IoT Edge.
  • Click on the device ID of the target device from the list of devices.
  • On the upper bar, select Set Modules.
  • In the IoT Edge Modules section of the page, click Add.
  • From the drop-down menu, select Marketplace Module.
  • In the IoT Edge Module Marketplace, search for “Simulated Temperature Sensor” and select that module.
  • Select Next: Review + create to continue.
  • After you create the module deployment details, the wizard returns you to the IoT Edge page of your IoT hub. Select your device from the list of IoT Edge devices to see its details.
  • On the device details page, scroll down to the Modules tab. Three modules should be listed: $edgeAgent, $edgeHub, and SimulatedTemperatureSensor

View Generated Data

Run Below command in Powershell to confirm that the module deployed from the cloud is running on your IoT Edge device.

iotedge list

View the messages being sent from the temperature sensor module to the cloud.

iotedge logs SimulatedTemperatureSensor -f

Your IoT Edge Device has been set and registered with IoT Hub. Try exploring the features of IoT Hub and Custom Modules.



Categories: Azure, Azure IoT

Tags: , , ,

2 replies

  1. I love your blog.. very nice colors & theme. Did you create
    this website yourself or did you hire someone to do it for you?
    Plz respond as I’m looking to design my own blog and would like to
    know where u got this from. thanks a lot

Leave a Reply

%d bloggers like this: