Comments (8)
I think I have a Similar problem. My Setup: ESP8266
My programm recieves a Json via MQTT every few minutes, and Publishes a Json vie MQTT. The Problem:
Every time I publish the Json, it Breaks my other Json.
Here Is a Piece of my Code:
for (uint8_t i = 0; i < length_d; i++) { //Here the Json is still Intact
Serial.print((char)payload_d[i]);
}
Serial.println(" 4");
client.publish("temp/display", mqtt_message, true); //Publishing a different Json
for (uint8_t i = 0; i < length_d; i++) { //Here the Json is Broken
Serial.print((char)payload_d[i]);
}
Serial.println(" 5");
And here is the Serial Print:
22:54:21:415 -> {"time":2000,"data":[{"name":"Leistung","value":50,"unit":"W"},{"name":"Durchfluss","value":520,"unit":"L"},{"name":"test","value":234,"unit":"J"}]} 4
22:54:21:429 -> y{"temp_display":22.36099243,"humidity_display":57.86027908}"},{"name":"Durchfluss","value":520,"unit":"L"},{"name":"test","value":234,"unit":"J"}]} 5
Is this a Bug?
from pubsubclient.
How is payload_d and length_d defined?
from pubsubclient.
Yes, its getting defined by my callback function:
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (uint8_t i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
payload_d = payload;
length_d = length;
}
from pubsubclient.
Yes, its getting defined by my callback function:
void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); for (uint8_t i = 0; i < length; i++) { Serial.print((char)payload[i]); } payload_d = payload; length_d = length; }
But how/where are they declared?
from pubsubclient.
Yes, its getting defined by my callback function:
void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); for (uint8_t i = 0; i < length; i++) { Serial.print((char)payload[i]); } payload_d = payload; length_d = length; }
But how/where are they declared?
Here is your problem, the callback provides you with a buffer (byte* payload) owned by the mqtt library, once you return from the callback, the mqtt will likely free the buffer or re-use it, so once you return that memory space can be used by anyone else. You need to copy the buffer in your callback:
payload_d = new char[sizeof(char) * length];
memcpy(payload_d, length);
And when you later have consumed the content of payload_d, you need to release it:
delete payload_d;
from pubsubclient.
The problem is, that I am consuming The payload_d slowly over time
from pubsubclient.
The problem is, that I am consuming The payload_d slowly over time
There is no magic, you cannot use the buffer that pubSub will re-circulate or free once you return from the callback, you need to copy it to a buffer you have allocated/own.
If you need to retain the information also after the next callback arrives you can do so by introducing an array of buffers which you maintain. But at some point of time you need to free your buffers - or you will exhaust your Heap.
That is just reallity.
Your problem seems to be unrelated to the lack of thread-safeing which was originally reported in this issue (#1055)
from pubsubclient.
Related Issues (20)
- Sometimes payload are mixed up with topic. HOT 1
- Callback is not called when i am pubishing audio data in bytes.
- Intermittent delay of minutes between ESP publish and receipt by mosquitto in a pi HOT 1
- Anomolous performing when using along with I2S
- Issue while setting keep alive zero. HOT 2
- Bug in PubSubClient::connected HOT 1
- Retain Flag not working HOT 1
- receiving arbitrarily large messages HOT 1
- Use hostname for the MQTT broker IP HOT 2
- Library when subscribing to third topic won't connect to ethernet
- Connecting and running smoothly on mobile hotspot while rc=-2 on fiber broadband wifi router
- Future of this module HOT 7
- Reconnect Problem. HOT 11
- Different error on Ubuntu arduino ide from windows arduino ide.
- #error "This library only supports boards with an AVR, SAM, SAMD, NRF52 or STM32F4 processor." HOT 1
- Client.connected() does not return false if MQTT Broker Pc disconnects from Internet or local network. HOT 1
- Can't Connect to mqtt broker after including SPI.h
- When callback is registered to topic that contains underscore, the underscore is replaced with space HOT 1
- Connection failed rc= -1 when connecting to RabbitMQ with token auth0 HOT 2
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 pubsubclient.