Device Provisioning Service with Node.js

Set up SDK

Following steps for IoTHub SDK (https://github.com/Azure/autorest/blob/master/README.md#installing-autorest)

  1. Install autorest npm install -g autorest.

  2. Create Client Library for the DPS REST API autorest --input-file=[path to API definition] --output-folder=.\device-sdk --verbose --nodejs --openapi-type=arm --add-credentials

  3. Make sure the api-version for the requestUrl in `runtimeRegistration.js' is 2017-11-15

X509 - Individual Enrollment

  1. create a cert / key pair
openssl req -newkey rsa:2048 -nodes -x509 -keyout key.pem -out cert.pem -days 365 -config cert.cnf -subj "/C=US/ST=California/L=San Francis co/O=ACME, Inc./CN=YOURREGISTRATIONID/"

Note The common name CN must match the device registration.

  1. Follow the steps 4-6 of Create a device enrollment entry in the Device Provisioning Service in Create and provision an X.509 simulated device using IoT Hub Device Provisioning Service to upload your cert.

  2. Run the `runDevice.js' program

node runDevice.js [Device Registration ID] [ID Scope] [Path to Cert] [Path to Key]

X.509 - Group Enrollment

  1. Create your certs running the genCerts.sh script and follow the steps from the [IoT Hub SDK] (https://github.com/Azure/azure-iot-sdk-c/blob/master/tools/CACertificates/CACertificateOverview.md)

  2. Upload the cert.

  3. Create an enrollment group

  4. Run the `runDevice.js' program

node runDevice.js [Device Registration ID] [ID Scope] [Path to Full Chain PEM] [Path to Key]

Verify registration.

  1. Navigate to the IoT Hub and locate the device under IoT Devices. Make sure the status is enabled

  2. Invoke a Direct Method named YooHo

The node program should now exit.

Acknowledgements

Thank You to @amarzavery for pointers to the `CertificateCloudCredentials' and @simonporter for troubleshooting