Lwm2m - initialization issues(lwm2m Client Sample)

Good day

I am attempting to use lwm2m to post temperature sensor data.  At the moment I'm trying to to initialize object with ID 3300 (generic sensor)but it is not appearing on the Leshan Demo Server.   

My process of initialization is as follows:

// Initialize Object
//******************

typedef struct float32_value {
   int32_t val1;
   int32_t val2;
} float32_value_t;

static struct float32_value sensor_value = { 1, 0 };

 static void *read_cb(uint16_t obj_inst_id, uint16_t res_id, uint16_t res_inst_id, size_t *data_len)
 {
    /* Only object instance 0 is currently used */
    if (obj_inst_id != 0) {
       *data_len = 0;
    return NULL;
    }

    /* Demo: change the sensor value */
    ++sensor_value.val2;

    /* Return sensor value for the LwM2M library */
	
    lwm2m_engine_set_float("3300/0/5700", &sensor_value);
    *data_len = sizeof(sensor_value);
    return &sensor_value;
 }

int init_resource(void)
{
   lwm2m_engine_create_obj_inst("3300/0");
   lwm2m_engine_register_read_callback("3300/0/5700", read_cb);
   return 0;
}

The errors i'm getting are as follows:

[00:00:37.295,471] <err> net_lwm2m_registry: unable to find obj: 3300
[00:00:37.295,501] <err> net_lwm2m_registry: obj instance 3300/0 not found

I am aware that something must be added to the prj file to allow the use of the generic sensor(id:3300).  I might not be seeing something obvious or the documentation  might  be outdated in https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/nrf/libraries/networking/lwm2m_client_utils.html#example-callback

Please assist if you can.

Thank you !

  • Hi,

    I am looking into this, but need a day or two to test this myself.

    Best regards,

    Didrik

  • Is there a reason for why you don't use the temperature sensor object?

    I am aware that something must be added to the prj file to allow the use of the generic sensor(id:3300).

    Did you enable CONFIG_LWM2M_IPSO_GENERIC_SENSOR?

    Are you modifying the lwm2m_client sample?

    It already enables and uses the generic sensor object for the gas sensor. So you need to either increase the count (and use another instance), or disable the gas sensor from the sample.

  • Good afternoon

    Once I enable the generic or temperature sensors, how do I set the sensor values.  The functions i'm using are not working.  I intend to get temperatures from a UART, convert is to a double and then use 

    lwm2m_engine_set_float  to set the sensor value.  Is this the right way to do it?  When I do this I get the above mentioned errors.

    kind regards,

    Hassan

  • I tried to add a custom sensor myself to the lwm2m_client sample, following your code.

    You can see the changes I made to the sample here:

    diff --git a/samples/nrf9160/lwm2m_client/prj.conf b/samples/nrf9160/lwm2m_client/prj.conf
    index 188f4b896..c948978b3 100644
    --- a/samples/nrf9160/lwm2m_client/prj.conf
    +++ b/samples/nrf9160/lwm2m_client/prj.conf
    @@ -40,6 +40,7 @@ CONFIG_LWM2M_IPSO_SUPPORT=y
     CONFIG_LWM2M_IPSO_TEMP_SENSOR_VERSION_1_1=y
     CONFIG_LWM2M_IPSO_HUMIDITY_SENSOR_VERSION_1_1=y
     CONFIG_LWM2M_IPSO_GENERIC_SENSOR_VERSION_1_1=y
    +CONFIG_LWM2M_IPSO_GENERIC_SENSOR_INSTANCE_COUNT=2
     CONFIG_LWM2M_IPSO_PRESSURE_SENSOR_VERSION_1_1=y
     CONFIG_LWM2M_IPSO_PUSH_BUTTON_VERSION_1_1=y
     CONFIG_LWM2M_IPSO_ACCELEROMETER_VERSION_1_1=y
    diff --git a/samples/nrf9160/lwm2m_client/src/main.c b/samples/nrf9160/lwm2m_client/src/main.c
    index 522d046f2..c7ae9b4e9 100644
    --- a/samples/nrf9160/lwm2m_client/src/main.c
    +++ b/samples/nrf9160/lwm2m_client/src/main.c
    @@ -143,6 +143,51 @@ void send_periodically_work_handler(struct k_work *work)
     }
     #endif
     
    +typedef struct float32_value {
    +	int32_t val1;
    +	int32_t val2;
    +} float32_value_t;
    +
    +static struct float32_value sensor_value = {1,0};
    +
    +static void *read_cb(uint16_t obj_inst_id, uint16_t res_id, uint16_t res_inst_id, size_t *data_len)
    +{
    +	LOG_WRN("IN CUSTOM READ_CB");
    +	LOG_WRN("obj_inst_id: %d", obj_inst_id);
    +	if (obj_inst_id != 1) {
    +		*data_len = 0;
    +		return NULL;
    +	}
    +
    +	++sensor_value.val2;
    +	LOG_WRN("sensor value: %d.%d", sensor_value.val1, sensor_value.val2);
    +
    +	int err = lwm2m_engine_set_float("3300/1/5700", &sensor_value);
    +	if (err) {
    +		LOG_ERR("Could not set custom sensor value: %d", err);
    +		*data_len = 0;
    +		return NULL;
    +	}
    +	*data_len = sizeof(sensor_value);
    +	return &sensor_value;
    +}
    +
    +static int init_resource(void)
    +{
    +	int err = lwm2m_engine_create_obj_inst("3300/1");
    +	if (err) {
    +		LOG_ERR("Could not create custom sensor object: %d", err);
    +		return err;
    +	}
    +	err = lwm2m_engine_register_read_callback("3300/1/5700", read_cb);
    +	if (err) {
    +		LOG_ERR("Could not register callback for custom sensor: %d", err);
    +		return err;
    +	}
    +	LOG_WRN("DEVICE INITED SUCCESSFULLY");
    +	return 0;
    +}
    +
     static int lwm2m_setup(void)
     {
     #if defined(CONFIG_LWM2M_CLIENT_UTILS_DEVICE_OBJ_SUPPORT)
    @@ -213,6 +258,7 @@ static int lwm2m_setup(void)
     	defined(CONFIG_LWM2M_CLIENT_UTILS_LOCATION_ASSIST_EVENTS)
     	location_event_handler_init(&client);
     #endif
    +	init_resource();
     	return 0;
     }
     
    

    Creating the second instance of the object works as expected, and I can see it appear in Leshan. When I read the sensor value, the callback is called. However, Leshan always shows the value as 0.0, even though the callback should change the value each time it is called.

    But, I don't get the errors that you are seeing.

    I'll reach out to our LwM2M experts to see if we are doing something wrong when setting the value.

Related