Comments (8)
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.
I dont know what missing in this line. What must it be??
The error that the string not right
from esp32-mqtt.
@shirish47 can you show me your project
You work fine with cJSON ?
from esp32-mqtt.
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.
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.
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.
I got it, thanks @shirish47
from esp32-mqtt.
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)
- What does t MQTT_MSG_TYPE_PINGRESP mean? HOT 9
- Connection refuse, Unknown reason HOT 1
- Samsung's ARTIK Cloud connect error HOT 1
- not able to connect to hive mqtt HOT 3
- example fails compile, when fixed fails to execute properly HOT 1
- Compilation error - undeclared struct field "reconnect_cb" HOT 5
- Pending Subscription message is overwritten by Publish => subscribe callback is missed HOT 1
- esp32 as server broker
- mqtt_stop() sometimes does not work.
- Documentation for MQTT Library by tuanpmt HOT 1
- Client socket being destroyed after unsuccessful read from mqtt_start_receive_schedule() function HOT 2
- Offline Buffer issues trigger watchdog timer error
- Installing library in Eclipse environment
- Connection refused, not authorized HOT 9
- Mqtt_1 HOT 1
- lwt message is blank
- last will
- esp32 constant reboot HOT 1
- Link to new version is 404
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from esp32-mqtt.