This guide will walk you though integrating a LWM2M device to Azure IoT Hub using an available solution template.
If you do not have an IoT Hub, or need help getting the credentials for your IoT Hub, check out these guides, you need the connection string for an account with 'iothubowner' access:
If you have a LWM2M device, you will need to save the following information for later:
If you don't have a LWM2M device, you can use the Anjay open source client from AVSystem. If you already have a LWM2M device, you can skip this step.
Go to https://github.com/AVSystem/Anjay to find the instructions to build and run the Anjay client on your PC. We will cover the required command line arguments in a later step.
You can choose your own endpoint, identity and pre-shared key at this point, or use the randomly generated values as part of the import.
To connect your device you will import three solution templates, the first two are common, and the third will depend on which device you are using:
LWM2M Quickstart
and adds a service connection to Azure IoT Hub and triggers to send telemetry, update the device twin, and process incoming method and C2D events.Click Solution Templates at the bottom of any page.
Find the LWM2M Quickstart template and click Import.
Fill in the template as follows:
a. LWM2M Lifetime
: The default lifetime to issue the device if it bootstraps, 60 seconds is good for a demo as it will keep any NAT window open for realtime communications.
Check Start triggers so the triggers will automatically start upon import.
Click Import.
Verify the import was successful:
Azure connection string
: The connection string you retrieved in part 1 of this guide.This section assumes you pick the generic Onboard LWM2M device
template, if you pick a device specific template your fields may vary, but it should be intuitive based on the device you are working with.
LWM2M endpoint
: This is the endpoint that your device will send when it registers.LWM2M identity
: This is the DTLS identity used for pre-shared key authentication.LWM2M Pre-shared key
: This is a hex encoded pre-shared key. If your device supports ASCII keys make sure to hexify it before submitting it.LWM2M Service
: This is the instance of the LWM2M server to use, if you followed the steps above then select LWM2M Server
.Now that everything has been imported, you can connect your first LWM2M device.
If you have your own device and entered the credentials in step #4, then you can connect it now.
If you are using the Anjay client, run the client with the credentials generated during the import phase, or that you manually entered. Remember that the identity must be hex encoded.
./output/bin/demo --endpoint-name <endpoint> --server-uri coaps://bs.tartabit.com:5684 --security-mode psk --identity <hex encoded identity> --key <pre-shared key>
You should see that your client reports connected.
Verify that the client is connected by navigating to Endpoints -> List and checking the status.
With the device connected, you should now see it connected in the IoT Hub, and data being updated in the device twin.
write
as the method name.{
"path": "3/0/14",
"data": "UTC"
}
Note, you can substitute any writable resource and value you desire.
Invoke Method
to send the method.Azure IoT Hub - Method Handler
trigger to see what is happening.