Hi @GuidoL Greetings! You can certainly simulate the Data using a C# SDK. Perhaps one of the easiest ways to simulate this scenario is by creating a List of strings that you anticipate the Telemetry data would hold and iterate the lists to pick a random value before passing the telemetry.
I have used the template you provided above and created a Device on my IoT Central app. I have used the Github repository and modified the TemperatureControllerSample.cs
file under the directory \azure-iot-sdk-csharp-main\iothub\device\samples\solutions\PnpDeviceSamples\TemperatureController
to the following to simulate the data.
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Devices.Shared;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using PnpHelpers;
namespace Microsoft.Azure.Devices.Client.Samples
{
public class TemperatureControllerSample
{
// The default reported "value" and "av" for each "Thermostat" component on the client initial startup.
// See https://docs.microsoft.com/azure/iot-develop/concepts-convention#writable-properties for more details in acknowledgment responses.
private readonly DeviceClient _deviceClient;
private readonly ILogger _logger;
//private string ItemID = "Item";
List<string> ItemID = new List<string>() { "Item1", "Item2", "Item3", "Item4" };
List<string> Description = new List<string>() { "Des 1", "Des 2", "Des 3", "Des 4", "Des 5", "Des 6", "Des 7" };
List<string> Location = new List<string>() { "Downingtown", "Midtown", "Redmond", "Hyderabad" };
Random randNum = new Random();
public TemperatureControllerSample(DeviceClient deviceClient, ILogger logger)
{
_deviceClient = deviceClient ?? throw new ArgumentNullException(nameof(deviceClient));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public async Task PerformOperationsAsync(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
await SendTemperatureAsync(cancellationToken);
Thread.Sleep(30000);
await Task.Delay(5 * 1000, cancellationToken);
}
}
private async Task SendTemperatureAsync(CancellationToken cancellationToken)
{
string telemetryName = "ItemId";
int index = randNum.Next(ItemID.Count);
using Message msg = PnpConvention.CreateMessage(telemetryName, ItemID[index]);
await _deviceClient.SendEventAsync(msg, cancellationToken);
_logger.LogDebug($"Telemetry: Sent - {{ \"{telemetryName}\": {ItemID[index]} }}.");
telemetryName = "Description";
index = randNum.Next(Description.Count);
using Message msg1 = PnpConvention.CreateMessage(telemetryName, Description[index]);
await _deviceClient.SendEventAsync(msg1, cancellationToken);
_logger.LogDebug($"Telemetry: Sent - {{ \"{telemetryName}\": {Description[index]} }}.");
telemetryName = "Location";
index = randNum.Next(Location.Count);
using Message msg3 = PnpConvention.CreateMessage(telemetryName, Location[index]);
await _deviceClient.SendEventAsync(msg3, cancellationToken);
_logger.LogDebug($"Telemetry: Sent - {{ \"{telemetryName}\": {Location[index]} }}.");
telemetryName = "DateTimeGateEvent";
DateTime time = DateTime.Now;
using Message msg4 = PnpConvention.CreateMessage(telemetryName, time);
await _deviceClient.SendEventAsync(msg4, cancellationToken);
_logger.LogDebug($"Telemetry: Sent - {{ \"{telemetryName}\": {time} }}.");
}
}
}
I have also made a change in Line 23 of Program.cs
file under the same directory to make sure the Model ID is set to reflect the Model ID of the above template. Please find the change below
private const string ModelId = "dtmi:digitaltwins:org:archive:download:rfidsmartlibrary:RfidSmartLibraryowl:LibraryItemGateCheckSensor;1";
You can then follow the steps provided in Configure your environment to set the required values for the device and run the code to simulate the data. Please find the below image displaying the telemetry data received on the IoT Central device.
You can then export the data from IoT Central to one of the supported Data end points and Create an Azure Function to ingest the data into the Azure Digital Twin.
Hope this helps. Please let us know if the above approach is useful and do let us know if you have any further questions.
If the response helped, please do click Accept Answer and Yes. Doing so would help other community members with similar issue identify the solution. I highly appreciate your contribution to the community.