0

I'm using amqp to send messages from my iot devices to Azure IoTHub. I'm writing a program in C# and I want to check if the devices are able to establish a connection with iot hub. What would be the best way of achieving this? Is it possible to create a callback function?

I noticed that one of my devices are on a closed network, that is the amqp port is blocked. But the device logs that It's sending messages to IoT Hub (no exceptions thrown) and when I check Iot Hub no messages have been received. This is why I want to check if the device is able to establish a connection with IoT Hub

The java sdk has a eventcallback class but not C#, or am I missing something?

protected static class EventCallback implements IotHubEventCallback { public void execute(IotHubStatusCode status, Object context) { System.out.println("IoT Hub responded to message with status " + status.name()); } }

Edit: Would this be a good implementation?

try { client = DeviceClient.CreateFromConnectionString("***", TransportType.Amqp); var task = client.OpenAsync(); task.Wait(30000); // wait for 30 sec if (task.IsCompleted) { Console.WriteLine("Connected"); await client.CloseAsync(); } else { throw new Exception("Time out"); } } catch (Exception e) { Console.WriteLine("Error"); }

Reginbald
  • 325
  • 4
  • 11
  • see https://stackoverflow.com/questions/39100278/how-do-i-know-if-my-device-in-azure-iot-hub-is-reachable – GorvGoyl Aug 14 '17 at 07:43

1 Answers1

3

Assuming you're using the Azure IoT SDK, the connection isn't actually opened when initializing the DeviceClient. It gets opened when you call the SendEventAsync or ReceiveAsync methods. You can also call OpenAsync() to attempt an open. If communication can't be established or something goes wrong, an exception will be thrown.

Take a look at the Azure IoT SDK samples on GitHub. Here's a link to the AmqpSample: https://github.com/Azure/azure-iot-sdk-csharp/tree/master/device/samples/DeviceClientAmqpSample

Since the SDK is open source you can walk the Microsoft.Azure.Devices.Client code and see what specific exception will be thrown for different connection failures (IotHubCommunicationException, UnauthorizedException, etc).

For testing you can use the Device Explorer tool to manage devices and monitor/send messages for your Hub: https://github.com/Azure/azure-iot-sdk-csharp/tree/master/tools/DeviceExplorer

Josh Kelley
  • 56,064
  • 19
  • 146
  • 246
Ian Hoppes
  • 252
  • 1
  • 4
  • Thanks for the help @ian, I noticed that one of my devices are on a closed network, that is the amqp port is blocked. But the device logs that It's sending messages to IoT Hub (no exceptions thrown) and when I check Iot Hub no messages have been received. This is why I want to check if the device is able to establish a connection with IoT Hub. – Reginbald Jun 23 '16 at 09:43
  • 2
    I noticed that the java sdk has an eventcallback class but not C# `protected static class EventCallback implements IotHubEventCallback { public void execute(IotHubStatusCode status, Object context) { System.out.println("IoT Hub responded to message with status " + status.name()); } } ` – Reginbald Jun 23 '16 at 09:45