question

Jeffrey-9032 avatar image
0 Votes"
Jeffrey-9032 asked asergaz edited

Node.JS Simulated Device Certificate error

I am trying to deploy a device to my Edge device that simulates temp data with Node.js. I have gone though and made my certificates and added it to my Node.Js module and imported the cert I get

Could not connect: mqtt.js returned Failure on first connection (Not authorized): self signed certificate in certificate chain error

in the logs for the module. This is what I have in the config.toml. I tried with a host name got the same result I am getting using the IP address. (Wanted to remove DNS as an issue)

hostname = "10.90.27.150"
trust_bundle_cert = "file:///home/edge/certificates/certs/azure-iot-test-only.root.ca.cert.pem"
[edge_ca]
cert = "file:///home/edge/certificates/certs/iot-edge-device-ca-10.90.27.150-full-chain.cert.pem"
pk = "file:///home/edge/certificates/private/iot-edge-device-ca-10.90.27.150.key.pem"


Here is my module I am trying to use:

 // Copyright (c) Microsoft. All rights reserved.
 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
    
 'use strict';
    
 var fs = require('fs');
 var Protocol = require('azure-iot-device-mqtt').Mqtt;
 // Uncomment one of these transports and then change it in fromConnectionString to test other transports
 // var Protocol = require('azure-iot-device-http').Http;
 // var Protocol = require('azure-iot-device-amqp').Amqp;
 var Client = require('azure-iot-device').Client;
 var Message = require('azure-iot-device').Message;
    
 // 1) Obtain the connection string for your downstream device and to it
 //    append this string GatewayHostName=<edge device hostname>;
 // 2) The Azure IoT Edge device hostname is the hostname set in the config.yaml of the Azure IoT Edge device
 //    to which this sample will connect to.
 //
 // The resulting string should look like the following
 //  "HostName=<iothub_host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>;GatewayHostName=<edge device hostname>"
 var deviceConnectionString = "HostName=removed-devices.net;DeviceId=Virtual_Humidity_Simulator;SharedAccessKey=removed=;GatewayHostName=10.90.27.150" //process.env.DEVICE_CONNECTION_STRING;
    
 // Path to the Edge "owner" root CA certificate
 var edge_ca_cert_path = "./azure-iot-test-only.root.ca.cert.pem";
    
 // fromConnectionString must specify a transport constructor, coming from any transport package.
 var client = Client.fromConnectionString(deviceConnectionString, Protocol);
    
 var connectCallback = function (err) {
   if (err) {
     console.error('Could not connect: ' + err.message);
   } else {
     console.log('Client connected');
     client.on('message', function (msg) {
       console.log('Id: ' + msg.messageId + ' Body: ' + msg.data);
       // When using MQTT the following line is a no-op.
       client.complete(msg, printResultFor('completed'));
       // The AMQP and HTTP transports also have the notion of completing, rejecting or abandoning the message.
       // 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.
     });
    
     // Create a message and send it to the IoT Hub every two seconds
     var sendInterval = setInterval(function () {
       var windSpeed = 10 + (Math.random() * 4); // range: [10, 14]
       var temperature = 20 + (Math.random() * 10); // range: [20, 30]
       var humidity = 60 + (Math.random() * 20); // range: [60, 80]
       var data = JSON.stringify({ deviceId: 'myFirstDownstreamDevice', windSpeed: windSpeed, temperature: temperature, humidity: humidity });
       var message = new Message(data);
       message.properties.add('temperatureAlert', (temperature 28) ? 'true' : 'false');
       console.log('Sending message: ' + message.getData());
       client.sendEvent(message, printResultFor('send'));
     }, 2000);
    
     client.on('error', function (err) {
       console.error(err.message);
     });
    
     client.on('disconnect', function () {
       clearInterval(sendInterval);
       client.removeAllListeners();
       client.open(connectCallback);
     });
   }
 };
    
 // Provide the Azure IoT device client via setOptions with the X509
 // Edge root CA certificate that was used to setup the Edge runtime
 var options = {
   ca : fs.readFileSync(edge_ca_cert_path, 'utf-8'),
 };
    
 client.setOptions(options, function(err) {
   if (err) {
     console.log('SetOptions Error: ' + err);
   } else {
     client.open(connectCallback);
   }
 });
    
 // 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);
   };
 }
azure-iot-edge
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello @Jeffrey-9032 can you bring more details of the OS where IoT Edge is running and if what you are trying to achieve is to Connect a downstream device to an Azure IoT Edge gateway using azure iot sdk for node: https://github.com/Azure/azure-iot-sdk-node?

Thanks!


0 Votes 0 ·

0 Answers