【问题标题】:sending a message to an Azure Iot hub向 Azure IoT 中心发送消息
【发布时间】:2018-01-14 16:06:12
【问题描述】:

我是 AzureIot 的初学者,我已获得访问具有 IOT 中心的 azure 帐户的权限。 从 azure 门户中,我看到了以下形式的连接字符串: HostName=iothub-mycompany.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=_somekey_in_base64_

现在我只需要向这个 IOT 中心发送 JSON 有效负载。任何协议都可以。

假设当我发送消息时,我可以看到带有this device explorer tool 的消息。但我不确定是我做错了什么还是我的理解有缺陷。

我尝试了博客、msdn 等中的示例最后我想出了下面的代码

它运行,但我不知道它在做什么以及到哪里查找此消息是否已发送

  • 设备浏览器不显示任何内容。

  • 我查看了活动登录 portal.azure.com,但它看起来更像是登录活动日志。

  • 也在 azure 门户中在资源管理器下我看到了物联网设备。我看到了我需要定位的设备名称。(我知道它存在)

这是代码,是 .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);
        }
    }
}

附带说明,我的“设备”是ClearScada。我将有 C# 驱动程序,它将从 ClearScada 实例中获取数据,该驱动程序应该将该数据发送到 azure iot hub。 基本上我只需要计算从 C# 发送 到 Azure iot。问题:

  • 在哪里可以看到我的消息。?
  • 我的功能是正确的还是我的理解有缺陷。

【问题讨论】:

    标签: c# azure azure-iot-hub scada


    【解决方案1】:
    • 在哪里可以看到我的消息?

    您可以像这样在设备资源管理器中看到您的消息:

    • 我的功能是正确的还是我的理解有缺陷。

    你的函数有两个问题:

    • 首先,您需要使用设备指定的连接字符串,而不是 Azure IoT 中心连接字符串。格式如下:

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

    您可以像这样通过设备资源管理器轻松获取此连接字符串:

    • 其次,您的程序在发送操作完全之前退出。 您可以通过添加Console.ReadLine(); 来避免这种情况发生 这个:

    更新:

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

    您可以像这样编辑main 方法:

        static void Main(string[] args)
        {
            var ret = SendMessageToIoTHubAsync("temperature", 15);
    
            ret.Wait();
            Console.WriteLine("completed:" + ret.IsCompleted);
            Console.ReadLine();
        }
    

    【讨论】:

    • isCompleted 是否提供任务完成的任何保证?这是一种可以接受的方式来阻止 Main(即非异步 Main)与 .GetAwaiter().GetResult() 中的结果吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 2018-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多