Closed fflori4n closed 2 years ago
you cant do delay(). its better to do with task manager. You must also create a FS and file like config.json .With buffer you can do something like this below (i attached two example scripts you should figured out ) and than should be all right:
I think this is what you need
char buffer[250];
sprintf(buffer,
"{\"host\": \"%s\", " "\"short_message\": \"%s\", " "\"Lattitude\": \"%f\", " "\"Longitude\": \"%f\", " "\"Message Title\": \"%s\", " "\"Message\": \"%s\" " "}", mqttMessage.host, mqttMessage.shortMessage.c_str(), mqttMessage.lattitude, mqttMessage.longitude, mqttMessage.messageTitle.c_str(), mqttMessage.message.c_str() );
bool PublishToTopic(mqttMessageType &mqttMessage, const char* topic, MQTT::QoS qos)
{
MQTT::Message message;
message.qos = qos;
message.retained = false;
message.dup = false;
mqttMessage.host = "XYZ";
mqttMessage.shortMessage = "This is a short_message";
char buffer[250];
sprintf(buffer,
"{\"host\": \"%s\", "
"\"short_message\": \"%s\", "
"\"Lattitude\": \"%f\", "
"\"Longitude\": \"%f\", "
"\"Message Title\": \"%s\", "
"\"Message\": \"%s\" "
"}",
mqttMessage.host, mqttMessage.shortMessage.c_str(),
mqttMessage.lattitude, mqttMessage.longitude,
mqttMessage.messageTitle.c_str(), mqttMessage.message.c_str()
);
cout << buffer;
message.payload = (void*)buffer;
message.payloadlen = strlen(buffer) + 1;
rc = client.publish(topic, message);
if (rc != 0) {
printf("Error %d from sending QoS 0 message\n", rc);
}
else
{
cout << "Message published successfully to " << topic << endl;
}
return true;
}
OR
void loop() {
StaticJsonBuffer<300> JSONbuffer; /// <<===== Here i preffer to use DynamicJsonDocument json(ESP.getMaxFreeBlockSize() - 4096); or DynamicJsonDocument json(1024);
JsonObject& JSONencoder = JSONbuffer.createObject(); ///// YOU CAN USE DYNAMICJSON is better
JSONencoder["device"] = "ESP32";
JSONencoder["sensorType"] = "Temperature";
JsonArray& values = JSONencoder.createNestedArray("values");
values.add(20);
values.add(21);
values.add(23);
char JSONmessageBuffer[100]; /// Exchange for dynamic buffer
JSONencoder.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
Serial.println("Sending message to MQTT topic..");
Serial.println(JSONmessageBuffer);
if (client.publish("esp/test", JSONmessageBuffer) == true) {
Serial.println("Success sending message");
} else {
Serial.println("Error sending message");
}
client.loop();
Serial.println("-------------");
}
ok thats should help you
void Serialize(){ Serial.println("[WSc] SENT: Broadcast message!!"); const size_t capacity = 1024; DynamicJsonDocument doc(capacity);
doc["temp"] = temp;
doc["humi"] = humi;
doc["humi1"] = humi1;
doc["humi2"] = humi2;
doc["humi3"] = humi3;
doc["humi4"] = humi4;
String buf;
serializeJson(doc, buf);
Serial.println("Json " + buf ); }
The problem was with the publishing function, managed to fix it with a workaround splitting up the string, suggested in the large msg example.
client.publish() was replaced with client.beginPublish(), client.print(), and client.endPublish(). As in: https://github.com/knolleary/pubsubclient/blob/master/examples/mqtt_large_message/mqtt_large_message.ino
Thanks for the suggestions, especially the JSON buffer stuff is much appreciated and will be part of the code in the future.
Hello,
When sending a JSON string larger than 128 chars,
client.publish()
causes heep overflow on the ESP32. I have changed the buffer size withclient.setBufferSize(512);
but maybe it's not working or I'm not using it correctly.here's the min reproducable code:
and the serial output: