【问题标题】:How to route message coming from IotDevice to IoTEdge device?如何将来自 IoT 设备的消息路由到 IoT Edge 设备?
【发布时间】:2018-09-11 10:31:14
【问题描述】:

我已关注此链接To write custom modules。在本教程中,一个名为 tempSensor 的模块将数据发送到另一个模块 CSharpModule。就教程而言,我成功实现了。

我想做的是:IoTDevice接收遥测数据到IoTEdge Device

架构: Azure IoTHub 与 IoT 设备和 IoTEdge 设备相连

我尝试过的:我尝试从使用 connectionString 连接到 ioTEdge 的模拟设备发送遥测数据。

发送数据的代码在这里

        //DeviceClient connected to IoTEdge     
        s_deviceClient = DeviceClient.CreateFromConnectionString(s_connectionString);

        private static async void SendDeviceToCloudMessagesAsync()
        {
            // Initial telemetry values
            double minTemperature = 20;
            double minHumidity = 60;
            Random rand = new Random();

            while (true)
            {
                double currentTemperature = minTemperature + rand.NextDouble() * 15;
                double currentHumidity = minHumidity + rand.NextDouble() * 20;

                // Create JSON message
                var telemetryDataPoint = new
                {
                    temperature = currentTemperature,
                    humidity = currentHumidity
                };
                var messageString = JsonConvert.SerializeObject(telemetryDataPoint);
                var message = new Message(Encoding.ASCII.GetBytes(messageString));

                // Add a custom application property to the message.                    
                message.Properties.Add("temperatureAlert", (currentTemperature > 30) ? "true" : "false");

                // Send the tlemetry message to endpoint output1
                await s_deviceClient.SendEventAsync("ouput1",message);

                //await s_deviceClient.SendEventAsync(message);
                Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, messageString);

                await Task.Delay(s_telemetryInterval * 10000);
            }
        }

IoTEdge 自定义模块代码的接收端在这里...

        static async Task Init()
        {
            AmqpTransportSettings amqpSetting = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only);
            ITransportSettings[] settings = { amqpSetting };

            // Open a connection to the Edge runtime
            ModuleClient ioTHubModuleClient = await ModuleClient.CreateFromEnvironmentAsync(settings);
            await ioTHubModuleClient.OpenAsync();
            Console.WriteLine("IoT Hub module client initialized.");

        // Register a callback for messages that are received by the module.
            // await ioTHubModuleClient.SetImputMessageHandlerAsync("input1", PipeMessage, iotHubModuleClient);

            // Read the TemperatureThreshold value from the module twin's desired properties
            var moduleTwin = await ioTHubModuleClient.GetTwinAsync();
            var moduleTwinCollection = moduleTwin.Properties.Desired;
            try {
                temperatureThreshold = moduleTwinCollection["iothub-connection-device-id"];
            } catch(ArgumentOutOfRangeException e) {
                Console.WriteLine($"Property TemperatureThreshold not exist: {e.Message}"); 
            }

            // Attach a callback for updates to the module twin's desired properties.
            await ioTHubModuleClient.SetDesiredPropertyUpdateCallbackAsync(OnDesiredPropertiesUpdate, null);

            // Register a callback for messages that are received by the module
            await ioTHubModuleClient.SetInputMessageHandlerAsync("input1", PipeMessage, ioTHubModuleClient);
        }

自定义模块的deployment.template.json文件中的路由信息​​如下。

 "routes": {
      "aggregationModuleToIoTHub": "FROM /messages/modules/aggregationModule/outputs/* INTO $upstream"           
    }

但问题是回调 PipeMessage 从未调用过。我的理解是 IoTEdge 端点 messages/input1 没有消息。

【问题讨论】:

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


    【解决方案1】:

    为确保我理解您的问题,您正尝试将代码从 IoT Edge外部的 IoT 设备发送到 IoT Edge,然后通过 IoT Edge 中的模块将该数据路由到 IoT中心?那是对的吗?如果是这种情况,我们将 IoT 设备称为下游或“叶”设备。 IoT Edge 需要设置为“透明网关”,如 this 文档。完成此操作后,您需要将 ;GatewayHostName= 添加到连接字符串的末尾,其中是您在 config.yaml 文件中用作“主机名”参数的名称。

    【讨论】:

    • 是的,你理解正确。那我试试看,会告诉你的。只想确认一件事:数据发送设备是否应该连接到 IotEdge?与连接字符串?还是使用 IOT Hub?
    • 数据发送设备使用普通的基于 IoT Hub 的连接字符串...到它的末尾,您只需附加 ";GatewayHostName=" 其中 是您的名称来自 config.yaml 中主机名参数的边缘设备……您需要确保您的“叶”设备(我们称之为)可以解析该名称(因此,如果您没有 DNS,则可能需要添加主机文件条目) .另外,正如我上面提到的文档,您需要确保叶子设备信任边缘设备的根 ca 证书,否则叶子和边缘之间的连接将失败
    猜你喜欢
    • 1970-01-01
    • 2018-11-05
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-22
    • 2019-09-21
    • 1970-01-01
    相关资源
    最近更新 更多