EMQX LwM2M Gateway
The LwM2M Specifications is a Lightweight Machine to Machine protocol.
With emqx_lwm2m
, user is able to send LwM2M commands(READ/WRITE/EXECUTE/...) and get LwM2M response in MQTT way. emqx_lwm2m
transforms data between MQTT and LwM2M protocol.
emqx_lwm2m needs object definitions to parse data from lwm2m devices. Object definitions are declared by organizations in XML format, you could find those XMLs from LwM2MRegistry, download and put them into the directory specified by lwm2m.xml_dir
. If no associated object definition is found, response from device will be discarded and report an error message in log.
./bin/emqx_ctl plugins load emqx_lwm2m
wakaama is an easy-to-use lwm2m client command line tool.
Start lwm2mclient using an endpoint name ep1
:
./lwm2mclient -n ep1 -h 127.0.0.1 -p 5683 -4
To send an LwM2M DISCOVER command to lwm2mclient, publish an MQTT message to topic lwm2m/<epn>/dn
(where <epn>
is the endpoint name of the client), with following payload:
{
"reqID": "2",
"msgType": "discover",
"data": {
"path": "/3/0"
}
}
The MQTT message will be translated to an LwM2M DISCOVER command and sent to the lwm2mclient. Then the response of lwm2mclient will be in turn translated to an MQTT message, with topic lwm2m/<epn>/up/resp
, with following payload:
{
"reqID": "2",
"msgType": "discover",
"data": {
"code":"2.05",
"codeMsg": "content",
"content": [
"</3/0>;dim=8",
"</3/0/0>",
"</3/0/1>",
"</3/0/4>",
"</3/0/16>"
]
}
}
LwM2M Register and Update message will be converted to following MQTT message:
lwm2m/{?EndpointName}/up/resp
(configurable){
"msgType": {?MsgType},
"data": {
"ep": {?EndpointName},
"lt": {?LifeTime},
"sms": {?MSISDN},
"lwm2m": {?Lwm2mVersion},
"b": {?Binding},
"alternatePath": {?AlternatePath},
"objectList": {?ObjectList}
}
}
To send a downlink command to device, publish following MQTT message:
lwm2m/{?EndpointName}/dn
{
"reqID": {?ReqID},
"msgType": {?MsgType},
"data": {?Data}
}
If {?MsgType} = "read" or "discover":
{
"path": {?ResourcePath}
}
If {?MsgType} = "write" (single write):
{
"path": {?ResourcePath},
"type": {?ValueType},
"value": {?Value}
}
If {?MsgType} = "write" (batch write):
{
"basePath": {?BasePath},
"content": [
{
"path": {?ResourcePath},
"type": {?ValueType},
"value": {?Value}
}
]
}
If {?MsgType} = "write-attr":
{
"path": {?ResourcePath},
"pmin": {?PeriodMin},
"pmax": {?PeriodMax},
"gt": {?GreaterThan},
"lt": {?LessThan},
"st": {?Step}
}
If {?MsgType} = "execute":
{
"path": {?ResourcePath},
"args": {?Arguments}
}
If {?MsgType} = "create":
{
"basePath": "/{?ObjectID}",
"content": [
{
"path": {?ResourcePath},
"type": {?ValueType},
"value": {?Value}
}
]
}
If {?MsgType} = "delete":
{
"path": "{?ObjectID}/{?ObjectInstanceID}"
}
The response of LwM2M will be converted to following MQTT message:
"lwm2m/{?EndpointName}/up/resp"
{
"reqID": {?ReqID},
"imei": {?IMEI},
"imsi": {?IMSI},
"msgType": {?MsgType},
"data": {?Data}
}
If {?MsgType} = "write", "write-attr", "execute", "create", "delete", or "read"(when response without content):
{
"code": {?StatusCode},
"codeMsg": {?CodeMsg},
"reqPath": {?RequestPath}
}
If {?MsgType} = "discover":
{
"code": {?StatusCode},
"codeMsg": {?CodeMsg},
"reqPath": {?RequestPath},
"content": [
{?Link},
...
]
}
"</3>"
, "<3/0/1>;dim=8"
If {?MsgType} = "read"(when response with content):
{
"code": {?StatusCode},
"codeMsg": {?CodeMsg},
"content": {?Content}
}
[
{
"path": {?ResourcePath},
"value": {?Value}
}
]
If {?MsgType} = "ack", "data" does not exists
To observe/cancel-observe LwM2M client, send following MQTT PUBLISH:
lwm2m/{?EndpointName}/dn
{
"reqID": {?ReqID},
"msgType": {?MsgType},
"data": {
"path": {?ResourcePath}
}
}
Responses will be converted to following MQTT message:
lwm2m/{?EndpointName}/up/resp
{
"reqID": {?ReqID},
"msgType": {?MsgType},
"data": {
"code": {?StatusCode},
"codeMsg": {?CodeMsg},
"reqPath": {?RequestPath},
"content": [
{
"path": {?ResourcePath},
"value": {?Value}
}
]
}
}
lwm2m/{?EndpiontName}/up/notify
{
"reqID": {?ReqID},
"msgType": {?MsgType},
"seqNum": {?ObserveSeqNum},
"data": {
"code": {?StatusCode},
"codeMsg": {?CodeMsg},
"reqPath": {?RequestPath},
"content": [
{
"path": {?ResourcePath},
"value": {?Value}
}
]
}
}
emqx-lwm2m support DTLS to secure UDP data.
Please config lwm2m.certfile and lwm2m.keyfile in emqx_lwm2m.conf. If certfile or keyfile are invalid, DTLS will be turned off and you could read a error message in the log.
Apache License Version 2.0
EMQX Team.