Set up a simulated sensor for testing (preview)
[This article is prerelease documentation and is subject to change.]
If you want to test Sensor Data Intelligence without installing any physical sensors, you can use the Raspberry PI Azure IoT Online Simulator service to emulate sensor signals and send them to your Internet of Things (IoT) solution on Microsoft Azure. For more information about the simulator, see Connect Raspberry Pi online simulator to Azure IoT Hub (Node.js).
Warning
This procedure describes how to set up a simulated sensor for testing purposes. It uses resource owner password credential (ROPC) authentication, which you should only implement in a test environment. Microsoft recommends against using this type of authentication in any production system. You should always use the most secure authentication flow available. The authentication flow described in this procedure requires a very high degree of trust in the application, and carries security risks that are not present in other flows. You should only use this flow when other more secure flows, such as managed identities, aren't viable.
Video instructions
The following video shows how to set up a simulated sensor for testing. The remaining sections in this article provide the same instructions in a text-based format.
Create a device in Azure IoT Hub
You must first set up a device to authenticate the sensor signals to the Azure IoT Hub.
In Azure, go to the list of resources for the resource group that you created for use with Sensor Data Intelligence. (Learn more in Deploy an IoT solution on Azure.)
In the resource list, find the record where the Type field is set to IoT Hub. In the Name column, select the name to open the details page for the resource.
In the left navigation pane, select Devices.
On the Devices page, select Add device.
On the Create a device page, set the following fields:
- Device ID – Enter a name for the new device (for example, My-IoT-Device).
- Authentication type – Select Symmetric keys.
- Auto-generate keys – Select this checkbox.
- Connect this device to an IoT hub – Select Enable.
Select Save to return to the Devices page.
Find the new device in the list. In the Device ID column, select the name to open the details page for the device. If you don't see the new device in the list, refresh the page.
Copy the Primary connection string value (for example, by selecting the Copy to clipboard button). You will need this value later when you set up the Raspberry Pi IoT simulator to emulate sensor signals. Therefore, consider pasting it into a text file for now.
Add the Azure connection string to the Raspberry Pi IoT simulator
Follow these steps to add the connection string from the device in Azure IoT Hub to the script in the Raspberry service.
Open the Raspberry Pi IoT simulator.
In the code editor pane, find the line that contains the following command.
const connectionString = '[Your IoT hub device connection string]';
Replace the help text, including the brackets, with the Primary connection string value that you copied in the previous section. The result should resemble the following example.
const connectionString = 'HostName=XXX;DeviceId=YYY;SharedAccessKey=ZZZ';
Add sensor IDs and values to the payload in the Raspberry Pi IoT simulator
You must now set up the Raspberry Pi IoT simulator with simulated sensors and the values that they will send as payloads.
In the code editor of the Raspberry Pi IoT simulator, find the
getMessage
function, and edit it so that it matches the following code. (The sensors are set up in thecb()
lines.)function getMessage(cb) { messageId++; sensor.readSensorData() .then(function (data) { cb(JSON.stringify({ value: 1, sensorId: 'MachineStatus' }), false); cb(JSON.stringify({ value: 70, sensorId: 'Quality' }), false); cb(JSON.stringify({ value: 1, sensorId: 'AssetMaintenance' }), false); cb(JSON.stringify({ value: 1, sensorId: 'ProductionDelay' }), false); cb(JSON.stringify({ value: 20, sensorId: 'AssetDowntime' }), false); }) .catch(function (err) { console.error('Failed to read out sensor data: ' + err); }); }
Important
The sensors IDs that are defined in the code editor for the Raspberry Pi IoT simulator must be identical to the sensor IDs that you will specify later for the scenarios in Supply Chain Management. The preceding example code uses human-readable sensor IDs. However, in an actual scenario, the sensor IDs will be globally unique identifier (GUID) values that are provided by the sensor manufacturer. The human-readable sensor IDs that are used in this example code are also used in the examples for the product quality scenario, asset maintenance scenario, production delays scenario, asset downtime scenario, and machine status scenario). Therefore, use this code if you will work through those scenarios.
Edit the interval for sending sensor signals
You must now set the interval at which the Raspberry Pi IoT simulator should send the emulated sensor signals.
In the code editor of the Raspberry Pi IoT simulator, find the following function invocation.
setInterval(sendMessage, 2000);
By default, the Raspberry Pi IoT simulator sends a sensor signal every 2,000 milliseconds (two seconds). You can adjust the value as you require.
Run the Raspberry Pi IoT simulator
- Select Run to start the simulator and begin to send simulated sensor data.