npm init
npm install azure-iot-device azure-iot-device-mqtt –save
'use strict'; // require('package-name') var clientFromConnectionString = require('azure-iot-device-mqtt').clientFromConnectionString; var Message = require('azure-iot-device').Message; var connectionString = 'HostName=ArduinoDemoHub.azure-devices.net;DeviceId=ArduinoAzureTwin;SharedAccessKey=Fqve19DXyMOx3vQI63eHxW0bK6VmT+R6iqPK23vhdiQ='; var client = clientFromConnectionString(connectionString); 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); }; } var connectCallback = function (err) { if (err) { console.log('Could not connect: ' + err); } else { console.log('Client connected'); // «» writeLine client.onDeviceMethod('writeLine', onWriteLine); // IoT Hub setInterval(function(){ var iotdata = Math.round((Math.random() * 25)); var data = JSON.stringify({ deviceId: 'ArduinoAzureTwin', iotdata: iotdata }); var message = new Message(data); console.log("Sending message: " + message.getData()); client.sendEvent(message, printResultFor('send')); }, 1000); } }; function onWriteLine(request, response) { response.send(200, 'Input was written to log.', function(err) { if(err) { console.error('An error ocurred when sending a method response:\n' + err.toString()); } else { console.log('Response to method \'' + request.methodName + '\' sent successfully.' ); } }); } client.open(connectCallback);
node index.js
$cert = New-SelfSignedCertificate -Type Custom -KeySpec Signature ` -Subject "CN=AlexRootCert" -KeyExportPolicy Exportable ` -HashAlgorithm sha256 -KeyLength 2048 ` -CertStoreLocation "Cert:\CurrentUser\My" -KeyUsageProperty Sign -KeyUsage CertSign
Get-ChildItem -Path "Cert:\CurrentUser\My"
$cert = Get-ChildItem -Path "Cert:\CurrentUser\My\FDF386B2AE761CBDD5A111CEF047CC5419F9425A"
New-SelfSignedCertificate -Type Custom -KeySpec Signature ` -Subject "CN=AlexClientCert" -KeyExportPolicy Exportable ` -HashAlgorithm sha256 -KeyLength 2048 ` -CertStoreLocation "Cert:\CurrentUser\My" ` -Signer $cert -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.2")
function New-CASelfsignedCertificate([string]$subjectName, [object]$signingCert, [bool]$isASigner=$true) { # Build up argument list $selfSignedArgs =@{"-DnsName"=$subjectName; "-CertStoreLocation"="cert:\LocalMachine\My"; "-NotAfter"=(get-date).AddDays(30); } if ($isASigner -eq $true) { $selfSignedArgs += @{"-KeyUsage"="CertSign"; } $selfSignedArgs += @{"-TextExtension"= @(("2.5.29.19={text}ca=TRUE&pathlength=12")); } } else { $selfSignedArgs += @{"-TextExtension"= @("2.5.29.37={text}1.3.6.1.5.5.7.3.2,1.3.6.1.5.5.7.3.1", "2.5.29.19={text}ca=FALSE&pathlength=0") } } if ($signingCert -ne $null) { $selfSignedArgs += @{"-Signer"=$signingCert } } if ($useEcc -eq $true) { $selfSignedArgs += @{"-KeyAlgorithm"="ECDSA_nistP256"; "-CurveExport"="CurveName" } } # Now use splatting to process this Write-Host ("Generating certificate {0} which is for prototyping, NOT PRODUCTION. It will expire in 30 days." -f $subjectName) write (New-SelfSignedCertificate @selfSignedArgs) } function New-CAVerificationCert([string]$requestedSubjectName) { $cnRequestedSubjectName = ("CN={0}" -f $requestedSubjectName) $verifyRequestedFileName = "D:\verifyCert4.cer" $ert = Get-ChildItem -Path "Cert:\CurrentUser\My\FDF386B2AE761CBDD5A111CEF047CC5419F9425A" Write-Host "Using Signing Cert:::" Write-Host $ert $verifyCert = New-CASelfsignedCertificate $cnRequestedSubjectName $ert $false Export-Certificate -cert $verifyCert -filePath $verifyRequestedFileName -Type Cert if (-not (Test-Path $verifyRequestedFileName)) { throw ("Error: CERT file {0} doesn't exist" -f $verifyRequestedFileName) } Write-Host ("Certificate with subject {0} has been output to {1}" -f $cnRequestedSubjectName, (Join-Path (get-location).path $verifyRequestedFileName)) } New-CAVerificationCert "8957B1DCD5D19DDFD06BFCC90A9B740C9EF7B4DBEC84C5AB"
$ert = Get-ChildItem -Path "Cert:\CurrentUser\My\FDF386B2AE761CBDD5A111CEF047CC5419F9425A"
sudo apt-get update sudo apt-get install openssl
openssl pkcs12 -in alex-client-cert.pfx -out key.pem -nodes
.\openssl pkcs12 -in D:\iotapp\alex-client-cert.pfx -out D:\iotapp\key.pem -nodes
'use strict'; var clientFromConnectionString = require('azure-iot-device-mqtt').clientFromConnectionString; var Message = require('azure-iot-device').Message; var connectionString = 'HostName=ArduinoDemoHub.azure-devices.net;DeviceId=NodeTwin;x509=true'; var fs = require('fs'); var certFile = 'alex-client-cert.cer'; var keyFile = 'key.pem'; var passphrase = 'KJu86Hfjw3jG'; var client = clientFromConnectionString(connectionString); 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); }; } var connectCallback = function (err) { if (err) { console.log('Could not connect: ' + err); } else { console.log('Client connected'); client.onDeviceMethod('writeLine', onWriteLine); // Create a message and send it to the IoT Hub every second setInterval(function(){ var iotdata = Math.round((Math.random() * 25)); var data = JSON.stringify({ deviceId: 'ArduinoAzureTwin', iotdata: iotdata }); var message = new Message(data); console.log("Sending message: " + message.getData()); client.sendEvent(message, printResultFor('send')); }, 1000); } }; function onWriteLine(request, response) { console.log('sdfdsf'); response.send(200, 'Input was written to log.', function(err) { if(err) { console.error('An error ocurred when sending a method response:\n' + err.toString()); } else { console.log('Response to method \'' + request.methodName + '\' sent successfully.' ); } }); } var options = { cert : fs.readFileSync(certFile, 'utf-8').toString(), key : fs.readFileSync(keyFile, 'utf-8').toString(), passphrase: passphrase }; client.setOptions(options); client.open(connectCallback);
Source: https://habr.com/ru/post/346598/
All Articles