ESP arduino webserve openfing file for writing during upload
Posted: Wed Feb 08, 2017 2:01 pm
Hello.
I have simple ESP arduino question .
I have web server and handle POST function.
I just want to save file sent by POST to SPIFFS.
When upload starts I try to open fiel for wrirting fsUploadFileconf= SPIFFS.open(CONF_FILE, "w");).
It generates exception.
Funny thing but openning for read works fine fsUploadFileconf= SPIFFS.open(CONF_FILE, "r");
My code below:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include "FS.h"
const char* ssid = "xxxxxxxxxxxxxxxxx";
const char* password = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
ESP8266WebServer httpServer(80);
#define CONF_FILE "/conf/conf.props"
File fsUploadFileconf;
void handleConfUpload()
{
Serial.printf("handleConfUpload :%s\n",httpServer.uri().c_str());
// handler for the file upload, get's the sketch bytes, and writes
// them through the Update object
HTTPUpload upload = httpServer.upload();
if(upload.status == UPLOAD_FILE_START){
Serial.printf("UPLOAD_FILE_START\n");
Serial.printf("uri: %s \n",httpServer.uri().c_str());
fsUploadFileconf= SPIFFS.open(CONF_FILE, "w");
if(!fsUploadFileconf)
{
Serial.printf("open file failed %s\n" ,CONF_FILE);
return;
}
} else if(upload.status == UPLOAD_FILE_WRITE){
Serial.printf("UPLOAD_FILE_WRITE\n");
Serial.printf("Writing upload.currentSize %d \n",upload.currentSize );
Serial.printf("Writing buf %s \n",upload.buf);
fsUploadFileconf.write((const uint8_t*)upload.buf, 3);
//holds the current upload
fsUploadFileconf.write(upload.buf, upload.currentSize);
} else if(upload.status == UPLOAD_FILE_END){
Serial.printf("UPLOAD_FILE_END\n" );
fsUploadFileconf.close();
} else if(upload.status == UPLOAD_FILE_ABORTED){
Serial.println("UPLOAD_FILE_ABORTED");
}
delay(0);
}
void handleConfUploadAfterFinished()
{
Serial.println("handleConfUploadAfterFinished");
httpServer.sendHeader("Location","/");
httpServer.sendHeader("Cache-Control","no-cache");
httpServer.send(302);
}
void handleRoot() {
httpServer.send(200, "text/plain", "hello from esp8266!");
}
void setup(void){
SPIFFS.begin();
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
httpServer.on("/", handleRoot);
httpServer.on("/inline", [](){
httpServer.send(200, "text/plain", "this works as well");
});
httpServer.on("/confupload", HTTP_POST, handleConfUploadAfterFinished, handleConfUpload);
httpServer.begin();
Serial.println("HTTP server started");
}
void loop(void){
httpServer.handleClient();
}
Serial output:
...
UPLOAD_FILE_START
uri: /confupload
Exception (2):
epc1=0x3ffef7e0 epc2=0x00000000 epc3=0x00000000 excvaddr=0x3ffef7e0 depc=0x00000000
ctx: sys
sp: 3ffef630 end: 3fffffb0 offset: 01a0
>>>stack>>>
3ffef7d0: 000c3a05 000c3a04 00000001 4020ced8
3ffef7e0: ffffff7e 3ffef820 00000000 00000004
3ffef7f0: 3fffc718 3ffef820 00000000 3ffef874
3ffef800: 4020cfb5 4020cfaa 000c7804 00000004
3ffef810: 000c7805 000c7804 00000001 4020ced8
Can you help me and tell why I can not open file for writig during file upload ?
I have simple ESP arduino question .
I have web server and handle POST function.
I just want to save file sent by POST to SPIFFS.
When upload starts I try to open fiel for wrirting fsUploadFileconf= SPIFFS.open(CONF_FILE, "w");).
It generates exception.
Funny thing but openning for read works fine fsUploadFileconf= SPIFFS.open(CONF_FILE, "r");
My code below:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include "FS.h"
const char* ssid = "xxxxxxxxxxxxxxxxx";
const char* password = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
ESP8266WebServer httpServer(80);
#define CONF_FILE "/conf/conf.props"
File fsUploadFileconf;
void handleConfUpload()
{
Serial.printf("handleConfUpload :%s\n",httpServer.uri().c_str());
// handler for the file upload, get's the sketch bytes, and writes
// them through the Update object
HTTPUpload upload = httpServer.upload();
if(upload.status == UPLOAD_FILE_START){
Serial.printf("UPLOAD_FILE_START\n");
Serial.printf("uri: %s \n",httpServer.uri().c_str());
fsUploadFileconf= SPIFFS.open(CONF_FILE, "w");
if(!fsUploadFileconf)
{
Serial.printf("open file failed %s\n" ,CONF_FILE);
return;
}
} else if(upload.status == UPLOAD_FILE_WRITE){
Serial.printf("UPLOAD_FILE_WRITE\n");
Serial.printf("Writing upload.currentSize %d \n",upload.currentSize );
Serial.printf("Writing buf %s \n",upload.buf);
fsUploadFileconf.write((const uint8_t*)upload.buf, 3);
//holds the current upload
fsUploadFileconf.write(upload.buf, upload.currentSize);
} else if(upload.status == UPLOAD_FILE_END){
Serial.printf("UPLOAD_FILE_END\n" );
fsUploadFileconf.close();
} else if(upload.status == UPLOAD_FILE_ABORTED){
Serial.println("UPLOAD_FILE_ABORTED");
}
delay(0);
}
void handleConfUploadAfterFinished()
{
Serial.println("handleConfUploadAfterFinished");
httpServer.sendHeader("Location","/");
httpServer.sendHeader("Cache-Control","no-cache");
httpServer.send(302);
}
void handleRoot() {
httpServer.send(200, "text/plain", "hello from esp8266!");
}
void setup(void){
SPIFFS.begin();
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
httpServer.on("/", handleRoot);
httpServer.on("/inline", [](){
httpServer.send(200, "text/plain", "this works as well");
});
httpServer.on("/confupload", HTTP_POST, handleConfUploadAfterFinished, handleConfUpload);
httpServer.begin();
Serial.println("HTTP server started");
}
void loop(void){
httpServer.handleClient();
}
Serial output:
...
UPLOAD_FILE_START
uri: /confupload
Exception (2):
epc1=0x3ffef7e0 epc2=0x00000000 epc3=0x00000000 excvaddr=0x3ffef7e0 depc=0x00000000
ctx: sys
sp: 3ffef630 end: 3fffffb0 offset: 01a0
>>>stack>>>
3ffef7d0: 000c3a05 000c3a04 00000001 4020ced8
3ffef7e0: ffffff7e 3ffef820 00000000 00000004
3ffef7f0: 3fffc718 3ffef820 00000000 3ffef874
3ffef800: 4020cfb5 4020cfaa 000c7804 00000004
3ffef810: 000c7805 000c7804 00000001 4020ced8
Can you help me and tell why I can not open file for writig during file upload ?