IoT Hub: C# Device Simulator Deployment
iot powershell dotnetcore iothub azure iot-core twinning telemetry c2d
Following on from the previous post, this adds C# device simulator code for 10 devices with telemetry, twin support, and device update hooks. Tip: avoid hardcoded hub names by setting an environment variable once in PowerShell:
$IOTHUB_NAME = 'my-iot-hub-137'
After that, scripts can be run without -HubName, for example:
.\3_Simulator\run_10_devices.ps1
.\3_Simulator\toggle_telemetry_all.ps1 -Action stop
.\3_Simulator\toggle_telemetry_all.ps1 -Action start
.\3_Simulator\stop_10_devices.ps1
Repository
Azure IoT Hub Fast Setup on GitHub: djaus2/iothub-fast-setup
Scripts
The scripts from this post are on GitHub at djaus2/iothub-fast-setup/3_Simulator
List of Scripts
This is a C# console app (switched from Python) that demonstrates device simulation for Azure IoT Hub, including:
- telemetry
- direct methods
- cloud-to-device messages
- twinning for runtime settings
- device update hooks
Requirements:
- Azure subscription with IoT Hub (S1) and 10 devices created (see VSCode-Azure-IoTHub-Setup-Guide.md)
- Note: IoT Hub must be S1, not B1
- Upgrade to S1 if you have B1, or create a new S1 hub and migrate devices
The hub was:
- Name: my-iot-hub-137
- SKU: B1
- Tier: Basic
Basic tier does not support device twins (and also blocks features your app needs like direct methods/C2D/Device Update workflows).
You need Standard tier, typically S1, to use twinning.
Used this upgrade command:
az iot hub update --name my-iot-hub-137 --sku S1
Then verify:
az iot hub show --name my-iot-hub-137 --query "{sku:sku.name,tier:sku.tier,capacity:sku.capacity}" -o json
Project location:
- csharp_simulator
Capabilities included:
- Telemetry send loop with random temperature every N sends
- Telemetry can be paused/resumed per device
- Direct methods:
setText(stores a string value)getText(returns the stored string value)setNumber(stores an integer value)getNumber(returns the stored integer value)startTelemetry(resumes telemetry)stopTelemetry(pauses telemetry)- unsupported method names return a
404response with an allowed-method list
- Cloud-to-device (C2D) message receive and log, including enqueue time
- Device Twin desired properties for runtime settings
- Device Update intent hook (
du.targetVersion) with safe stop signal for external update workflow
Important:
- This app does not install Device Update packages itself.
- Actual package rollout is still handled by Device Update agent workflow on target devices.
Repository
Azure IoT Hub Fast Setup on GitHub: djaus2/iothub-fast-setup
1. Files/Scripts
- 1_QuickSetup/set_iothub_env.ps1
- csharp_simulator/devices.sample.json — sample device list. You can generate the runtime
devices.jsonusing the generator script: - 3_Simulator/generate_devices_json.ps1.
- csharp_simulator/Program.cs
- csharp_simulator/csharp_simulator.csproj
- 3_Simulator/get_device_connections.ps1
- 3_Simulator/run_10_devices.ps1
- 3_Simulator/stop_10_devices.ps1
- 3_Simulator/toggle_telemetry_all.ps1
2. Set IoT Hub Name Once
Set a reusable environment variable so scripts do not require -HubName every time.
Current session only:
.\1_QuickSetup\set_iothub_env.ps1 -HubName $IOTHUB_NAME -Scope Process
Persist for your user profile:
.\1_QuickSetup\set_iothub_env.ps1 -HubName $IOTHUB_NAME -Scope User
3. Run 10 Devices
From workspace root:
.\3_Simulator\run_10_devices.ps1
What the script does:
- Builds
devices.json(device1..device10) if missing. - Runs the C# simulator against all entries in
devices.json.
4. Twin Runtime Settings
Example desired properties update for one device:
az iot hub device-twin update -n $IOTHUB_NAME -d device1 --set properties.desired.sim.intervalSeconds=3 properties.desired.sim.randomEvery=10 properties.desired.sim.tempMin=15 properties.desired.sim.tempMax=40 properties.desired.sim.baseTemp=23
Apply to device1..device10:
for ($i=1; $i -le 10; $i++) {
az iot hub device-twin update -n $IOTHUB_NAME -d ("device" + $i) --set properties.desired.sim.intervalSeconds=3 properties.desired.sim.randomEvery=10 properties.desired.sim.tempMin=15 properties.desired.sim.tempMax=40 properties.desired.sim.baseTemp=23
}
5. Direct Method Calls
PowerShell-safe payload form:
$payload = '{"value":"Hello"}'
Set string value:
$payload = '{"value":"hello from cloud"}'
az iot hub invoke-device-method -n $IOTHUB_NAME -d device1 --method-name setText --method-payload $payload
Read string value:
az iot hub invoke-device-method -n $IOTHUB_NAME -d device1 --method-name getText --method-payload '{}'
Set number value:
$payload = '{"value":42}'
az iot hub invoke-device-method -n $IOTHUB_NAME -d device1 --method-name setNumber --method-payload $payload
Read number value:
az iot hub invoke-device-method -n $IOTHUB_NAME -d device1 --method-name getNumber --method-payload '{}'
Stored values are included in telemetry payload and reported twin state.
Start telemetry for one device:
az iot hub invoke-device-method -n $IOTHUB_NAME -d device1 --method-name startTelemetry --method-payload '{}'
Stop telemetry for one device:
az iot hub invoke-device-method -n $IOTHUB_NAME -d device1 --method-name stopTelemetry --method-payload '{}'
Azure IoT Explorer payloads:
setTextpayload:{"value":"hello from Explorer"}getTextpayload:{}setNumberpayload:{"value":42}getNumberpayload:{}
If you use the Explorer UI, paste the JSON directly into the method payload box.
For getText and getNumber, use an empty JSON object because the device reads stored state from the simulator.
6. C2D Message
Send cloud-to-device message:
az iot device c2d-message send -n $IOTHUB_NAME -d device1 --data "server-ping $(Get-Date -Format o)"
The simulator logs:
- message body
enqueuedTimeUtc
7. Device Update Hook
Signal target version through twin desired property:
az iot hub device-twin update -n $IOTHUB_NAME -d device1 --set properties.desired.du.targetVersion=1.1.0
Behavior:
- If
du.targetVersiondiffers from app version, simulator marksdu.restartRequested=trueand exits cleanly. - This allows external Device Update rollout/restart process to take over.
8. Stop the App
Press Ctrl+C in the simulator terminal.
Or stop from another terminal with the helper script:
.\3_Simulator\stop_10_devices.ps1
Preview which processes would be stopped:
.\3_Simulator\stop_10_devices.ps1 -WhatIf
9. Bulk Telemetry Toggle
Pause telemetry on all devices:
.\3_Simulator\toggle_telemetry_all.ps1 -Action stop
Resume telemetry on all devices:
.\3_Simulator\toggle_telemetry_all.ps1 -Action start
| Topic | Subtopic | |
| Next: > | IoT Hub | ADU Workflow (Simulator) |
| < Prev: | IoT Hub | Device Twin vs Device Update for IoT Hub |
| This Category Links | ||
| Category: | IoT Index: | IoT |