Closed huyrua291996 closed 6 years ago
hi, I am also used cJSON in my project but I made another file that handles cJSON messages those functions are called in the data_cb. however I would like to know what error you are getting ??
Also on the line where you defined double item you are missing ')'.
I dont know what missing in this line. What must it be?? The error that the string not right
@shirish47 can you show me your project You work fine with cJSON ?
I can't share the whole project I am sorry also its very large project containing BLE and WIFI functionality. I am building a BLE HUB for some devices. I though of showing parts of my code but its complex with many functions.
what is the error you are getting while you compile your code please post it. also could you please clean your code in that post with only lines that your are testing? Tomorrow I will test it myself.
I've editted as below.
I test this line
char *items = "{\"items\": \"huy\"}"; cJSON *request_json = cJSON_Parse(items); char *item = cJSON_GetObjectItem(request_json, "items")->valuestring; cJSON_Delete(request_json); ESP_LOGI(MQTT_TAG,"[APP] Publish data : %s", item);
the result of item is not "huy" it's "R%R%.."
OK I tested your lines and compiles fine for me. what are your getting as error? Also please not to forget to include cJSON.h and cJSON.c files in your folder and also in your main.c file add
Problem is that location of the item is cleaned by delete.
there are two ways to solve your problem.
char item[20];
void data_cb(void *self, void *params)
{
mqtt_client *client = (mqtt_client *)self;
mqtt_event_data_t *event_data = (mqtt_event_data_t *)params;
if(event_data->data_offset == 0) {
const char *items = "{\"items\": \"huy\"}";
cJSON *request_json = cJSON_Parse(items);
char *str=cJSON_GetObjectItem(request_json, "items")->valuestring;
// also you can dynamically assign memory to item and cpy str to it.
memcpy(item,str,strlen(str));
cJSON_Delete(request_json);
ESP_LOGI(MQTT_TAG,"[APP TEST] Publish data : %s", item);
char *topic = malloc(event_data->topic_length + 1);
memcpy(topic, event_data->topic, event_data->topic_length);
topic[event_data->topic_length] = 0;
ESP_LOGI(MQTT_TAG, "[APP] Publish topic: %s", topic);
free(topic);
}
}
another method.
void data_cb(void *self, void *params)
{
mqtt_client *client = (mqtt_client *)self;
mqtt_event_data_t *event_data = (mqtt_event_data_t *)params;
if(event_data->data_offset == 0) {
const char *items = "{\"items\": \"huy\"}";
cJSON *request_json = cJSON_Parse(items);
char *item=cJSON_GetObjectItem(request_json, "items")->valuestring;
ESP_LOGI(MQTT_TAG,"[APP TEST] Publish data : %s", item);
cJSON_Delete(request_json);
char *topic = malloc(event_data->topic_length + 1);
memcpy(topic, event_data->topic, event_data->topic_length);
topic[event_data->topic_length] = 0;
ESP_LOGI(MQTT_TAG, "[APP] Publish topic: %s", topic);
free(topic);
}
}
and other way is to delete cJSON after your have printed it. But some how in my code I have reference to the string given by cJSON_Print() and even after I delete it I am able to used it without issue from last 2 weeks.
I got it, thanks @shirish47
On the same note will my memory overflow if I keep using (char str*=cJSON_Print(item) and delete cJSON _Delete(item)later) str or str is global variable? and every time I make new cJSON I assign the string to str. Will it cause problem in long term ? I am not sure when the use of str is over. because I send the generated str to other functions and then they publish it(over MQTT). Right now I am not facing any problem but my project is important, and don't want it to fail in field.
i use cjson to handle string i received when subcribe a topic But if I use some cJSON function in event_handler loop in data_cb, it'll be wrong When i use outside the event loop, it works fine. Can you help me, here is my code in data_cb ``void data_cb(void self, void params) { mqtt_client client = (mqtt_client )self; mqtt_event_data_t event_data = (mqtt_event_data_t )params;
if(event_data->data_offset == 0) {
}``