[英]send data from Rfid rc522 to azure iot hub using nodejs
//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);
I cannot send rfid rc522 serial number to Azure IOT hub with Nodejs.I am able to connect to client and display serial number on console but cannot see any received messages in azure iot hub.I wrote function app and sent iothub message to tablestorage. 我无法使用Node.js将rfid rc522序列号发送到Azure IOT集线器。我能够连接到客户端并在控制台上显示序列号,但是在天蓝色的iot集线器中看不到任何收到的消息。我编写了功能应用并将iothub消息发送到了tabletorage。
Below is my code and output 下面是我的代码和输出
iothub and table storage output , iothub和表存储输出 ,
console output for rfid nodejs , rfid nodejs的控制台输出 ,
Can somepone please explain me how to send serial number to azure IOT hub. 可以请我解释一下如何将序列号发送到天蓝色的IOT集线器。 I have searched for resources about this but could not find any in nodejs.
我已经搜索了有关此资源,但在nodejs中找不到任何资源。
Many Thanks in advance 提前谢谢了
What happens when you log JSON.stringify(readings)
to the console? 当您将
JSON.stringify(readings)
登录到控制台时会发生什么? I am not a node.js expert but I think serial
is undefined there since it is used outside the scope of rc522(function(serial){ ..}
where it is defined 我不是node.js专家,但我认为在那里没有定义
serial
,因为它在rc522(function(serial){ ..}
定义的范围之外使用
try 尝试
rc522(function(serial){
console.log(serial);
var readings = { Id:serial};
var message = new Message(JSON.stringify(readings));
client.sendEvent(message, function (error) {
// code here
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.