使用nodejs将数据从Rfid rc522发送到azure iot hub

时间:2017-08-29 09:56:15

标签: node.js azure azure-table-storage rfid azure-iot-hub

//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 hub中看到任何收到的消息。我编写了功能应用并发送了iothub消息给tablestorage。

以下是我的代码和输出

iothub and table storage output

console output for rfid nodejs

有人可以解释一下如何将序列号发送到azure IOT集线器。 我搜索过有关此内容的资源但在nodejs中找不到任何资源。

非常感谢提前

1 个答案:

答案 0 :(得分:0)

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
    });
});