Skip to content

Commit 688ebe9

Browse files
updated to reflect the new library
- removed intermediate SPIFFS storage - directly send the upload buffer to the nextion over serial - added server.on("/fs", HTTP_POST.... for receiving upload file size (Workaround as the file content-length is of by +/- 200 bytes. Known issue: esp8266/Arduino#3787)
1 parent a8749f3 commit 688ebe9

File tree

1 file changed

+73
-47
lines changed

1 file changed

+73
-47
lines changed

examples/UploadServer/UploadServer.ino

Lines changed: 73 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <SPIFFS.h>
1212
#endif
1313

14-
#include <WiFiClient.h>
1514
#include <ESPNexUpload.h>
1615

1716
/*
@@ -20,21 +19,23 @@
2019
Serial pins are defined in the ESPNexUpload.cpp file
2120
*/
2221

23-
const char* ssid = "your_wlan_ssid";
24-
const char* password = "your_wlan_password";
25-
const char* host = "nextion";
22+
const char* ssid = "your_wlan_ssid";
23+
const char* password = "your_wlan_password";
24+
const char* host = "nextion";
2625

27-
// Name for updatefile (no need to change, used only internally)
28-
String updateFileName = "/update.tft";
26+
// used only internally
27+
int fileSize = 0;
28+
bool result = true;
29+
30+
// init Nextion object
31+
ESPNexUpload nextion(115200);
2932

3033
#if defined ESP8266
3134
ESP8266WebServer server(80);
3235
#elif defined ESP32
3336
WebServer server(80);
3437
#endif
3538

36-
//holds the current upload
37-
File fsUploadFile;
3839

3940
String getContentType(String filename){
4041
if(server.hasArg("download")) return "application/octet-stream";
@@ -53,7 +54,8 @@ String getContentType(String filename){
5354
return "text/plain";
5455
}
5556

56-
bool handleFileRead(String path) { // send the right file to the client (if it exists)
57+
58+
bool handleFileRead(String path){ // send the right file to the client (if it exists)
5759
Serial.println("handleFileRead: " + path);
5860
if (path.endsWith("/")) path += "index.html"; // If a folder is requested, send the index file
5961
String contentType = getContentType(path); // Get the MIME type
@@ -71,55 +73,67 @@ bool handleFileRead(String path) { // send the right file to the client (if it e
7173
return false;
7274
}
7375

74-
void handleFileUpload(){ // upload a new file to the SPIFFS
76+
77+
// handle the file uploads
78+
void handleFileUpload(){
7579
HTTPUpload& upload = server.upload();
7680

7781
// Check if file seems valid nextion tft file
7882
if(!upload.filename.endsWith(".tft")){
7983
return server.send(500, "text/plain", "ONLY TFT FILES ALLOWED\n");
8084
}
85+
86+
if(!result){
87+
// Redirect the client to the failure page
88+
server.sendHeader("Location","/failure.html?reason=" + nextion.statusMessage);
89+
server.send(303);
90+
return false;
91+
}
92+
93+
Serial.println("\nConnect to Nextion display");
8194

95+
8296
if(upload.status == UPLOAD_FILE_START){
83-
Serial.print("handleFileUpload Name: ");
84-
Serial.println(updateFileName);
97+
98+
// Prepair the Nextion display by seting up serial and telling it the file size to expect
99+
result = nextion.prepairUpload(fileSize);
85100

86-
fsUploadFile = SPIFFS.open(updateFileName, "w"); // Open the file for writing in SPIFFS (create if it doesn't exist)
87-
} else if(upload.status == UPLOAD_FILE_WRITE){
88-
if(fsUploadFile)
89-
fsUploadFile.write(upload.buf, upload.currentSize); // Write the received bytes to the file
90-
} else if(upload.status == UPLOAD_FILE_END){
91-
if(fsUploadFile) { // If the file was successfully created
92-
fsUploadFile.close(); // Close the file again
93-
Serial.print("handleFileUpload Size: ");
94-
Serial.println(upload.totalSize);
95-
96-
Serial.println("Sending file to display");
97-
fsUploadFile = SPIFFS.open(updateFileName, "r"); // open for reading
98-
updateNextion(); // update nextion display
99-
fsUploadFile.close(); // Close the file again
100-
} else {
101-
server.send(500, "text/plain", "500: couldn't create file");
101+
if(result){
102+
Serial.print("Start upload. File size is: ");
103+
Serial.print(fileSize);
104+
Serial.println(" bytes");
105+
}else{
106+
Serial.println(nextion.statusMessage + "\n");
107+
return false;
102108
}
103-
}
104-
}
109+
110+
}else if(upload.status == UPLOAD_FILE_WRITE){
105111

106-
void updateNextion() {
107-
ESPNexUpload nextion(fsUploadFile, fsUploadFile.size(), 115200);
112+
// Write the received bytes to the nextion
113+
result = nextion.upload(upload.buf, upload.currentSize);
114+
115+
if(result){
116+
Serial.print(".");
117+
}else{
118+
Serial.println(nextion.statusMessage + "\n");
119+
return false;
120+
}
108121

109-
if(nextion.upload()) {
110-
// Redirect the client to the success page
111-
server.sendHeader("Location","/success.html");
112-
server.send(303);
113-
} else {
114-
// Redirect the client to the success page
115-
server.sendHeader("Location","/failure.html?reason=" + nextion.statusMessage);
116-
server.send(303);
122+
}else if(upload.status == UPLOAD_FILE_END){
123+
124+
// End the serial connection to the Nextion and softrest it
125+
nextion.end();
126+
127+
Serial.println("");
128+
//Serial.println(nextion.statusMessage);
129+
return true;
117130
}
118131
}
119132

133+
120134
void setup(void){
121135
Serial.begin(115200);
122-
Serial.print("\n");
136+
Serial.println("");
123137

124138
Serial.setDebugOutput(true);
125139
if(!SPIFFS.begin()){
@@ -137,8 +151,7 @@ void setup(void){
137151
delay(500);
138152
Serial.print(".");
139153
}
140-
Serial.println("");
141-
Serial.print("Connected! IP address: ");
154+
Serial.print("\nConnected! IP address: ");
142155
Serial.println(WiFi.localIP());
143156

144157
MDNS.begin(host);
@@ -147,11 +160,24 @@ void setup(void){
147160
Serial.println(".local");
148161

149162
//SERVER INIT
150-
server.on("/", HTTP_POST, // if the client posts to the upload page
151-
[](){ server.send(200); }, // Send status 200 (OK) to tell the client we are ready to receive
152-
handleFileUpload // Receive and save the file
163+
server.on("/", HTTP_POST, [](){
164+
Serial.println("Succesfull upload\n");
165+
166+
// Redirect the client to the success page after handeling the file upload
167+
server.sendHeader("Location","/success.html");
168+
server.send(303);
169+
return true;
170+
},
171+
// Receive and save the file
172+
handleFileUpload
153173
);
154174

175+
// receive fileSize once a file is selected (Workaround as the file content-length is of by +/- 200 bytes. Known issue: https://github.com/esp8266/Arduino/issues/3787)
176+
server.on("/fs", HTTP_POST, [](){
177+
fileSize = server.arg("fileSize").toInt();
178+
server.send(200, "text/plain", "");
179+
});
180+
155181
//called when the url is not defined here
156182
//use it to load content from SPIFFS
157183
server.onNotFound([](){
@@ -160,7 +186,7 @@ void setup(void){
160186
});
161187

162188
server.begin();
163-
Serial.println("HTTP server started");
189+
Serial.println("\nHTTP server started");
164190

165191
}
166192

0 commit comments

Comments
 (0)