Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json string is "damaged" on transmitting #89

Open
g6094199 opened this issue Jul 5, 2023 · 2 comments
Open

json string is "damaged" on transmitting #89

g6094199 opened this issue Jul 5, 2023 · 2 comments
Labels
type: imperfection Perceived defect in any part of project

Comments

@g6094199
Copy link

g6094199 commented Jul 5, 2023

hi,

i try to send an json string to mqtt like so:

   const char topic[]      = "Hoechst/PHI-Log/data/json";

    .....

    mqttClient.beginMessage(topic);

    serializeJson(doc, mqttClient);
    mqttClient.endMessage();

and the received string is in the correct topic but like so:

null3���Hoechst/PHI-Log/data/json�{"clock":"22/06/2023 13:41:40","number":"","sht85_humid":"51.34","sht85_temp":"29.64","ENV_temp":"32.53","ENV_humid":"53.32","ENV_pressure":"99.91","ENV_illuminance":"81.29","sgp40_sraw":"32571","sgp40_voc":"0","scd30_humid":"43.94","scd30_temp":"32.79","scd3

someone knows whats going wrong? or is it a bug?

@per1234 per1234 added the type: imperfection Perceived defect in any part of project label Jul 5, 2023
@jwende
Copy link

jwende commented Feb 23, 2024

I did something similar today and it worked for me by providing a seperate buffer ...

StaticJsonDocument<200> doc;
Serial.print("Temperature = ");
Serial.print(bme.readTemperature());
Serial.println(" *C");
doc["Temperature"] = bme.readTemperature();

Serial.print("Pressure = ");
Serial.print(bme.readPressure() / 100.0F);
Serial.println(" hPa");
doc["Pressure"] = bme.readPressure() / 100.0F;

Serial.print("Humidity = ");
Serial.print(bme.readHumidity());
Serial.println(" %");
doc["Humidity"] = bme.readHumidity();

mqttClient.beginMessage(topic+"/json");
char output[128];
serializeJson(doc, output);
mqttClient.print(output);
mqttClient.endMessage();

Hope this helps ...

@domfie
Copy link

domfie commented May 25, 2024

Problem is here that the internal buffer is only 128 or 256 characters wide. You need to supply the length of the message to send so the lib will switch to streaming magic. I was searching for a malformed json error in AWS IoT and found this out after debugging the message in another topic. Message was 307 chars long and got truncated :).

You can see an example in the advanced example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: imperfection Perceived defect in any part of project
Projects
None yet
Development

No branches or pull requests

4 participants