简体   繁体   English

使用Node.js将Rfid rc522中的数据发送到Azure Iot Hub

[英]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.

相关问题 从连接到Raspberry Pi的两个RFID RC522读取器读取序列号,时间戳等数据 - Read data like serial number,Time stamp from two RFID RC522 readers connected to Raspberry Pi Azure 物联网和事件中心? - Azure ioT and Event Hub? 如何从NodeJS中的单个azure函数向azure服务总线和事件发送消息到事件集线器总线? - How to send a message to azure service bus & event to event hub bus from single azure functions in NodeJS? Azure IOT 集线器:发送遥测数据“无效传输配置”的参考错误 - Azure IOT Hub: ReferenceError sending Telemetry Data 'Invalid transport configuration' 发送消息时 Azure IoT 中心库出错 - Error from Azure IoT Hub library when sending a message Azure IOT Hub 设备“暂停” - Azure IOT Hub Device “Suspension” 如何使用 Azure IOT HUB 订阅 mqtt 代理上的主题,因为我希望在发布主题时将数据存储在 Azure iot hub 中 - How to subscribes to topic on mqtt broker with Azure IOT HUB as i want the data to be stored in Azure iot hub when I published my topic 用于发送推送通知的 Azure 功能(通知中心 + NodeJs) - Azure function to send push notifications (Notification Hub + NodeJs) Microsoft Azure IoT中心和不带DeviceId的connectionString - Microsoft Azure IoT Hub and connectionString without DeviceId Azure function 触发器与物联网中心“EntityPath” - Azure function trigger with IoT-Hub "EntityPath"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM