This guide will walk you though integrating a LWM2M device to Azure Digital Twins using an available solution template.
az dt list --query "[].{name:name,id:id}"
id
field for use later.az ad sp create-for-rbac --name http://{name for sp} --scopes {id from #1} --role bcd981a7-7f74-457b-83e1-cceb9e632ffe
{
"appId": "bffc9e45-xxxxx",
"displayName": "http://qwerty-adt-data-access",
"name": "bffc9e45-5a3f-xxxxxxx",
"password": "XGR.lE~xxxxxxxxxW-O2p56aFI",
"tenant": "3b48acfa-3149-xxxxxxxx"
}
We have created a sample model that can generically represent any device. This model is not practical for production use cases, but for the purposes of getting started it is highly effective. The steps below will walk you through importing the model to your digital twins instance.
You can download the generic model here: model.json
{
"@id": "dtmi:com:tartabit:generic;1",
"@type": "Interface",
"@context": [
"dtmi:dtdl:context;2"
],
"displayName": "Generic Object",
"contents": [
{
"@type": "Property",
"name": "name",
"displayName": "Name",
"schema": "string"
},
{
"@type": "Property",
"name": "type",
"displayName": "Type",
"schema": "string"
},
{
"@type": "Property",
"name": "properties",
"displayName": "Properties",
"schema": {
"@type": "Map",
"mapKey": {
"name": "param",
"schema": "string"
},
"mapValue": {
"name": "value",
"schema": {
"@type": "Object",
"fields": [
{
"displayName": {
"en": "Name"
},
"name": "metaName",
"schema": "string"
},
{
"displayName": {
"en": "Description"
},
"name": "metaDesc",
"schema": "string"
},
{
"displayName": {
"en": "Boolean"
},
"name": "boolean",
"schema": "boolean"
},
{
"displayName": {
"en": "Double"
},
"name": "double",
"schema": "double"
},
{
"displayName": {
"en": "String"
},
"name": "string",
"schema": "string"
},
{
"name": "location",
"displayName": {
"en": "Location"
},
"schema": {
"@type": "Object",
"fields": [
{
"displayName": {
"en": "Latitude"
},
"name": "lat",
"schema": "double"
},
{
"displayName": {
"en": "Longitude"
},
"name": "lon",
"schema": "double"
}
]
}
}
]
}
}
}
},
{
"@type": "Telemetry",
"name": "telemetry",
"displayName": "Telemetry",
"schema": {
"@type": "Map",
"mapKey": {
"name": "param",
"schema": "string"
},
"mapValue": {
"name": "value",
"schema": {
"@type": "Object",
"fields": [
{
"displayName": {
"en": "Name"
},
"name": "metaName",
"schema": "string"
},
{
"displayName": {
"en": "Description"
},
"name": "metaDesc",
"schema": "string"
},
{
"displayName": {
"en": "Boolean"
},
"name": "boolean",
"schema": "boolean"
},
{
"displayName": {
"en": "Double"
},
"name": "double",
"schema": "double"
},
{
"displayName": {
"en": "String"
},
"name": "string",
"schema": "string"
},
{
"name": "location",
"displayName": {
"en": "Location"
},
"schema": {
"@type": "Object",
"fields": [
{
"displayName": {
"en": "Latitude"
},
"name": "lat",
"schema": "double"
},
{
"displayName": {
"en": "Longitude"
},
"name": "lon",
"schema": "double"
}
]
}
}
]
}
}
}
},
{
"@type": "Relationship",
"@id": "dtmi:com:tartabit:device:rel_has_generics;1",
"name": "rel_has_generics",
"displayName": "generic has generics",
"target": "dtmi:com:tartabit:generic;1",
"properties": [
{
"@type": "Property",
"name": "desc",
"schema": "string"
}
]
}
]
}
az dt model create -n {instance_or_hostname} --models {path_to_downloaded_file}
upload model
button to import the model JSON file.Solution Tempates
at the bottom of the window.LWM2M to Azure Digital Twins
template.import
.Start triggers
to start the triggers upon import and click import
.You should receive an import result like this:
Let's review the key elements of this solution:
The ADT service connector enables both sending updates to ADT (via the Azure Digital Twins API) and receiving updates from ADT via events from an Event Hub. At this time, we have only configured the ADT API, and have not configured the Event Hub connection to receive twin change events.
This trigger listens for lwm2m-simple-data
generic events and publishes data to ADT. It will attempt to update the twin first, and if that does not succeed it will create a new twin with the endpoint key ad the ID of the twin. This uses the generic data format.
This trigger listens on an event hub for changes to the twin and returns the JSON-Patch data to the trigger for processing. Specifically it is handling changes to the /properties/command/string field to accept a set of comma separated parameters. The trigger will reply in /properties/commandReply/string field.
Endpoints
page.Triggers
page.At this point you can log into the Azure Digital Twins explorer and browse your device in the twin.
Query
button in the top right.Congratulations!
While this guide did not specifically address processing twin changes, you can check out the Azure Digital Twin - Handle Changes
trigger. To enable changes, follow the steps below:
Azure Digital Twin - Handle Changes
trigger./properties/command/string
value and set it to write,3/0/15,UTC
to try to write to the device./properties/commandReply/string
to see the result.Ensure that the trigger
LWM2M - Flatten device data
is present and started. If you do not see this trigger, then re-import theLWM2M Quickstart
solution template to get the latest version of the triggers.