This demo showcases the integration of mock LwM2M protocol devices with Shifu, alongside devices using HTTP and MQTT protocols. Shifu communicates with the LeShan server via the LwM2M protocol, demonstrating seamless device management and interaction.
All devices connect to the LwM2M mock server (LeShan server).
Create a security object for each device in the LeShan server to ensure communication security.
Operate the devices in the LwM2M mock server.
Demo tested on:
- Raspberry Pi 4B (4GB RAM) with Raspberry Pi OS (64-bit)
- K3s v1.29.4+k3s1
- Docker v26.0.0
- Go 1.22.3
git clone --recurse-submodules https://github.com/Edgenesis/shifu-lwm2m-demo.git
Start the LeShan mock server as LwM2M Server in Cloud.
docker run --name leshan-server -d -p 5683:5683/udp -p 5684:5684/udp -p 8080:8080/tcp edgenesis/lwm2m-demo-leshan-server:nightly
This command runs the LeShan server in detached mode, with ports 5683 and 5684 for CoAP and CoAPs communication and port 8080 for HTTP access.
k3s is a lightweight Kubernetes distribution ideal for edge computing and IoT environments. We can create a K3s cluster by running the following command:
curl -sfL https://get.k3s.io | sh -
Shifu is a Kubernetes native, production-grade, protocol & vendor agnostic IoT gateway. Install Shifu:
kubectl apply -f shifu/pkg/k8s/crd/install/shifu_install.yml
If you have additional nodes to join your K3s cluster:
curl -sfL https://get.k3s.io | K3S_URL='https://<master-node-ip>:6443' K3S_TOKEN='<TOKEN>' K3S_NODE_NAME=shifu-worker sh -
Replace <master-node-ip>
with the IP address of your master node and <TOKEN>
with the token found at /var/lib/rancher/k3s/server/token
on the master node.
Edit the lwm2m/lwm2m-edgedevice.yaml
file and set it to the coap/coaps listening address of the LwM2M server:
apiVersion: shifu.edgenesis.io/v1alpha1
kind: EdgeDevice
metadata:
name: edgedevice-lwm2m
namespace: devices
spec:
sku: "LwM2M Device"
...
gatewaySettings:
protocol: lwm2m
address: leshan.eclipseprojects.io:5684 # edit it to lwm2m server address
...
kubectl apply -f lwm2m
docker run --rm -it edgenesis/lwm2m-demo-leshan-client:nightly bash
java -jar leshan-client-demo-2.0.0-SNAPSHOT-jar-with-dependencies.jar -u coaps://<ip>:30001 -n test -i hint -p ABC123 -c TLS_PSK_WITH_AES_128_CCM_8
Replace <ip>
with the IP address of your local server (you can get this from ip addr
in Linux).
kubectl delete -f lwm2m
To create a HTTP mock device:
kubectl apply -f http/mockdevice
This command creates the mock HTTP device, which simulates HTTP interactions.
Edit the http/deviceshifu/http-edgedevice.yaml
file and set it to the coap/coaps listening address of the LwM2M server:
apiVersion: shifu.edgenesis.io/v1alpha1
kind: EdgeDevice
metadata:
name: edgedevice-thermometer
namespace: devices
spec:
...
gatewaySettings:
protocol: lwm2m
address: leshan.eclipseprojects.io:5684 # edit it to lwm2m server address
...
kubectl apply -f http/deviceshifu
kubectl delete -f http/deviceshifu
To create a mock MQTT device Mosquitto(MQTT Broker):
kubectl apply -f mqtt/mockdevice
Edit the mqtt/deviceshifu/mqtt_edgedevice.yaml
file and set it to the coap/coaps listening address of the LwM2M server:
apiVersion: shifu.edgenesis.io/v1alpha1
kind: EdgeDevice
metadata:
name: edgedevice-mqtt
namespace: devices
spec:
...
gatewaySettings:
protocol: lwm2m
address: leshan.eclipseprojects.io:5684 # edit it to lwm2m server address
...
kubectl apply -f mqtt/deviceshifu
Publish a message to the mock MQTT device using the following command:
kubectl exec -it deploy/mosquitto -n devices -- mosquitto_pub -t "/topic/channel1" -m 'Hello, World'
kubectl delete -f mqtt/deviceshifu
in this part we re-use the MQTT device mosquitto and deploy a new deviceShifu with a customized process function.
Edit the mqtt-customized/mqtt_edgedevice.yaml
file and set it to the coap/coaps listening address of the LwM2M server and the MQTT broker address:
apiVersion: shifu.edgenesis.io/v1alpha1
kind: EdgeDevice
metadata:
name: edgedevice-mqtt
namespace: devices
spec:
sku: "MQTT Device"
connection: Ethernet
address: mosquitto-service.devices.svc.cluster.local:31883 # edit it to MQTT broker address
...
gatewaySettings:
protocol: lwm2m
address: leshan.eclipseprojects.io:5684 # edit it to LwM2M server address
...
kubectl apply -f mqtt-customized
Publish a message to the mock MQTT device using the following command:
kubectl exec -it deploy/mosquitto -n devices -- mosquitto_pub -t "/test/topic1" -m '{"xAngle":-73.08371735,"yAngle":-90}'
kubectl delete -f mqtt-customized
kubectl delete node shifu-worker
This command removes the worker node from the k3s cluster.