在透明网关上运行的IoT Edge无法处理从叶设备下游设备发送消息

时间:2018-09-18 20:58:23

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

我已经按照所有说明设置了“下游设备”,以便通过运行在透明网关中的IoT Edge发送消息。我相信我的路由规则是正确的,但是我的功能模块没有通过消息流接收任何消息。

这些是我遵循的说明: https://docs.microsoft.com/en-us/azure/iot-edge/how-to-create-transparent-gateway-linux

我正在使用2个Linxu VM(ubuntu 16.04.5)。

  1. IoT Edge透明网关VM已配置并正确设置,配置和验证了所有证书。我已经可以使用
  2. 中的openssl工具

openssl s_client -connect {my-gateway-machine-name-dns-name} .centralus.cloudapp.azure.com:8883 -CAfile /certs/certs/azure-iot-test-only.root.ca.cert .pem -showcerts

  1. 在Linux VM上运行的下游设备,已安装并验证了Certs。我的连接字符串如下:

    HostName = {IoTHubName} .azure-devices.net; DeviceId = TC51_EdgeDownStreamDevice01; SharedAccessKey = {My-Shared-Access-Key} = GatewayHostName = {my-gateway-machine-name-dns-name} .centralus.cloudapp .azure.com

a。我已经验证我使用openssl工具成功验证了SSL证书。 b。我在下游设备中使用NodeJS SDK进行连接时使用以下内容

var client = DeviceClient.fromConnectionString(connectionString,Mqtt); C。我可以看到消息显示在云中的Azure IoT中心,但是我的模块无法在IoT Edge透明网关上运行。

  1. 这是我为edgeHub配置的路由规则,如示例文档页面的“从下游设备路由消息”中所指定。

这是示例文档显示的内容: {“路线”:{“ sensorToAIInsightsInput1”:“ FROM /消息/ * WHERE NOT IS_DEFINED($ connectionModuleId)INTO BrokeredEndpoint(\” / modules / ai_insights / inputs / input1 \“)”,“ AIInsightsToIoTHub”:“ FROM /消息/ modules / ai_insights / outputs / output1 INTO $ upstream“}}

这是我的路由配置设置为:         “路线”:{           “ downstreamBatterySensorToBatteryDataFunctionInput1”:“从/ *不在IS_DEFINED($ connectionModuleId)INTO BrokeredEndpoint(\” / modules / BatteryDataFunctionModule / inputs / input1 \“)”,           “ BatteryDataFunctionModuleToIoTHub”:“从/消息/模块/ BatteryDataFunctionModule / outputs / * INTO $ upstream”         }

**请注意,“ FROM / * WHERE NOT IS_DEFINED”和“ FROM / messages / * WHERE NOT IS_DEFINED”已使用

  1. 我在IoT Edge上的模块已设置为功能。当我使用开箱即用的示例(其中模拟器设备是在IoT Edge上运行的另一个模块)时,我的功能被正确击中。仅当我尝试使用“下游设备”时,才触发模块。

我已启用在我的透明网关上运行的“用于IoT边缘服务的调试日志记录”。

This is the basic Run method for the Function module:

#r "Microsoft.Azure.Devices.Client"
#r "Newtonsoft.Json"

using System.IO;
using Microsoft.Azure.Devices.Client;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

