This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Buffer JSON data and publish through MQTT nrf9160

Hi Dev Team,

I have an application wherein I am publishing my sensor data every hour through MQTT using a Thingy91. 

I am storing my sensor data value in a JSON format through cJSON library. I need to read this value every 10 mins, buffer it and send out all the values every hour.

So basically I need to store 6 values (values obtained every 10 mins through the hour) and send them out together every 1 hour.

I needed to know how could I buffer my sensor data and store it to send all of it together every hour.

I obtain my JSON format through this following code: 

void my_work_handler_1(struct k_work *work)
{

     char *desc2 = "BME680";
     char *id2 = "rH";
     char *type2 = "humidity";

     sensor1 = cJSON_CreateObject();

    cJSON_AddItemToObject(sensor1, "description", cJSON_CreateString(desc2));
    cJSON_AddItemToObject(sensor1, "Time", cJSON_CreateString(time_string));
    cJSON_AddItemToObject(sensor1, "value", cJSON_CreateNumber(testval2));
    cJSON_AddItemToObject(sensor1, "unit", cJSON_CreateString(id2));
    cJSON_AddItemToObject(sensor1, "type", cJSON_CreateString(type2));

       /* print everything */
        out1 = cJSON_Print(sensor1);
        printk("%s\n", out1);
        free(out1);
   /* free all objects under root and root itself */
        cJSON_Delete(sensor1);
        return;

}
K_WORK_DEFINE(my_work1, my_work_handler_1);

I save my json in the variable "out1" in the code. I wanted to know how I could buffer the output and store it in a variable, so I could publish that through MQTT every hour.

Regards,

Adeel.

Related