How to Change Dusun’s IoT Gateway into a LwM2M Client?

Any standard protocol is compatible with the Dusun IoT gateway to transfer data to the cloud. We’ll demonstrate how to transfer data using the open-source Wakayama-based standard lwm2m protocol, which allows the gateways to be converted into lwm2m clients that send data collected from sensors to lwm2m servers.

We will use the The lwm2m server at https://leshan.eclipseprojects.io/ which is a leshan based sandbox.

The Video: Change Dusun’s IoT Gateway into a LwM2M Client.

Build the Wakaama LwM2M Client

The Wakaama demo client is used to demonstrate observe mode. Normally we can make a program for the gateway on a PC and cross-compile the program which will be copied to the gateway to run. The following steps show the procedure:

  1. open a terminal in a Linux PC in which git and cmake are installed.
  2. Clone the Wakaama source into a local folder. E.g.: /home/software Git clone GitHub - eclipse/wakaama: Eclipse Wakaama is a C implementation of the Open Mobile Alliance's LightWeight M2M protocol (LWM2M).
  3. Download the cross compile OpenWrt-Toolchain from the following website (figure 2):

http://archive.openwrt.org/barrier_breaker

DM 20220630163508 012

Figure 2. the OpenWrt-Toolchain for cross-compiling

  1. Decompress the downloaded OpenWrt Toolchain to a local folder (E.g.: home/software/) and add the toolchain path to the system path:

Open /etc/bash.bashrc and add the following to the end of the file:

Export

PATH=home/software/OpenWrt-Toolchain-ramips-for-mipsel_24kec+dsp-gcc-4.8-linaro_uClibc-0.9.33.2/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/bin:$PATH

Execute source /etc/bash.bashrc in a terminal.

  1. Execute the following commands to build the lwm2mclient:

cd /home/software/wakaama/examples/client

mkdir build

cd build

cmake-gui … (configure the cross-compiler according to figure 3)

make​

DM 20220630163508 013

Figure 3. the configuration of cross-compiler in cmake-gui

The compiled lwm2mclient features nine LWM2M objects:

  • Security Object (id: 0)
  • Server Object (id: 1)
  • Access Control Object (id: 2) as a skeleton
  • Device Object (id: 3) containing hard-coded values from the Example LWM2M Client of Appendix E of the LWM2M Technical Specification.
  • Connectivity Monitoring Object (id: 4) as a skeleton
  • Firmware Update Object (id: 5) as a skeleton.
  • Location Object (id: 6) as a skeleton.
  • Connectivity Statistics Object (id: 7) as a skeleton.
  • Test Object (id: 31024) with the following description:

Multiple

Object | ID | Instances | Mandatory |

Test | 31024 | Yes | No |

Resources:

Supported Multiple

Name | ID | Operations | Instances | Mandatory | Type | Range |

test | 1 | R/W | No | Yes | Integer | 0-255 |

exec | 2 | E | No | Yes | | |

dec | 3 | R/W | No | Yes | Float | |

Test the LwM2M client

Connecting the Dusun gateway and execute commands in the opened console:

./lwm2mclient -4 –h leshan.eclipseprojects.io (-4 means ipv4; -h means server)

The connection procedure is shown below:

DM 20220630163508 014

Figure 4. lwm2m client connection procedure

If the connection succeeds, the program will print state: STATE_READY. The connection id above is KMCstnKU3g. Connect https://leshan.eclipseprojects.io, we can find our clients by registration id(figure 5):

DM 20220630163508 015

Figure 5. the connected clients on the test leshan sandbox

Click our clients, and we can find the 9 objects listed above. Figure 6 shows the device(/3) object. Figure 7 shows the test(/31024) object:

DM 20220630163508 016

Figure 6. the device object

DM 20220630163508 017

Figure 7. the test object

Now, we can do data communication by lwm2m protocol. Clicking the read button right to the Battery Level whose resource is /3/0/9. Then the value[100] is read from the client and screened on the web which is shown as below:

DM 20220630163508 018

If we type: “change /3/0/9 88” in the client console and activate the observe button for resource /3/0/9, the value will be changed to 88 immediately which is shown as below.

DM 20220630163508 019

DM 20220630163508 020

By clicking the read button right to the test object instance 10, we can get all the values in the instance. The figure below shows the situation.

DM 20220630163508 021

By executing “change /31024/10/1 99” and “change /31024/10/3 5.5”, We can see the values in the server web has changed to 99 and 5.5 as illustrated in figures below:

DM 20220630163508 022

Developing your LwM2M Client On Dusun’s Gateway

  1. Developing your LWM2M client (for the Light Device and the Sensor device) is done by modifying the client example source code in the …/wakaama-master/examples/client folder on your Linux PC.

  2. Create new C file for every new object in your LWM2M client. For example, for the LWM2M client in the Light Device, you need to create a new object_light_profile.c file for the Light Profile object by modifying the object_device.c file in the …/wakaamamaster/examples/client folder. Adjust the write and read functions for the Light Profile object in object_light_profile.c. Adjust as well the get_object and free_object functions in object_light_profile.c.

  3. Adjust the lwm2mclient.c and lwm2mclient.h files in …/wakaamamaster/examples/client folder accordingly. In lwm2mclient.c, you need to initialize the Light Profile object in the beginning of the main function, and free the object memory allocation in the end of the main function. In lwm2mclient.c, you also need to set the right number of objects in your LWM2M client.

  4. Adjust file …/wakaama-master/core/liblwm2m.h by adding the ID of the new object under line 167.

  5. Adjust file …/wakaama-master/examples/client/CMakeLists.txt to include the new object file and any new libraries used in the modified code.

  6. Cross-compile the new client and run the new lwm2mclient on the Dusun gateway.