How to do it...

We will be creating a simulated IoT device to send telemetry data and read that in the cloud application:

  1. Create a console application in the Visual Studio simulated device.
  2. Add the Azure.Devices package from NuGet.
  3. Create an event hub client connection using the following code:
eventHubClient = EventHubClient.CreateFromConnectionString(AzureIoTHub.GetConnectionString(), iotHubD2cEndpoint);
  1. 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;
  1. 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;
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.70.255