// Filter messages based on the temperature value in the body of the     message and the temperature threshold value.
public static async Task Run(Message messageReceived, IAsyncCollector<Message> output, TraceWriter log)
{

我如何弄清楚如何使我的模块在IoT Edge中运行,以便从下游设备上进行击打/触发?

2 个答案:

答案 0 :(得分:1)

因此,您说您正在看到消息显示在IoT中心中,但没有出现在Edge中……两件事:

您将此作为您的节点应用程序中的连接字符串发布了: HostName = {IoTHubName} .azure-devices.net; DeviceId = TC51_EdgeDownStreamDevice01; SharedAccessKey = {My-Shared-Access-Key} = GatewayHostName = {my-gateway-machine-name-dns-name} .centralus.cloudapp.azure。 com

您确实复制/粘贴了吗?我问的原因是,在共享访问密钥和单词“ GatewayHostName”之间,您有一个等号而不是分号。

应为: HostName = {IoTHubName} .azure-devices.net; DeviceId = TC51_EdgeDownStreamDevice01; SharedAccessKey = {My-Shared-Access-Key}; GatewayHostName = {my-gateway-machine-name-dns-name} .centralus.cloudapp.azure。 com

(请注意GatewayHostName之前的“;”…如果您确实在其中有一个等号而不是分号,则不会告诉您将导致什么混乱:-)

第二,在您的路由中,您调用模块BatteryDataFunctionModule ..只想确保模块名称正确,包括区分大小写。您可能知道这一点,但不想假设。

最后,如果上述两项都可以检出,您是否可以添加一条附加的调试路径,也将“传入数据”发送到IoTHub。 “从/ *不在IS_DEFINED($ connectionModuleId)到$ upstream的地方”

所以我们可以确保消息实际上是通过 物联网边缘实现的。

答案 1 :(得分:0)

要使下游设备进行通信,需要解决两个问题

  1. 由于@ Steve-Busby-Msft,我需要在SharedAccessKey的末尾和GatewayHostName之前使用分号(;)

您将此作为连接字符串发布在节点应用程序中:HostName = {IoTHubName} .azure-devices.net; DeviceId = TC51_EdgeDownStreamDevice01; SharedAccessKey = {My-Shared-Access-Key} = GatewayHostName = {my-gateway-机器名称-DNS名称} .centralus.cloudapp.azure.com

  1. NodeJS应用程序下游设备还必须在“应用程序级别”正确加载证书。

注意

的代码部分
  

var edge_ca_cert_path ='[Edge CA证书的路径]';

Node JS Downstream Application

'use strict';

var fs = require('fs');
var Protocol = require('azure-iot-device-mqtt').Mqtt;
// Uncomment one of these transports and then change it in fromConnectionString to test other transports
// var Protocol = require('azure-iot-device-http').Http;
// var Protocol = require('azure-iot-device-amqp').Amqp;
var Client = require('azure-iot-device').Client;
var Message = require('azure-iot-device').Message;

// 1) Obtain the connection string for your downstream device and to it
//    append this string GatewayHostName=<edge device hostname>;
// 2) The edge device hostname is the hostname set in the config.yaml of the Edge device
//    to which this sample will connect to.
//
// The resulting string should look like the following
//  "HostName=<iothub_host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>;GatewayHostName=<edge device hostname>"
var connectionString = '[Downstream device IoT Edge connection string]';

// Path to the Edge "owner" root CA certificate
var edge_ca_cert_path = '[Path to Edge CA certificate]';

// fromConnectionString must specify a transport constructor, coming from any transport package.
var client = Client.fromConnectionString(connectionString, Protocol);

var connectCallback = function (err) {
  if (err) {
    console.error('Could not connect: ' + err.message);
  } else {
    console.log('Client connected');
    client.on('message', function (msg) {
      console.log('Id: ' + msg.messageId + ' Body: ' + msg.data);
      // When using MQTT the following line is a no-op.
      client.complete(msg, printResultFor('completed'));
      // The AMQP and HTTP transports also have the notion of completing, rejecting or abandoning the message.
      // When completing a message, the service that sent the C2D message is notified that the message has been processed.
      // When rejecting a message, the service that sent the C2D message is notified that the message won't be processed by the device. the method to use is client.reject(msg, callback).
      // When abandoning the message, IoT Hub will immediately try to resend it. The method to use is client.abandon(msg, callback).
      // MQTT is simpler: it accepts the message by default, and doesn't support rejecting or abandoning a message.
    });

    // Create a message and send it to the IoT Hub every second
    var sendInterval = setInterval(function () {
      var windSpeed = 10 + (Math.random() * 4); // range: [10, 14]
      var temperature = 20 + (Math.random() * 10); // range: [20, 30]
      var humidity = 60 + (Math.random() * 20); // range: [60, 80]
      var data = JSON.stringify({ deviceId: 'myFirstDownstreamDevice', windSpeed: windSpeed, temperature: temperature, humidity: humidity });
      var message = new Message(data);
      message.properties.add('temperatureAlert', (temperature > 28) ? 'true' : 'false');
      console.log('Sending message: ' + message.getData());
      client.sendEvent(message, printResultFor('send'));
    }, 2000);

    client.on('error', function (err) {
      console.error(err.message);
    });

    client.on('disconnect', function () {
      clearInterval(sendInterval);
      client.removeAllListeners();
      client.open(connectCallback);
    });
  }
};

// Provide the Azure IoT device client via setOptions with the X509
// Edge root CA certificate that was used to setup the Edge runtime
var options = {
  ca : fs.readFileSync(edge_ca_cert_path, 'utf-8'),
};

client.setOptions(options, function(err) {
  if (err) {
    console.log('SetOptions Error: ' + err);
  } else {
    client.open(connectCallback);
  }
});