I'm sending files to the ESP that are approximately 200KB in size, the intent is to have 4 spaces in the filesystem for these files which contain RGB LED program data, and to be able to remotely replace them by sending a new file to the ESP. Admittedly my C/C++ is pretty poor so I might be making a stupid mistake here but I seem to be getting unreliable results. I can upload the file and have it overwrite the existing one between 1 and 5 times but never more than that. Upon the final write it seems to only write part of the file but behave as if it succeeded. After that subsequent attempts fail when calling SPIFFS.open on the file. I have cut out the lengthy LED handling code from the below code but I am hoping it is enough for somebody to spot my error (if there is one):
#include "arduino.h"
// Needed for SPIFFS
#include <FS.h>
// Wireless / Networking
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <ESP8266WiFi.h>
// PIN used to reset configuration. Enables internal Pull Up. Ground to reset.
#define PIN_RESET 13 // Labeled D7 on ESP12E DEVKIT V2
#define RESET_DURATION 30
#include <NeoPixelBus.h>
ESP8266WebServer server(80);
File UploadFile;
String uploadfilename;
void handleUpload(){
if (server.uri() != "/test") return;
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
if (UploadFile) {
Serial.println("Upload file is already open... that's bad");
}
uploadfilename = upload.filename;
Serial.print("Upload Name: "); Serial.println(uploadfilename);
printfsinfo();
if (SPIFFS.exists("/data/" + uploadfilename)) {
Serial.println("file exists - overwriting");
}
UploadFile = SPIFFS.open("/data/" + uploadfilename, "w");
if (!UploadFile) {
Serial.print("Creating file failed. File size: ");
Serial.print(upload.totalSize);
Serial.print(" filename length: ");
Serial.println(uploadfilename.length());
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
if (UploadFile) {
UploadFile.write(upload.buf, upload.currentSize);
Serial.print(upload.totalSize); Serial.print(",");
}
} else if (upload.status == UPLOAD_FILE_END) {
Serial.println("finished");
if (UploadFile) {
Serial.print("Received ");
Serial.print(upload.totalSize);
Serial.println(" bytes");
UploadFile.close();
}
}
}
void printfsinfo() {
FSInfo fs_info;
SPIFFS.info(fs_info);
Serial.print("total fs size: ");
Serial.println(fs_info.totalBytes);
Serial.print("used size: ");
Serial.println(fs_info.usedBytes);
}
void setup() {
Serial.begin(115200);
SPIFFS.begin();
printfsinfo();
server.on("/", [](){
server.send(200, "text/plain", "Hello World");
});
server.on("/test", HTTP_POST, []() {
if (uploaderror) {
server.send(500, "text/plain", "Error");
} else {
server.send(200, "text/plain", "Success");
}
}, handleUpload);
server.begin();
}
void loop() {
server.handleClient();
}
And here is the output. You can see on the final successful pass it seems to state it received all 178111 bytes from the file but the next run through the filesystem reports only 68021 used instead of the 183481 it should report (as there is some other data in the filesystem)
Upload Name: goodfile.dat
total fs size: 957314
used size: 3765
0,2048,4096,6144,8192,10240,12288,14336,16384,18432,20480,22528,24576,26624,28672,30720,32768,34816,36864,38912,40960,43008,45056,47104,49152,51200,53248,55296,57344,59392,61440,63488,65536,
67584,69632,71680,73728,75776,77824,79872,81920,83968,86016,88064,90112,92160,94208,96256,98304,100352,102400,104448,106496,108544,110592,112640,114688,116736,118784,120832,122880,124928,126
976,129024,131072,133120,135168,137216,139264,141312,143360,145408,147456,149504,151552,153600,155648,157696,159744,161792,163840,165888,167936,169984,172032,174080,176128,finished
Received 178111 bytes
Upload Name: goodfile.dat
total fs size: 957314
used size: 183481
file exists - overwriting
0,2048,4096,6144,8192,10240,12288,14336,16384,18432,20480,22528,24576,26624,28672,30720,32768,34816,36864,38912,40960,43008,45056,47104,49152,51200,53248,55296,57344,59392,61440,63488,65536,
67584,69632,71680,73728,75776,77824,79872,81920,83968,86016,88064,90112,92160,94208,96256,98304,100352,102400,104448,106496,108544,110592,112640,114688,116736,118784,120832,122880,124928,126
976,129024,131072,133120,135168,137216,139264,141312,143360,145408,147456,149504,151552,153600,155648,157696,159744,161792,163840,165888,167936,169984,172032,174080,176128,finished
Received 178111 bytes
Upload Name: goodfile.dat
total fs size: 957314
used size: 183481
file exists - overwriting
0,2048,4096,6144,8192,10240,12288,14336,16384,18432,20480,22528,24576,26624,28672,30720,32768,34816,36864,38912,40960,43008,45056,47104,49152,51200,53248,55296,57344,59392,61440,63488,65536,
67584,69632,71680,73728,75776,77824,79872,81920,83968,86016,88064,90112,92160,94208,96256,98304,100352,102400,104448,106496,108544,110592,112640,114688,116736,118784,120832,122880,124928,126
976,129024,131072,133120,135168,137216,139264,141312,143360,145408,147456,149504,151552,153600,155648,157696,159744,161792,163840,165888,167936,169984,172032,174080,176128,finished
Received 178111 bytes
Upload Name: goodfile.dat
total fs size: 957314
used size: 183481
file exists - overwriting
0,2048,4096,6144,8192,10240,12288,14336,16384,18432,20480,22528,24576,26624,28672,30720,32768,34816,36864,38912,40960,43008,45056,47104,49152,51200,53248,55296,57344,59392,61440,63488,65536,
67584,69632,71680,73728,75776,77824,79872,81920,83968,86016,88064,90112,92160,94208,96256,98304,100352,102400,104448,106496,108544,110592,112640,114688,116736,118784,120832,122880,124928,126
976,129024,131072,133120,135168,137216,139264,141312,143360,145408,147456,149504,151552,153600,155648,157696,159744,161792,163840,165888,167936,169984,172032,174080,176128,finished
Received 178111 bytes
Upload Name: goodfile.dat
total fs size: 957314
used size: 183481
file exists - overwriting
0,2048,4096,6144,8192,10240,12288,14336,16384,18432,20480,22528,24576,26624,28672,30720,32768,34816,36864,38912,40960,43008,45056,47104,49152,51200,53248,55296,57344,59392,61440,63488,65536,
67584,69632,71680,73728,75776,77824,79872,81920,83968,86016,88064,90112,92160,94208,96256,98304,100352,102400,104448,106496,108544,110592,112640,114688,116736,118784,120832,122880,124928,126
976,129024,131072,133120,135168,137216,139264,141312,143360,145408,147456,149504,151552,153600,155648,157696,159744,161792,163840,165888,167936,169984,172032,174080,176128,finished
Received 178111 bytes
Upload Name: goodfile.dat
total fs size: 957314
used size: 68021
file exists - overwriting
Creating file failed. File size: 0 filename length: 12
finished
Upload Name: goodfile.dat
total fs size: 957314
used size: 68021
file exists - overwriting
Creating file failed. File size: 0 filename length: 12
finished
I thought at first it was that the filesystem had to catch up so I introduced a 10 second delay between uploads but that doesn't seem to matter. Also, on reboot I go through and clear out all of the files in the "/data/" directory, and after one of these uploads fails removing the file will fail for the next 2-3 reboots then suddenly be successful.
Am I doing something wrong or am I perhaps dealing with bad flash memory on my esp module? I have read about some having knock-off flash chips that fail after just a few writes, am I dealing with that?... The plan isn't to replace these files often but I do need it to be reliable when I have to upload a new one.
Sorry for the wall of text, and thanks in advance for any help!