Skip to content

Instantly share code, notes, and snippets.

@vspedr
Created September 22, 2017 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vspedr/e4c6932bee6a19ab868bc1c272edd287 to your computer and use it in GitHub Desktop.
Save vspedr/e4c6932bee6a19ab868bc1c272edd287 to your computer and use it in GitHub Desktop.
// based on: https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-node-node-getstarted#create-a-device-identity
const iothub = require('azure-iothub');
const connectionString = '{iothub connection string}';
const registry = iothub.Registry.fromConnectionString(connectionString);
const device = {
deviceId: 'myFirstNodeDevice'
};
registry.create(device, function(err, deviceInfo, res) {
if (err) {
registry.get(device.deviceId, printDeviceInfo);
}
if (deviceInfo) {
printDeviceInfo(err, deviceInfo, res);
}
});
const printDeviceInfo = (err, deviceInfo, res) => {
if (err) console.error(err);
if (deviceInfo) {
console.log('Device ID: ' + deviceInfo.deviceId);
console.log('Device key: ' + deviceInfo.authentication.symmetricKey.primaryKey);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment