-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Closed
Labels
Area: LibrariesIssue is related to Library support.Issue is related to Library support.
Milestone
Description
Board
ESP32 (Wemos D1 mini)
Device Description
Custom ESP32, got no photo here sorry (am in the office right now)
Hardware Configuration
SD-Card Shield connected
Version
v2.0.3
IDE Name
Arduino IDE
Operating System
Linux Manjaro
Flash frequency
40
PSRAM enabled
no
Upload speed
115200
Description
I have a sketch which works as follows:
On client-side (webserver) the user selects a date which is then sent (per Ajax) to the ESP.
if the date is e.g. "2022-06-08" a file named "2022-06-08.bin" is opened for reading and then read line by line.
With esp core 1.0.6 it works perfectly.
Same code with esp core 2.0.2 was very slow (cause there was still a bug i think)
Now with esp core 2.0.3 it is really fast again, but after a few date selects (means a few SD-card-Read-cycles) the esp totally freezes (webserver is not available any more).
Sketch
// Just Parts of the Sketch
// I cannot publish complete Sketch here because of copyrights (customer project)
struct data {
uint16_t value1;
uint16_t value2;
uint16_t value3;
// 10 more values, just an example
};
void tagesauswertung (char* datum_select, int startpos, int endpos, int tag, int monat, int jahr, int rand_key_sent) {
struct data Messung;
DynamicJsonDocument auswertung_arr(4800);
sprintf(filename_auswertung, "/e/%s.bin", datum_select);
auswertungenFile = SD.open(filename_auswertung, FILE_READ);
if (auswertungenFile) {
int i = 0;
int alive = 0;
while (auswertungenFile.available()) {
auswertungenFile.read((uint8_t *)&Messung, sizeof(Messung));
if (i >= startpos && i <= endpos) {
// As the file is very big, the JSON-Array would get to o big
// So the server sends "startpos" and "endpos" and thus the content is transferred partly to the client
alive = 1;
auswertung_arr[i - startpos]["val1"] = Messung.value1;
auswertung_arr[i - startpos]["val2"] = Messung.value2;
auswertung_arr[i - startpos]["val3"] = Messung.value3;
// ten more values, just an example
}
i++;
}
if (alive == 1) {
// Still Data left --> Send it to the client
String jsonString;
serializeJson(auswertung_arr, jsonString);
Serial.println(jsonString);
server.send(200, "application/json", jsonString);
} else {
server.send(200, "text/plain", "Done");
}
} else {
server.send(200, "text/plain", "Fehler");
}
}
Debug Message
ESP Freezes (Webserver is not available anymore)
Other Steps to Reproduce
No response
I have checked existing issues, online documentation and the Troubleshooting Guide
- I confirm I have checked existing issues, online documentation and Troubleshooting guide.To pick up a draggable item, press the space bar. While dragging, use the arrow keys to move the item. Press space again to drop the item in its new position, or press escape to cancel.
Metadata
Metadata
Assignees
Labels
Area: LibrariesIssue is related to Library support.Issue is related to Library support.
Type
Projects
Status
Done
Relationships
Development
Select code repository
Activity
VojtechBartoska commentedon Jun 9, 2022
Can you please take a look @P-R-O-C-H-Y?
basementmedia2 commentedon Jun 9, 2022
Hi @VojtechBartoska
what do you mean? Does @P-R-O-C-H-Y have a solution for this problem?
Or should i post my issue in his repo?
Best wishes
Daniel
P-R-O-C-H-Y commentedon Jun 9, 2022
Hi @basementmedia2
I take care of issues about SD lib :) Thats why @VojtechBartoska assigned me and pinged me in comment.
I will take a look on you issue soon, no need to repost issue anywhere ;)
P-R-O-C-H-Y commentedon Jul 13, 2022
Hi @basementmedia2,
Can you try it with latest 2.0.4 release? Thanks
basementmedia2 commentedon Jul 13, 2022
Hi @P-R-O-C-H-Y ,
sure, I will check it these days and come back with my experience.
Thank you for your help!
Best wishes
Daniel
basementmedia2 commentedon Jul 19, 2022
Hi @P-R-O-C-H-Y ,
unfortunately updating to 2.0.4 does not fix the problem.
I have created a test sketch, which I think you can use to reproduce the problem yourself.
Here are the bin-files, used in the function "auswertung"
bin-files.zip
The JSON-File you must pölace somewhere on your server (but i don't know if this part is really relevant).
If you run the sketch with 1.0.6, it runs endlessly correctly through - the function "day evaluation" is started again and again. With 2.0.3 and with 2.0.4 after a few runs (sometimes more, sometimes less) the loop-task is shot down. Then only the separate task "keepWiFiAlive" is running.
Could it be that the heap memory is running full and therefore at some point the void loop can no longer be executed?
I also noticed that any execution of SD.begin() when no SD card is inserted causes the heap to grow until then eventually the memory overflows. No idea if this is intentional or if it's a bug.
You can test this with the following sketch and NOT putting an SD-Card in the SD-Card-Reader:
Would be great to get further support.
Best wishes
Daniel
P-R-O-C-H-Y commentedon Jul 20, 2022
Hi @basementmedia2 Thanks for really nice report 👍
Gonna take a look on the issue, and for sure the heap cannot grow. There has to be some bug.
I will inform you about the progress :)
15 remaining items
P-R-O-C-H-Y commentedon Jul 22, 2022
Hi @basementmedia2,
Yes I understand your problem and I know where to look at, where is the issue. But it's weird that in 1.0.6 it works.
You might take a look on the While function I mentioned. The problem is for sure there.
Huge output log to show you the issue, I have added print at the start to show startpos and endpos you are passing to the function and print "messung" to the part where you actually needs the data and write them to the array :
Why you always read whole file, and use just a part of the data? I think using
seek
you can always jump to the part of file where you need to read and change the while to something like read only the part from startpos to endpos.P-R-O-C-H-Y commentedon Jul 22, 2022
In the log above, you can see that you don't even get the data for position 88 - 128.
basementmedia2 commentedon Jul 22, 2022
Hi,
for explanation:
The file contains measurings of a water meter. For each day there is a separate file.
Each file contains a few hundred measurings.
At the frontend (HTML) i can select a day and then the measurings of this day should be displayed in a table.
Therefore the file (which is on SD-card) is to be read out and then send to the webserver frontend.
The measurings i send in JSON format.
Because of the high number of measurings, JSON file would get to big to send all in one.
So my idea was to read out the file partially.
But i understand, that my current way is not really the best (also in performance i think).
Also i don't understand, why 88-128 is not read out??
Do you have a tip, how to do this better?
Can you help me optimizing the posted code, would be really great help.
I'm still learning every day and not an expert.
Thank you in advance again
P-R-O-C-H-Y commentedon Jul 22, 2022
Hi, thanks for explanation.
I don't know much about JSON file handling so I cannot help on this side.
For the SD card. For sure close the file ofter finishing reading. I would change the function to read whole file but after you hit some point line now 16 "lines" of data, just send it. And continue reading. Don't split it by closing and opening file again.
After opening file, you can get size. From that you can read multiple bytes at once and just fill the array, that can improve the speed.
About these 88-128 data was not read I am not sure, but it shows that the code is not safe and some faults are happening.
I can try to change that a bit, but like I said, I am not familiar with JSON.
basementmedia2 commentedon Jul 22, 2022
Hi,
the JSON part is no problem for me.
But how to optimize the while loop so that not always the whole file is read althought i just need a part of it - that is more a problem ;-( If you have an idea how to optimize this would be great help.
You wrote
"I would change the function to read whole file but after you hit some point line now 16 "lines" of data, just send it."
Current procedure is:
and you mean something like this
But if you could demonstrate how to use seek (you mentioned) would be great - so that not always the whole file is read.
Best wishes
Daniel
P-R-O-C-H-Y commentedon Jul 22, 2022
Hi,
I have edited the code a bit, but now I see your comments and don't think it is what you need. I did not know, that server requests just part of the data. I thought that server server request while file, and you send it divided into smaller parts.
Take a look on my change, it never failed for me now. (I am currently on 170th loop)
After opening file, I check the file size, divide it by number of bytes in your Message (2+2+4) and after that call read that exact times that are data for in the file. Meanwhile send the JSON when you already read 16 messages into array and send it, then clear it (don't know if needed, but did it that it should be the some now, like you have it before). This send JSON send is happening each 16 messages read and last JSON send as after all lines were read. Then just close the file and send the finish.
I did not use seek in the code above, but its easy to use. You just need position where you want to move in the file. The call the seek function to the point. You can find a lot of examples on the internet :)
PS: There are few Serial prints left for better debugging :)
basementmedia2 commentedon Jul 22, 2022
OK so THANK YOU VERY MUCH for so much help
I will try to get the rest (e.g. seek) by myself.
We will see if my brain gets a damage on it ;-)
Best wishes and nice weekend
P-R-O-C-H-Y commentedon Jul 22, 2022
You are welcome :)
If you have any problem, you can write any comment directly to this issue.
I will close it, but feel free to comment ;)
Thanks and nice weekend for you too.
lbernstone commentedon Jul 24, 2022
And, just as background advertising, if you prefer to spend your brain damage learning a data managment tool instead of managing the bits, you can take a look at using https://github.com/lbernstone/rrdtool_ESP32 and just deliver the raw rrd files to the client for presentation.
basementmedia2 commentedon Jul 25, 2022
Hi @P-R-O-C-H-Y ,
the corrected sketch no works with 2.0.4 as it should, but:
As told my sketch works as follows:
Current procedure is:
This works fine if WiFi-Connection is good.
But with 2.0.4 if WiFi-connection is not so good it happens sometime, that some JSON-values seem not to "reach" the webserver which results in incomplete statistic (i show the values in a graphic).
WIth 1.0.6 it works also perfect when WIFi-Connection is not so good. It then takes longer time but in the end ALL values reach the webserver.
So two questions left:
Best wishes
Daniel
lbernstone commentedon Jul 25, 2022
There is explicitly no warranty for the code in this repository. There is also no SLA on this forum, so any deadlines imposed, are yours to manage. If you are unable to make 2.0.4 work for you, then I would recommend you stick with 1.0.6. There will be no new features added to 1.0.6, but if you have a static product, you should choose to stick with what is working.
basementmedia2 commentedon Jul 25, 2022
OK, But do you have an idea, why 1.0.6 has another behaviour in my case than 2.0.4.
And do you think that i will anyway be kind of forced to update to 2.0.4 in the near future.
Sorry for this maybe stupid question but i try to find out, if it is better to getting 2.0.4 working or go with 1.0.6.