We will be creating a simulated IoT device to send telemetry data and read that in the cloud application:
- Create a console application in the Visual Studio simulated device.
- Add the Azure.Devices package from NuGet.
- Create an event hub client connection using the following code:
eventHubClient = EventHubClient.CreateFromConnectionString(AzureIoTHub.GetConnectionString(), iotHubD2cEndpoint);
- Scan through all partitions of the event hub:
var d2cPartitions = eventHubClient.GetRuntimeInformation().PartitionIds;
String data = "";
foreach (string partition in d2cPartitions)
{
var result = ReceiveMessagesFromDeviceAsync(partition);
data = result.Result.ToString();
if (data != "")
return data;
}
return data;
- lets use following code for reading the messages sent by the device:
var eventHubReceiver = eventHubClient.GetDefaultConsumerGroup().CreateReceiver(partition, DateTime.Now);
while (true)
{
EventData eventData = await eventHubReceiver.ReceiveAsync();
if (eventData == null) continue;
var data = Encoding.UTF8.GetString(eventData.GetBytes());
return data;
}