Azure IoT Simulated Device

When developing an IoT solution, the connected devices are not always available from the start. An ideal solution for this is to temporarily simulate a device.

Also, a simulated device is ideal for test work. Since we control the simulated device ourselves, we can explore boundary-value, for example.

In this article I will go further into what is needed and how to make a simulated device.

Preparation

First of all, we need to add the IoT extension to the Azure Cloud Shell. Open de Cloud Shell in the Azure portal. In the portal you can find the Azure Cloud Shell in the top menu bar:

Enter the text below to add the IoT extention.

az extension add --name azure-iot

IoT Hub

Next, we need an IoT Hub where we can publish the simulated device. We start by creating a resource group.

az group create --name <Recoure Group name> --location westeurope

And now we can create the IoT Hub

az iot hub create --name <IoT Hub name> --resource-group <Recoure Group name> --sku S1

Device

Before we can start simulating the device needs to be created.

az iot hub device-identity create --hub-name <IoT Hub name> --device-id <Device name>

The connection string is needed to connect to the device. We need this connection string later in the process.

az iot hub device-identity show-connection-string --hub-name <IoT Hub name> --device-id <Device name> --output table

Simulated Device

For the simulated device we use node.js. We created the script below to simulate a device. Store this script as MyDevice.js in a folder.

///////////////////////////////////////////////////////////////////
// Device Simulator
// This is a DEMO device
///////////////////////////////////////////////////////////////////
'use strict';
var IotHubString = '<connection string>';
var Mqtt = require('azure-iot-device-mqtt').Mqtt;
var DeviceClient = require('azure-iot-device').Client
var Message = require('azure-iot-device').Message;
var client = DeviceClient.fromConnectionString(IotHubString, Mqtt);
// Create a message and send it to the IoT hub every second
setInterval(function(){
// Simulate telemetry.
var message = new Message(JSON.stringify({
Name:'MyDevice',
Latitude: 40.741895,
Longitude: -73.989308,
Coordinates: [49.81158139, 8.57908623],
Value1: Math.random() * 25,
Value2: Math.random() * 45
}));
console.log('Sending message: ' + message.getData());
// Send the message.
client.sendEvent(message, function (err) {
if (err) {
console.error('send error: ' + err.toString());
} else {
console.log('message sent');
}
});
}, 1000);

Store this script as package.json in a folder.

{
"name": "MyDevice",
"version": "1.0.0",
"description": "My simulated device",
"main": "MyDevice.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"azure-iot-device": "^1.7.1",
"azure-iot-device-mqtt": "^1.7.1"
}
}

Start simulation

Now we can start the simulation. Open a Command Prompt and browse to the folder of the MyDevice.js script. Install node.js (only once):

npm install

Now start the simulation:

node MyDevice.js

The result will look like the screenshot below

Leave a comment

Your email address will not be published. Required fields are marked *

Exit mobile version