How to Send telemetry message to Azure IOT Hub using Node Js ?
- 
					
					
					
					
 I am able to install node using Opkg Install Node, when I create sample program , simple hello world, it is working fine. 
 'use strict';
 console.log("Hello world"); however when I run following code in Js, 
 // Copyright (c) Microsoft. All rights reserved.
 // Licensed under the MIT license. See LICENSE file in the project root for full license information.'use strict'; // Choose a protocol by uncommenting one of these transports. 
 const Protocol = require('azure-iot-device-mqtt').Mqtt;
 // const Protocol = require('azure-iot-device-amqp').Amqp;
 // const Protocol = require('azure-iot-device-http').Http;
 // const Protocol = require('azure-iot-device-mqtt').MqttWs;
 // const Protocol = require('azure-iot-device-amqp').AmqpWs;const Client = require('azure-iot-device').Client; 
 const Message = require('azure-iot-device').Message;// String containing Hostname, Device Id & Device Key in the following formats: 
 // "HostName=<iothub_host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>"
 const deviceConnectionString = process.env.IOTHUB_DEVICE_CONNECTION_STRING;
 let sendInterval;function disconnectHandler () { 
 clearInterval(sendInterval);
 sendInterval = null;
 client.open().catch((err) => {
 console.error(err.message);
 });
 }// The AMQP and HTTP transports have the notion of completing, rejecting or abandoning the message. 
 // For example, this is only functional in AMQP and HTTP:
 // client.complete(msg, printResultFor('completed'));
 // If using MQTT calls to complete, reject, or abandon are no-ops.
 // When completing a message, the service that sent the C2D message is notified that the message has been processed.
 // When rejecting a message, the service that sent the C2D message is notified that the message won't be processed by the device. the method to use is client.reject(msg, callback).
 // When abandoning the message, IoT Hub will immediately try to resend it. The method to use is client.abandon(msg, callback).
 // MQTT is simpler: it accepts the message by default, and doesn't support rejecting or abandoning a message.
 function messageHandler (msg) {
 console.log('Id: ' + msg.messageId + ' Body: ' + msg.data);
 client.complete(msg, printResultFor('completed'));
 }function generateMessage () { 
 const windSpeed = 10 + (Math.random() * 4); // range: [10, 14]
 const temperature = 20 + (Math.random() * 10); // range: [20, 30]
 const humidity = 60 + (Math.random() * 20); // range: [60, 80]
 const data = JSON.stringify({ deviceId: 'myFirstDevice', windSpeed: windSpeed, temperature: temperature, humidity: humidity });
 const message = new Message(data);
 message.properties.add('temperatureAlert', (temperature > 28) ? 'true' : 'false');
 return message;
 }function errorHandler (err) { 
 console.error(err.message);
 }function connectHandler () { 
 console.log('Client connected');
 // Create a message and send it to the IoT Hub every two seconds
 if (!sendInterval) {
 sendInterval = setInterval(() => {
 const message = generateMessage();
 console.log('Sending message: ' + message.getData());
 client.sendEvent(message, printResultFor('send'));
 }, 2000);
 }
 }// fromConnectionString must specify a transport constructor, coming from any transport package. 
 let client = Client.fromConnectionString(deviceConnectionString, Protocol);client.on('connect', connectHandler); 
 client.on('error', errorHandler);
 client.on('disconnect', disconnectHandler);
 client.on('message', messageHandler);client.open() 
 .catch(err => {
 console.error('Could not connect: ' + err.message);
 });// Helper function to print results in the console 
 function printResultFor(op) {
 return function printResult(err, res) {
 if (err) console.log(op + ' error: ' + err.toString());
 if (res) console.log(op + ' status: ' + res.constructor.name);
 };
 }
 I am getting following error, can some one help here to resolve this issue ?
  
 
- 
					
					
					
					
 @somnathavhad move your file Device.js to the root directory, then from the root directory execute the command node ./Device.js You will receive an error but please post the error message. 
 
- 
					
					
					
					
 @crispyoz I tried to run it from root folder, please go through below error. 
  then I try to run npm install, it has created Package.jason in root folder. I tried to run Device.js had same issues 
  
 
- 
					
					
					
					
 @somnathavhad if you re-run: node --max_old_space_size=512 $(which npm) install azure-iot-device-mqtt What is the output? 
 
- 
					
					
					
					
 @crispyoz : I get following error. I  
 
- 
					
					
					
					
 @crispyoz I shared results. Still not able to proceed. It seems I need to upgrade node js version. ? 
 
- 
					
					
					
					
 @somnathavhad I was able to duplicate your issue, it appears there is a mismatch with node and npm. I took a new Omega2Pro out of the box. Run oupgrade so to be on the latest stable firmware. Next follow the document link I sent. Now if you run the command "npm i" to get the npm information, this will tell you that you should upgrade npm to a new version. If I do upgrade npm then your error will occur, if I do not upgrade npm then npm has no error and can install the azure-iot-device-mqtt From this I conclude you have installed an incompatible npm. I don't think you can upgrade node because Onion Team produced a custom version of node so it could work on Omega2. 
 
- 
					
					
					
					
 @crispyoz Thank you for your support and help very much appreciated . I see some thing wrong with my installation , I tried on brand new omega 2S and I am able to run the Node JS code and send data from Omega to Azure cloud. I am good now.