Hi!
I have a project, where I want to upload a Nextion Firmware from an ESP32, where the TFT file is stored on the ESP in SPIFFS file system. Yes I have found the ESPNexUpload library, but it looks way-way overkill. It is also full of string operations, what is considered as a big no-no in microcontrollers’ world. I checked the documentation of Nextion Upload Protocol V1.1, and it looks pretty easy and straightforward, so I implemented it… Aaaand it does not work.
The issue is, it sends the whole file, but the display says only about 90% of the packets has arrived. When I put a delay(100) (or 200 or 300 or 500) between each packet, then it goes up to 99% of all bytes, but it still waits for more. (It changes, but 6-8Kbytes are missing, so not only the last partial package.) When I put delay(1000) between each 4096 byte packages, then it works perfectly. (but takes ages to finish) What am I doing wrong? I think I perfectly followed the protocol documentation.
Ohh, and another strange thing: the 0x88 arrives at the end of the while loop from the display, but it still shows the white screen with the small red text, saying “SIZE: 1957708 writing DATA… 90% 1777664” (When I add delay(100), it stops at 99% and 1949696 bytes)
HardwareSerial SerialNextion(1);
SerialNextion.begin(115200, SERIAL_8N1, 25, 26);
File file = SPIFFS.open("/Nextion35.tft");
if(file){
size_t filesize = file.size();
SerialNextion.printf("whmi-wri %d,115200,0\xFF\xFF\xFF", filesize);
delay(1000);
WaitForNextionResponse(0x05, 10000);
char buf[4096];
size_t avail;
size_t readcount;
while(filesize > 0){
avail = file.available();
if(avail){
readcount = file.readBytes(buf, avail > 4096 ? 4096 : avail);
SerialNextion.write(buf, readcount);
filesize -= readcount;
// delay(1000); would solve the problem here
WaitForNextionResponse(0x05, 10000);
}
delay(1);
}
file.close();
WaitForNextionResponse(0x88, 10000);
SPIFFS.end();
}
void WaitForNextionResponse(byte ResposeToWait, uint32_t TimeoutMillis){
byte b = 0;
uint32_t waittime = millis();
while(b != ResposeToWait && (waittime + TimeoutMillis) < millis()){
if(SerialNextion.available()){
b = SerialNextion.read();
waittime = millis();
}
delay(1);
}
}