Coder Social home page Coder Social logo

Comments (8)

Mallo321123 avatar Mallo321123 commented on June 16, 2024

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.

jonasbjurel avatar jonasbjurel commented on June 16, 2024

How is payload_d and length_d defined?

from pubsubclient.

Mallo321123 avatar Mallo321123 commented on June 16, 2024

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.

jonasbjurel avatar jonasbjurel commented on June 16, 2024

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.

jonasbjurel avatar jonasbjurel commented on June 16, 2024

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.

Mallo321123 avatar Mallo321123 commented on June 16, 2024

The problem is, that I am consuming The payload_d slowly over time

from pubsubclient.

jonasbjurel avatar jonasbjurel commented on June 16, 2024

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)

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.