-
Notifications
You must be signed in to change notification settings - Fork 60
How to monitor memory leak #12
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
Comments
Hey @baqwas, I might have experienced a similar problem. I suggest you declare a variable for the buffer before passing it on to MQTT library. I don't know if it should be necessary but I have also had some problems concerning buffers over at my project.
I made it a habit to use the long form of PS: I see you round your temperature values using |
Hello @tinxx, Thanks for letting me know. Much appreciated that you chose to follow up to help me out. Your suggestion was one of the first things that I tried but not in the elegant manner that you have illustrated. I am not in a position to try out your suggestion for another fortnight but I will definitely give it a workout and let you know. A few other unrelated (but nevertheless self-inflicted) anomalies to resolve in other projects for now. Perhaps, I was chasing the wrong library (viz. Ethernet) since it required a little extra code even with an empty SD card slot. I have to use LAN and cannot use Wi-Fi for this exercise. Kind regards. P.S. Thanks for the example! I'll use a Schenzen BME680 sensor eventually! |
Hello @tinxx, Do you have few minutes to spare to teach me a little about C++. In trying to adapt to your suggestion, I converted your example to the following for my case. Unfortunately, I am ignorant about string conversions and hence the compiler is issuing an error message: String payload = (String)JSON.stringify(MQTTmsg).c_str();
byte[] b = bytes(payload.begin(), payload.end());
//bytes.push_back('\0');
//char *c = &bytes[0];
MQTTclient.beginPublish(pubTopic, payload.length(), false);
MQTTclient.write(bytes, payload.length());
MQTTclient.endPublish(); The error message is:
I would like to stay with JSON payloads but clearly there is some ignorance on my part because my original code halts on two different boards with regularity (i.e. number of times the loop is executed). If I upload to both boards at the same time then the halts occur on both boards around the same time. Thanks for your guidance. Kind regards. P.S. #include <Ethernet.h>
#include <PubSubClient.h>
#include <Arduino_MKRENV.h>
#include <Arduino_JSON.h> |
Hi @baqwas, You use the following expression: So what you tell the compiler is:
So you tell the compiler that this C string is a C++ This is called type casting, it just works for uniform types like Calling So what you need to keep in mind is that there are three different kinds of strings you usually come across if you read or write Arduino code:
The first two types of string in the list are complex data types that come with many handy methods. Next you manually assign a buffer That is not necessary because a string of type 3 from the list above is a raw memory array of characters. Just use such one. So we first create the actual JSON string and assign it to a variable to keep it in memory until it goes out of scope.
So we go ahead and create our raw buffer by converting the
So now we have the raw data in memory until the variable Next, we want to send it via MQTT. I have experienced that longer messages got cropped by ArduinoMqttClient. If we look into PubSubClient's header file we can find some explanation about arbitrary length payloads and no memory copy.
So now we use the write function.
Finally we tell
Please note that I did not test any of the code in this comment and never used PubSubClient. |
Hello,
I've been using the following libraries for some LAN telemetry exercises:
The following statement causes a problem after about ~72 hours of operation at 1 second interval:
MQTTclient.publish(pubTopic, ((String)JSON.stringify(MQTTmsg)).c_str());
Is my method to serialize the JSON object the culprit? The number of fields in the JSON object does not change with the iterations. All 10 fields except the first (which has a constant string value) are floats as in the example below:
MQTTmsg["Temperature"] = roundf(ENV.readTemperature(unitTemperature) * DECIMALS) / DECIMALS;
The two boards (MKR1000, UNO) where this failure occurs repeatedly require a manual RESET. (I disable the SD card for MKR1000 HATs during setup with SPI CS pin 4). Is there an alternate way to serialize the JSON object using the Arduino_JSON (and not the 3rd party JSON) library? Thanks.
Kind regards.
The text was updated successfully, but these errors were encountered: