@nrfcloud/dfu-device-simulator

AWS IoT Thing simulator for nRF91 DFU

Usage no npm install needed!

<script type="module">
  import nrfcloudDfuDeviceSimulator from 'https://cdn.skypack.dev/@nrfcloud/dfu-device-simulator';
</script>

README

DFU Device Simulator npm version

Build Status
Greenkeeper badge semantic-release Commitizen friendly

AWS IoT Thing simulator for nRF91 DFU.

Getting Started

# install deps
npm i

# compile to js
npx tsc

Commands

# create a device and subscribe to jobs topic
# if using -cr, -c and -k are not needed.
node dist/device.js \
  -d <device id> \
  -e <mqtt endpoint> \
  -a <initial fw version> \
  -cr <certs response from API> \
  -c <location of device cert> \
  -k <location of device key> \

# create a job for a device
node dist/update-device.js \
  -d <device id> \
  -e <mqtt endpoint> \
  -a <next fw version> \
  -b <s3 bucket> \
  -f <name of the firmware file> 

Create device and subscribe to job updates

  1. Login to nrfcloud dev site and go to the accounts page and grab your API key
  2. Install AWS CLI
  3. Setup your environment:
# setup API variables
export API_KEY=<your_api_key>
export API_HOST=<your_api_host, e.g., https://api.dev.nrfcloud.com>

# create a new generic device
curl -X POST $API_HOST/v1/devices -H "Authorization: Bearer $API_KEY"

# find the device id of the new device and export it (remember this for next step)
curl $API_HOST/v1/devices -H "Authorization: Bearer $API_KEY" | jq
export DEVICE_ID=<your_device_id>

# create and attach a device cert:
export CERTS_RESPONSE=$(curl -X POST $API_HOST/v1/devices/$DEVICE_ID/certificates -H "Authorization: Bearer $API_KEY")

# either export 'MQTT_ENDPOINT' manually or via the 'aws iot' command (remember for next step)
export MQTT_ENDPOINT=$(aws iot describe-endpoint --endpoint-type iot:Data-ATS | grep endpointAddress | awk '{ print  $2; }' | tr -d '"')
  1. Run the simulator:
node dist/device.js

Create a new job using the update-device script

  1. Open a new terminal window
  2. Install AWS CLI
  3. If running this on your own AWS account, ensure that Event-based Messages for jobs are enabled in AWS IoT Settings.
  4. Setup your environment:
# export device id and mqtt endpoint from previous steps
export DEVICE_ID=<device id>
export MQTT_ENDPOINT=<mqtt endpoint>

# export region
export AWS_REGION=us-east-1

# export aws account id
export AWS_ACCOUNT=$(aws sts get-caller-identity | jq -r '.Account')

# s3 bucket
export S3_BUCKET=<s3 bucket name>
  1. Run the simulator
node dist/update-device.js -f <firmware file in s3 bucket>.json -a <new firmware version string>

Create a new job using the Device API

  1. Open a new terminal window
# setup API variables
export API_KEY=<your_api_key>
export API_HOST=<your_api_host, e.g., https://api.dev.nrfcloud.com>
export DEVICE_ID=<device id from previous steps>
  1. Upload a dummy firmware file as a base64-encoded string.
curl -X POST $API_HOST/v1/firmwares -H "Authorization: Bearer $API_KEY" -d '{"file": "ewogICAgIm9wZXJhdGlvbiI6ImN1c3RvbUpvYiIsCiAgICAib3RoZXJJbmZvIjoic29tZVZhbHVlIgp9Cg==", "filename": "my-firmware.bin"}'
  1. Verify the file was uploaded
curl $API_HOST/v1/firmwares -H "Authorization: Bearer $API_KEY" | jq
  1. Export the filename
export FILENAME=<filename you uploaded>
  1. Enable DFU on the device (if not already enabled)
curl -X PATCH $API_HOST/v1/devices/$DEVICE_ID/state -d '{ "reported": { "device": { "serviceInfo": ["dfu"] } } }' -H "Authorization: Bearer $API_KEY"
  1. Create the DFU job
curl -X POST $API_HOST/v1/dfu-jobs -H "Authorization: Bearer $API_KEY" -d '{ "deviceIdentifiers": ["'$DEVICE_ID'"], "filename": "'$FILENAME'", "version": "1.1" }'
  1. View your DFU job
curl $API_HOST/v1/dfu-jobs -H "Authorization: Bearer $API_KEY" | jq
  1. Verify the job succeeded in the other tab where you ran node dist/device.js.

Clean up (if desired)

curl -X DELETE $API_HOST/v1/dfu-jobs/<jobId from GET /dfu-jobs> -H "Authorization: Bearer $API_KEY"
curl -X DELETE $API_HOST/v1/firmwares/$FILENAME -H "Authorization: Bearer $API_KEY"
curl -X DELETE $API_HOST/v1/devices/$DEVICE_ID -H "Authorization: Bearer $API_KEY"