如何将来自Iot Device的消息路由到IoT Edge设备?

问题描述 投票:1回答:1

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

我想要做的是:从物联网设备接收遥测数据到物联网边缘设备。

架构: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端点消息/ input1没有消息。

c# azure azure-iot-hub azure-iot-edge
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.