README
DFU Device Simulator
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
- Login to nrfcloud dev site and go to the accounts page and grab your API key
- Install AWS CLI
- 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 '"')
- Run the simulator:
node dist/device.js
Create a new job using the update-device script
- Open a new terminal window
- Install AWS CLI
- If running this on your own AWS account, ensure that Event-based Messages for jobs are enabled in AWS IoT Settings.
- 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>
- 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
- 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>
- 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"}'
- Verify the file was uploaded
curl $API_HOST/v1/firmwares -H "Authorization: Bearer $API_KEY" | jq
- Export the filename
export FILENAME=<filename you uploaded>
- 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"
- 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" }'
- View your DFU job
curl $API_HOST/v1/dfu-jobs -H "Authorization: Bearer $API_KEY" | jq
- 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"