I don't know why but the topic doesn't receive the message. But if I try to print the message saved on the variable "buffer" on the serial it appears. I know the mqtt server it's working because it receive other messages.
What could be the problem?
I don't know why but the topic doesn't receive the message. But if I try to print the message saved on the variable "buffer" on the serial it appears. I know the mqtt server it's working because it receive other messages.
What could be the problem?
What could be the problem?
We don't have enough information
-> post the full code and details about your set up, components used, how things are wired, connected to the network, powered etc....
Ok, I've got a Raspberry pi 3b+ with installed mosquitto and node-red. The code is running on a Wemos d1 mini and the data comes from a sensor: MPU6050.
Anything more?
I attached the full code. Thanks a lot!
final.ino (1.56 KB)
functions.h (4.46 KB)
library.h (295 Bytes)
Ok, solved the problem. It turned out that was a simply MQTT_MAX_PACKET_SIZE problem, I just set the size to 1024. Thanks a lot!
MQTT_MAX_PACKET_SIZE is probably 256, might indeed be too small.
Hi guys, I'm having problem configuring the buffer size for my Json Doucument and the MQTT_MAX_PACKET_SIZE.
I've got a JsonDocument which contain an array of 100 object each with 4 values.
Using the arduinoJson Assistant (Assistant | ArduinoJson 6) it turned out I need to set the buffer size to 8192. Once I established the size of the json document what other changes should I do in order to send the document via MQTT to my topic?
I tried to set the MQTT_MAX_PACKET_SIZE to 8192 but it doesn't work
(Also I modified the pubsubclient.h is there a method to set the size directly in the .ino sketch?)
Thanks a lot
final.ino (1.46 KB)
functions.h (4.38 KB)
library.h (295 Bytes)
seems more a MQTT question than a JSON question
which library do you use? (this one offers setBufferSize())
J-M-L:
MQTT_MAX_PACKET_SIZE is probably 256, might indeed be too small.
well now the problem is that I can't send to mqtt topic the same json array but with 100 object inside. I setup the buffer size using arduino json assistant (https://arduinojson.org/v6/assistant/)) and it turned out i have to set the buffer size to 8192.
Once I established the size of the json document what other changes should I do in order to send the document via MQTT to my topic? I tried to set the MQTT_MAX_PACKET_SIZE to 8192 but it doesn't work
(Also I modified the pubsubclient.h is there a method to set the size directly in the .ino sketch?)
Topics merged to avoid duplication
Why not just send multiple packets sized a packet size that works?
J-M-L:
seems more a MQTT question than a JSON questionwhich library do you use? (this one offers setBufferSize())
I'm using pubsubclient which is the one you linked and i set the buffer size to 8192 but nothing happen. I also get a watchdog timer error
What is the maximum packet size will the broker accept?
The max MQTT packet size is 240Mb, a size that most BROKERS will restrict, especially public brokers.
You might check for rc131 messages from the broker. If a packet is received by the broker that is larger then the broker set size then the broker will just discard the message.
I tried the whole send a large packet thingy. Found sending many smaller packets worked better. It is better to lose a small packet of data then a large packet of data.
edward_radical:
I also get a watchdog timer error
May be the library is not suited for such large buffer then and blocks the thread for too long...
splitting in smaller chunks could then be the option
The code I use is the one you corrected for me, I just use another method to set the capacity of the jsonDocument.
void mqtt_publish(){
File file = LittleFS.open("/file.txt", "r");
if(!file){
Serial.println("file open failed");
while (true) yield();
}else {
const size_t capacity = JSON_ARRAY_SIZE(30) + 30*JSON_OBJECT_SIZE(4);
DynamicJsonDocument globalDoc(capacity);
String jsonLine;
jsonLine.reserve(capacity);
StaticJsonDocument <capacity> localDoc;
while (file.available()) {
jsonLine = file.readStringUntil('\n');
DeserializationError error = deserializeJson(localDoc, jsonLine);
if (!error) globalDoc.add(localDoc);
}
char buffer[capacity];
serializeJson(globalDoc, buffer);
client.publish("esp8266/JSON", buffer);
file.close();
}
}
Trying to reduce the packet sent to mqtt I noticed that I can send just a jsondocument with an array of 10 objects inside. Anyway I need to send an array of 100 objects. How can I split the array in 10 different chunks?
As soon as I try to send an array with 20 objects inside I get the wdt error.
Getting wdt errors you are accessing or going outside the bounds of the array.
say the array is 200 chunks long and you want to send ten at a time. loop through your array, create a packet with each entry and when a count reaches 10, send.
the other option is to go back to your initial file representation where each line was a JSON. then you would read a line and send it, then read the next line and send it etc..
Remember the MQTT Broker is slow. For the best results, the Broker should not be receiving more than 1000 publications per second; total.
J-M-L:
the other option is to go back to your initial file representation where each line was a JSON. then you would read a line and send it, then read the next line and send it etc..
I am trying to avoid this because my project is solar powered and I need to power on wifi every 5 minutes. And reading values from my sensor every 3 seconds gave me a total of 100 json object. Is there anything different from what I did till now that will let me send 100 objects every five minutes?
Idahowalker:
Remember the MQTT Broker is slow. For the best results, the Broker should not be receiving more than 1000 publications per second; total.
Since I'm sending 100 json objects shouldn't it work? sorry but I'm really new here (programming and especially mqtt) and I'm trying to understand
There are many factors as to why it is not working the way you want it to work that may be outside of your control. You could have a JSON doc size issue. What size packet does your MQTT Broker accept?
What is wrong with sending 10 packets of 10 data sets?