LWM2M
LWM2M simulator is an enterprise only feature currently limited in preview.
LWM2M is currently in a preview only mode for Enterprise. Please contact us to get a demo.
Lightweight Machine to Machine specification is based on CoAP and is developed by the Open Mobile Alliance for the Internet of Things. The advantage of LWM2M over others is that it runs on UDP/DTLS and could even use SMS in some constrained cases. The underlying protocol for LWM2M is the Constrained Application Protocol (CoAP).
Currently the LWM2M client settings are minimalist. The client will automatically try to register to the provided server. In order to use the LWM2M client, you must build a smartobject in your state variable in device initialization. An example of a LWM2M device initialization code is
1
{
2
state.temp = 20;
3
state.led = false;
4
5
state.so = new smartObject();
6
state.so.init('temperature', 0,
7
{
8
_state : state,
9
sensorValue: {
10
read: function (cb) {
11
console.log('Read called for Sensor');
12
cb(null,state.temp); },
13
write: function (value, cb){
14
console.log('Write called for Sensor with value', value)
15
cb(null,state.temp);
16
}},
17
units: 'C'
18
});
19
20
21
// led
22
state.so.init('lightCtrl', 0 , {
23
_state: state,
24
onOff: {
25
read: function (cb) {
26
cb(null, state.led);
27
},
28
write: function (val, cb) {
29
state.led = val;
30
cb(null, state.led);
31
}
32
}
33
});
34
35
36
//no need for return
37
}
38
Copied!
Above Function will initiate two resource objects for your device. The server could read/write and observe to these resources and the handlers will be invoked.
The message sending functions are also a little different in case of LWM2M
1
{
2
state.temp = 25+index();
3
4
/* Update using a trigger method
5
state.so.write('temperature', 0, 'sensorValue', state.temp , function (err, data) {
6
if (err) {
7
console.log(err); // Error: 'Resource is unwritable.'
8
console.log(data); // _unwritable_
9
}
10
});
11
*/
12
13
14
// Read using trigger method
15
/*
16
state.so.dump('temperature', 0, function (err, data) {
17
console.log(data);
18
19
})
20
*/
21
22
var payload = {
23
pathname: '/.well-known/core',
24
payload: '</>;hb,</1/0>;obs,</3/0>;obs,</4/0>;obs,</3303/0>;obs',
25
method: 'GET',
26
waitResponse: false,
27
options: {
28
'Content-Format': 'application/link-format'
29
},
30
query: ''
31
}
32
33
return JSON.stringify(payload);
34
}
Copied!
A JSON object must be returned in stringified form, indicating the next action to be performed.
Last modified 1yr ago
Copy link