Hi @GuidoL Thank you for connecting with us offline to further investigate this issue. I am sharing more details on this thread to add more context. The code sample provided on a similar Q&A question - How to generate in a single and simple C# project custom telemetry for a IoT Device Template with complex object? should work and help you send telemetry from a single file. However, in our observations offline, we have noticed that even the default code sample C# SDK failed to connect with your devices. Sharing the code below
using System;
using Microsoft.Azure.Devices.Client;
using System.Text;
using Newtonsoft.Json;
namespace MyApp // Note: actual namespace depends on the project name.
{
internal class SimpleApp
{
private const string ModelId = "dtmi:digitaltwins:org:archive:download:rfidsmartlibrary:RfidSmartLibraryowl:LibraryItemGateCheckSensor;1";
public static string DeviceId = "<DeviceId";
public static string DeviceSymmetricKey = "<DevicePrimaryKey>";
public static string hostname = "<IoTCentralHostname>";
static async Task Main(string[] args)
{
Console.WriteLine("Hello World!");
var authMethod = new DeviceAuthenticationWithRegistrySymmetricKey(DeviceId, DeviceSymmetricKey);
var options = new ClientOptions
{
ModelId = ModelId,
};
DeviceClient deviceClient = DeviceClient.Create(hostname, authMethod, TransportType.Mqtt, options);
var cts = new CancellationTokenSource();
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();
while (true)
{
string telemetryName = "ItemId";
int index = randNum.Next(ItemID.Count);
Encoding messageEncoding = Encoding.UTF8;
IDictionary<string, object> telemetryPairs = new Dictionary<string, object> { { telemetryName, ItemID[index] } };
telemetryName = "Description";
index = randNum.Next(Description.Count);
telemetryPairs.Add(telemetryName, Description[index]);
telemetryName = "Location";
index = randNum.Next(Location.Count);
telemetryPairs.Add(telemetryName, Location[index]);
telemetryName = "DateTimeGateEvent";
DateTime time = DateTime.Now;
telemetryPairs.Add(telemetryName, time);
string payload = JsonConvert.SerializeObject(telemetryPairs);
var message = new Message(messageEncoding.GetBytes(payload))
{
ContentEncoding = messageEncoding.WebName,
ContentType = "application/json",
};
await deviceClient.SendEventAsync(message, cts.Token);
Console.WriteLine("Message delivered");
Thread.Sleep(30000);
}
}
}
}
Please replace the ModelId, DeviceId, DeviceSymmetricKey, hostname in the code with the appropriate values. Note that the easiest way to obtain the hostname is by putting a break point on the line 165 in Program.cs file of TemperatureController project DeviceClient deviceClient = DeviceClient.Create(hostname, authenticationMethod, TransportType.Mqtt, options);
Update
The code sample from the above thread is accurate. Code works as expected after updating the nuget
packages to latest versions. Below are the versions the packages got updated to.
- Azure.Core v.1.37.0
- Microsoft.Azure.Devices.Client v.1.42.2
- Newtonsoft.Json v.13.0.3
If the response helped, please do click Accept Answer and Yes for the answer provided. Doing so would help other community members with similar issue identify the solution. I highly appreciate your contribution to the community.