【问题标题】:send data from Rfid rc522 to azure iot hub using nodejs使用 nodejs 将数据从 Rfid rc522 发送到 azure iot hub
【发布时间】:2017-09-03 00:54:21
【问题描述】:

//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 hub。 我已经搜索了有关此的资源,但在 nodejs 中找不到任何资源。

提前致谢

【问题讨论】:

  • 您可以使用device explorer 来监控设备到云端的事件。
  • 是的。谢谢。但是我无法将串行转换为 json.stringify 格式?我无法调用 client.send 事件函数。如何解决这个问题?你能帮我解决这个问题吗?很多提前致谢
  • 您的问题到底是什么?你能看到你在设备资源管理器中发送的消息吗?还是消息数据不正确?或者调用client.sendEvent()时出错?
  • 我可以通过 console.log(serial) 看到 rfid 标签的序列号..但是当我尝试使用 client.send 事件向 IOT 集线器发送消息时,我没有收到任何错误或数据跨度>

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


【解决方案1】:

当您将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
    });
});

【讨论】:

  • 非常感谢。现在我可以将序列号发送到 Azure。
  • 很高兴能提供帮助。如果是,请将问题标记为已回答,以便对其他人有所帮助。见stackoverflow.com/help/someone-answers
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-15
  • 1970-01-01
相关资源
最近更新 更多