Code Monkey home page Code Monkey logo

Comments (8)

shirish47 avatar shirish47 commented on July 2, 2024

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 ')'.

from esp32-mqtt.

huyrua291996 avatar huyrua291996 commented on July 2, 2024

I dont know what missing in this line. What must it be??
The error that the string not right

from esp32-mqtt.

huyrua291996 avatar huyrua291996 commented on July 2, 2024

@shirish47 can you show me your project
You work fine with cJSON ?

from esp32-mqtt.

shirish47 avatar shirish47 commented on July 2, 2024

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.

from esp32-mqtt.

huyrua291996 avatar huyrua291996 commented on July 2, 2024

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%.."

from esp32-mqtt.

shirish47 avatar shirish47 commented on July 2, 2024

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
#include "cJSON.h"
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.

from esp32-mqtt.

huyrua291996 avatar huyrua291996 commented on July 2, 2024

I got it, thanks @shirish47

from esp32-mqtt.

shirish47 avatar shirish47 commented on July 2, 2024

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.

from esp32-mqtt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.