1

I'm a beginner to AzureIot, I've been given access to an azure account which has an IOT hub. from the azure portal I see a connection string of the form: HostName=iothub-mycompany.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=_somekey_in_base64_

Now all I need is to simply send a JSON payload to this IOT hub. Any protocol would do.

I was assuming that when I send a message I can see this message with the this device explorer tool. but I'm not sure if I'm doing something wrong or perhaps there is a flaw in my understanding.

I tried examples from blogs, msdn etc In the end I've come up with the code below

It runs, however I have no clue what it's doing and where to look for whether this message was sent.

  • The Device explorer shows nothing.

  • I checked the Activity Login portal.azure.com but it seems more like a login activity log.

  • Also in the azure portal Under explorers I See Iot Devices. I see the device name I need to target.(I know it exists)

Here is the code, part of .Net4.5.2 ConsoleApplication.

Program.cs:

class Program
{
    static DeviceClient deviceClient;
    const string IOT_HUB_CONN_STRING = "connection str in format specified above.";
    const string IOT_HUB_DEVICE_LOCATION = "_some_location.";
    const string IOT_HUB_DEVICE = "device_id";

    static void Main(string[] args)
    {
        var ret = SendMessageToIoTHubAsync("temperature", 15);


        Console.WriteLine("completed:" + ret.IsCompleted);


    }
    private static async Task SendMessageToIoTHubAsync(string sensorType, int sensorState)
    {
        deviceClient = DeviceClient.CreateFromConnectionString(IOT_HUB_CONN_STRING, TransportType.Mqtt);//i tried other trsnsports
        try
        {
            var payload = "{" +
                "\"deviceId\":\"" + IOT_HUB_DEVICE + "\", " +
                "\"location\":\"" + IOT_HUB_DEVICE_LOCATION + "\", " +
                "\"sensorType\":\"" + sensorType + "\", " +
                "\"sensorState\":" + sensorState + ", " +
                "\"localTimestamp\":\"" + DateTime.Now.ToLocalTime() + "\"" +
                "}";

            var msg = new Message(Encoding.UTF8.GetBytes(payload));

            Debug.WriteLine("\t{0}> Sending message: [{1}]", DateTime.Now.ToLocalTime(), payload);

            await deviceClient.SendEventAsync(msg);
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine("!!!! " + ex.Message);
        }
    }
}

As a side note, my "device" is ClearScada. I will have C# Driver which will get data out of a ClearScada instance, this driver should send that data to the azure iot hub. essentially I just need to figure the sending from C# to Azure iot. Questions:

  • Where Can I see my messages.?
  • Is my function correct or is there a flaw in my understanding.
gideon
  • 19,329
  • 11
  • 72
  • 113

1 Answers1

1
  • Where Can I see my messages.?

You can see your messages in the Device Explorer like this:

enter image description here

  • Is my function correct or is there a flaw in my understanding.

There are two issues to your function:

  • First, you need use device specified connection string instead of Azure IoT Hub connection string. It has the following format:

    "HostName=[YOUR HUB NAME].azure-devices.net;DeviceId=[DEVICE ID];SharedAccessKey=[DEVICE KEY]"

You can easily get this connection string via Device Explorer like this:

enter image description here

  • Second, your program exits before the sending operation completely. You can avoid this happening by adding Console.ReadLine(); like this:

enter image description here

Update:

Retrieving the value of the Task.IsCompleted property does not block the calling thread until the task has completed.

You can edit your main method like this:

    static void Main(string[] args)
    {
        var ret = SendMessageToIoTHubAsync("temperature", 15);

        ret.Wait();
        Console.WriteLine("completed:" + ret.IsCompleted);
        Console.ReadLine();
    }
Rita Han
  • 9,574
  • 1
  • 11
  • 24
  • Does `isCompleted` provide any guarantee the Task **finished**? Is that an acceptable way to block for result in Main (a non-async Main that is) vs `.GetAwaiter().GetResult()`? – evilSnobu Jan 15 '18 at 12:46