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 !

Parents Reply Children
  • 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.

  • Good day 

    I created another instance and the function was created as expected.  Thank you for the suggestion!  I am able to set the sensor values within my main.c file. The lwm2m_engine_set_float function returns 0 but I more often than not receive a timeout server side.  Have you any idea how to reduce this? 

  • Please let me know about any feedback as to why Leshan isnt updating as well please.  Thank you !

  • Hi, and sorry for the late reply.

    The documentation is a bit outdated. The lwm2m library no longer uses the custom float32_value_t and float64_value_t types.

    Instead, they now use the regular float and double types.

    Simply changing the type of the sensor_value is all that is needed to make the value appear in Leshan.

    Please see this updated .diff

    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..29d40bab3 100644
    --- a/samples/nrf9160/lwm2m_client/src/main.c
    +++ b/samples/nrf9160/lwm2m_client/src/main.c
    @@ -143,6 +143,46 @@ void send_periodically_work_handler(struct k_work *work)
     }
     #endif
     
    +double sensor_value = 1.5;
    +
    +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 += 0.2;
    +	LOG_WRN("Sensor_value: %0.2f", sensor_value);
    +
    +	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 +253,7 @@ static int lwm2m_setup(void)
     	defined(CONFIG_LWM2M_CLIENT_UTILS_LOCATION_ASSIST_EVENTS)
     	location_event_handler_init(&client);
     #endif
    +	init_resource();
     	return 0;
     }
     
    

    Best regards,

    Didrik

  • Good afternoon

    Thank you so much for the feedback.  I was able to use what you suggested!

    Kind regards,

    Hassan

Related