1
votes

//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.

Below is my code and output

iothub and table storage output,

console output for rfid nodejs,

Can somepone please explain me how to send serial number to azure IOT hub. I have searched for resources about this but could not find any in nodejs.

Many Thanks in advance

1
You can utilize device explorer to monitor device-to-cloud events. - Rita Han
Yes.Thankyou.But I am not able to convert serial to json.stringify format?I cannot call client.send event function.How to solve this problem?Can you please help me with this?Many Thanks in advance - Schatz
What's exactly your problem? Can you see the messages that you sent in device explorer? Or the message data is incorrect? Or getting error when calling client.sendEvent()? - Rita Han
I can see the serial number of rfid tag with console.log(serial)..but when I try to send message to IOT hub with client.send event neither I get any error nor data - Schatz

1 Answers

0
votes

What happens when you log JSON.stringify(readings) to the console? 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

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