node.js - 使用nodejs将数据从Rfid rc522发送到azure iot hub

标签 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 中心看不到任何收到的消息。我编写了函数应用程序并将 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/

相关文章:

azure - 对特定受众隐藏 Azure 表中的数据

javascript - 预期未定义是 Node 模块的对象

azure - ValidationError(Deployment.spec.template.spec.containers[0].env[13])

azure - 查询以显示 15 天前及更长时间内丢失的关键更新和安全更新

c# - 将所有行复制到 Azure 表存储中的另一个表

azure - azure-table 中的 N 到 N 关系建模

javascript - 如何从 Mongoose 中的多个数组更新数组内的值?

node.js - 将冗长的数据下载为 CSV 文件

node.js - 使用 NodeJS Promise 查询 MongoDB

asp.net-mvc-3 - ASP.NET 成员(member)资格 - 登录在本地有效,但在 Azure 上失败