//THIS IS THE CODE TO READ SERIAL NUMBER AND SEND DATA TO AZURE IOT HUB
var rc522=require("rc522/build/Release/rc522");
var Async = require('async');
var Protocol = require('azure-iot-device-http').Http;
var Client = require('azure-iot-device').Client;
var ConnectionString = require('azure-iot-device').ConnectionString;
var Message = require('azure-iot-device').Message;
// Enter Device Connection String: AZURE--> IOTHub --> Devices--> select Device--> Connection String.
var connectionString = '';
var deviceId = ConnectionString.parse(connectionString).DeviceId;
var client = Client.fromConnectionString(connectionString, Protocol);
var connectCallback=function(err){
if(err){
console.error('could not open connection' +err);
}
else{
console.log('client connected');
rc522(function(serial){
console.log(serial);
});
var readings = { Id:serial};
var message = new Message(JSON.stringify(readings));
client.sendEvent(message, function (error) {
if (error)
{
console.log(error.toString());
}
else
{
console.log("Data sent on %s...", new Date());
}
});
}
}
client.open(connectCallback);
我无法使用 Nodejs 将 rfid rc522 序列号发送到 Azure IOT 中心。我能够连接到客户端并在控制台上显示序列号,但在 azure iot 中心看不到任何收到的消息。我编写了函数应用程序并将 iothub 消息发送到表存储。
下面是我的代码和输出
iothub and table storage output ,
console output for rfid nodejs ,
有人可以解释一下如何将序列号发送到 azure IOT 集线器吗? 我搜索了有关此的资源,但在 Nodejs 中找不到任何资源。
提前非常感谢
最佳答案
将 JSON.stringify(readings)
记录到控制台时会发生什么?我不是 node.js 专家,但我认为 serial
在那里未定义,因为它在 rc522(function(serial){ ..}
范围之外使用。定义
尝试
rc522(function(serial){
console.log(serial);
var readings = { Id:serial};
var message = new Message(JSON.stringify(readings));
client.sendEvent(message, function (error) {
// code here
});
});
关于node.js - 使用nodejs将数据从Rfid rc522发送到azure iot hub,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45936038/