我已经按照所有说明设置了“下游设备”,以便通过运行在透明网关中的IoT Edge发送消息。我相信我的路由规则是正确的,但是我的功能模块没有通过消息流接收任何消息。
这些是我遵循的说明: https://docs.microsoft.com/en-us/azure/iot-edge/how-to-create-transparent-gateway-linux
我正在使用2个Linxu VM(ubuntu 16.04.5)。
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
在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透明网关上运行。
这是示例文档显示的内容: {“路线”:{“ 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”已使用
我已启用在我的透明网关上运行的“用于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中运行,以便从下游设备上进行击打/触发?
答案 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)
要使下游设备进行通信,需要解决两个问题
您将此作为连接字符串发布在节点应用程序中:HostName = {IoTHubName} .azure-devices.net; DeviceId = TC51_EdgeDownStreamDevice01; SharedAccessKey = {My-Shared-Access-Key} = GatewayHostName = {my-gateway-机器名称-DNS名称} .centralus.cloudapp.azure.com
注意
的代码部分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);
}
});