Skip to content

Instantly share code, notes, and snippets.

@ridomin
Created April 21, 2020 06:15
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 ridomin/77aa86fe8a7d36389700ea653a8f2362 to your computer and use it in GitHub Desktop.
Save ridomin/77aa86fe8a7d36389700ea653a8f2362 to your computer and use it in GitHub Desktop.
create-aziothub-device.js
'use strict';
const iothub = require('azure-iothub')
const registry = iothub.Registry.fromConnectionString(process.env.IOTHUB_CONNECTION_STRING)
const device = {deviceId: 'ppr-bb-' + new Date().getTime(), status: 'enabled'}
registry.create(device, (err) => {
if (err) throw(err)
console.log(`Device ${device.deviceId} created`)
registry.get(device.deviceId, (err, devInfo) => {
if (err) throw(err)
const connectionString = `HostName=${registry._restApiClient._config.host};` +
`DeviceId=${devInfo.deviceId};` +
`SharedAccessKey=${devInfo.authentication.SymmetricKey.primaryKey}`
console.log(connectionString)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment