# LWM2M

{% hint style="info" %}
LWM2M is currently in a preview only mode for Enterprise. Please contact us to get a demo.&#x20;
{% endhint %}

[Lightweight Machine to Machine](https://www.omaspecworks.org/what-is-oma-specworks/iot/lightweight-m2m-lwm2m/) specification is based on CoAP and is developed by the [Open Mobile Alliance](https://openmobilealliance.org/) 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](https://tools.ietf.org/html/rfc7252) (CoAP).&#x20;

![](/files/-M8PjeMZ4nTuXlWxtZJI)

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&#x20;

```
{
  state.temp = 20;
  state.led = false;
  
  state.so = new smartObject(); 
  state.so.init('temperature', 0, 
    {
        _state : state,
        sensorValue: {
        read: function (cb) {
            console.log('Read called for Sensor'); 
            cb(null,state.temp); },
        write: function (value, cb){ 
            console.log('Write called for Sensor with value', value)
            cb(null,state.temp); 
        }},
        units: 'C'	
    }); 


    // led
  state.so.init('lightCtrl', 0 , {
        _state: state,
        onOff: {
            read: function (cb) {
                cb(null, state.led);
            },
            write: function (val, cb) {
                state.led = val;
                cb(null, state.led);
            }
        }
    });


  //no need for return 
}

```

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.&#x20;

The message sending functions are also a little different in case of LWM2M

```
{
    state.temp = 25+index();
    
    /* Update using a trigger method
    state.so.write('temperature', 0, 'sensorValue', state.temp , function (err, data) {
        if (err) {
            console.log(err);   // Error: 'Resource is unwritable.'
            console.log(data);  // _unwritable_
        }
    });
    */
    
    
    // Read using trigger method
    /*
    state.so.dump('temperature', 0, function (err, data) {
        console.log(data);
        
    })
    */
    
	var payload = {
        pathname: '/.well-known/core',
        payload: '</>;hb,</1/0>;obs,</3/0>;obs,</4/0>;obs,</3303/0>;obs', 
        method: 'GET', 
        waitResponse: false,
        options: { 
            'Content-Format': 'application/link-format' 
        },
        query: '' 
	}
	
    return JSON.stringify(payload);
}
```

A JSON object must be returned in stringified form, indicating the next action to be performed.&#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.iotify.io/temp/protocol-settings/lwm2m.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
