Uploaded image for project: 'Help-Desk'
  1. Help-Desk
  2. HELP-15359

[fiware-stackoverflow] Temperature Sensor: LWM2M objects 3303 created

    Details

    • Type: Monitor
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: FIWARE-TECH-HELP
    • Labels:

      Description

      Created question in FIWARE Q/A platform on 24-01-2019 at 14:01
      Please, ANSWER this question AT https://stackoverflow.com/questions/54347716/temperature-sensor-lwm2m-objects-3303-created

      Question:
      Temperature Sensor: LWM2M objects 3303 created

      Description:
      Working with wakaama client implementation of the LWM2M and Fiware Orion (of course using IoTAgent). I already have raspberry Pi sensor collecting temperature and stores in SQLite DB.

      The scenario I want implement is to have the IoTAgent observes the wakaama LWM2M client (I mean client send the data to the IoTAgent.

      I already created the 3303 object in wakaama/example/client/object_temperature.c then build, and I can confirm the client connects to the IoTAgent as far IoT logs (see below).

      time=2019-01-24T12:51:15.270Z | lvl=DEBUG | corr=3e578c67-f589-4348-afaf-225509e9c787 | trans=3e578c67-f589-4348-afaf-225509e9c787 | op=IoTAgentNGSI.MongoDBDeviceRegister | srv=n/a | subsrv=n/a | msg=Looking for device with id [raspiSensorTV]. | comp=IoTAgent
      time=2019-01-24T12:51:15.299Z | lvl=DEBUG | corr=n/a | trans=n/a | op=LWM2MLib.Registration | msg=Registration request ended successfully
      time=2019-01-24T12:51:15.349Z | lvl=DEBUG | corr=n/a | trans=n/a | op=LWM2MLib.InformationReporting | msg=Observing value from resource /3303/0/0 in device [1]

      But unfortunately, no are measure is sent by the client. Is anything wrong with my object_temperature.c? Code reproduced:

      #include "liblwm2m.h"
      #include "lwm2mclient.h"

      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      #include <ctype.h>
      #include <time.h>
      #include <sqlite3.h>

      //Resource Id's
      #define RES_SENSOR_VALUE 5700
      #define RES_SENSOR_UNITS 5701

      typedef struct prv_instance

      { struct _prv_instance_ * next; uint16_t shortID; double temp; char unit[10]; }

      prv_instance_t;

      static uint8_t prv_temperature_read(uint16_t instanceId,
      int * numDataP,
      lwm2m_data_t ** dataArrayP,
      lwm2m_object_t * objectP)
      {
      prv_instance_t * targetP;
      int i;
      sqlite3 *db;
      sqlite3_stmt *res;
      char temperature_val[30];

      targetP = (prv_instance_t *)lwm2m_list_find(objectP->instanceList, instanceId);
      if (NULL == targetP) return COAP_404_NOT_FOUND;

      fprintf(stderr, "----------------- Entering in prv_temperature ----------------\n");
      // connect to the backend
      int rc = sqlite3_open("../../urbansense.sqlite3", &db);

      if (rc != SQLITE_OK)

      { fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); return 1; }

      else

      { fprintf(stderr, "Connection successful.\n" ); }

      rc = sqlite3_prepare_v2(db, "SELECT temperature_data FROM basic_environment ORDER BY ID DESC LIMIT 1", -1, &res, 0);
      if (rc != SQLITE_OK)

      { fprintf(stderr, "Failed to fetch data: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); return 1; }

      rc = sqlite3_step(res);
      if (rc == SQLITE_ROW)

      { sprintf(temperature_val, "%s", sqlite3_column_text(res, 0)); fprintf(stdout, "Temperature value ==== : %s\n", sqlite3_column_text(res, 0)); sqlite3_finalize(res); sqlite3_close(db); }

      if(*numDataP == 0)

      { *dataArrayP = lwm2m_data_new(1); if (*dataArrayP == NULL) return COAP_500_INTERNAL_SERVER_ERROR; *numDataP = 1; (*dataArrayP)[0].id = RES_SENSOR_VALUE; (*dataArrayP)[1].id = RES_SENSOR_UNITS; }

      for (i = 0; i < *numDataP; i++)
      {
      switch((*dataArrayP)[i].id)

      { case RES_SENSOR_VALUE: lwm2m_data_encode_float(targetP->temp, *dataArrayP +1); break; case RES_SENSOR_UNITS: lwm2m_data_encode_string(targetP->unit, *dataArrayP +1); break; default: return COAP_404_NOT_FOUND; }

      }
      return COAP_205_CONTENT;
      }

      lwm2m_object_t * get_object_temperature()
      {
      /*

      • The get_object_temperature function create the object itself and return a pointer to the structure that represent it.
        */
        lwm2m_object_t * temperatureObj;

      temperatureObj = (lwm2m_object_t *)lwm2m_malloc(sizeof(lwm2m_object_t));

      if (NULL != temperatureObj)
      {
      memset(temperatureObj, 0, sizeof(lwm2m_object_t));

      /*

      • Assigns it's unique ID 3303
        */
        temperatureObj->objID = LWM2M_TEMPERATURE_OBJECT_ID;

      /*

      • and its unique instance
        *
        */
        temperatureObj->instanceList = (lwm2m_list_t *)lwm2m_malloc(sizeof(lwm2m_list_t));
        if (NULL != temperatureObj->instanceList) { memset(temperatureObj->instanceList, 0, sizeof(lwm2m_list_t)); }

        else

        { lwm2m_free(temperatureObj); return NULL; }

      temperatureObj->readFunc = prv_temperature_read;
      temperatureObj->userData = lwm2m_malloc(sizeof(prv_instance_t));

      }

      return temperatureObj;
      }

      void free_object_temperature(lwm2m_object_t * objectP)
      {
      if (NULL != objectP->userData)

      { lwm2m_free(objectP->userData); objectP->instanceList = NULL; }

      if(NULL != objectP->instanceList)

      { lwm2m_free(objectP->instanceList); objectP->instanceList = NULL; }

      lwm2m_free(objectP);
      }

        Activity

        Hide
        backlogmanager Backlog Manager added a comment -

        2019-01-24 15:05|CREATED monitor | # answers= 0, accepted answer= False

        Show
        backlogmanager Backlog Manager added a comment - 2019-01-24 15:05|CREATED monitor | # answers= 0, accepted answer= False

          People

          • Assignee:
            fermin Fermín Galán
            Reporter:
            backlogmanager Backlog Manager
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated: