Showing results for 
Search instead for 
Did you mean: 

Unable to store large payloads after subscribe in a topic with LwIP mqtt library.


I have a payload that I am publishing and its length is 305 bytes. I have configure the file lwip_opts.h and more specifically the parameter which has to do with the length of the incoming data. 


* Number of bytes in receive buffer, must be at least the size of the longest incoming topic + 8

* If one wants to avoid fragmented incoming publish, set length to max incoming topic length + max payload length + 8





Opening debugger I noticed that in the callback function where the payload arrives, which is

mqtt_incoming_data_cb(....., const u8_t *data, u16_t len, ...), the length of the payload topic was 305

bytes which is equal with the payload length that I am sending through MQTT-Explorer. However, the data

parameter does not store properly the contents of the payload. I noticed that the payload in data parameter has only a few contents

of the data and also the contents are stored in the opposite side from right to left.


I should mention that the payload is a JSON format string and also I am using this library in a project which has

STM32H7 microcontroller and the environment is the STM32CubeIDE version: 1.13.2


The way that I am handling the incoming payload is like below:

char *json = (char *)malloc((len+1)*sizeof(char));

strncpy(json, (char *)data, len); // The data variable from the callback.


The payload that I want to receive is this:

{"timestamp": "2023-11-20T12:34:04", "meas_interval_sec": 5, "i1": {"name": "i1", "value": 0.0, "unit": "A"}, "i2": {"name": "i2", "value": 0.0, "unit": "A"}, "i3": {"name": "i3", "value": 0.0, "unit": "A"}, "real_power": {"name": "real_power", "value": 0.0, "unit": "W"}, "apparent_power": {"name": "apparent_power", "value": 0.0, "unit": "VA"}}


and the payload that is stored in the variable json is this:

Name : json

Details:0x24034298 ": \"W\"}, \"apparent_power\": {\"name\": \"apparent_power\", \"value\": 0.0, \"unit\": \"VA\"}}"

Default:0x24034298 ": \"W\"}, \"apparent_power\": {\"name\": \"apparent_power\", \"value\": 0.0, \"unit\": \"VA\"}}"






I cannot understand why this is happening. I can ensure that this is not valid because after that in my code

I am calling a function, which handles this JSON string in order to extract data with jsmn.h library. The result

of this function is that it cannot recognize the string as a valid json format.


I would appreciate if anyone suggest me with something in order to overcome this obstacle.

Thank you in advance.