Hello, i am meeting some problems with my esp8266: whenever i try to write or read a .txt file it crashes
and wipe all the file that are present in the flash, like html file etc.
however i can read perfectly the html file whenever i need it.
what could it be?
code:
#include <FS.h>
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <PString.h>
#include <Streaming.h>
String messaggio;
const IPAddress apIp(192, 168, 0, 1);
DNSServer dnsServer;
ESP8266WebServer server(80);
.
.
.
File logfile;
char logbuf[256];
void httpDefault()
{
logline << server.client().remoteIP() << " redirect";
server.sendHeader("Location", "http://freewifi.lan/", true);
server.send(302, "text/plain", "");
server.client().stop();
}
void httpHome(){
if (server.hostHeader() != String("freewifi.lan")) {
return httpDefault();
}
logline << server.client().remoteIP() << " home";
File file = SPIFFS.open("/index.htm.gz", "r");
server.streamFile(file, "text/html");
file.close();
}
void httpChat() {
logfile = SPIFFS.open("/log.txt", "w");
if (server.args() > 0) {
for (int i = 0; i < server.args(); i++) {
messaggio = server.arg(i);
logfile.print(messaggio);
logfile.close();
char copy[50];
messaggio.toCharArray(copy, 32);
Serial.println(copy);
}
httpHome();
} else {
httpHome();
}
}
void httpLog(){
logfile = SPIFFS.open("/log.txt", "r");
server.streamFile(logfile, "text/plain");
logfile.close();
}
void setup()
{
Serial.begin(115200);
Serial.println();
SPIFFS.begin();
WiFi.persistent(false);
WiFi.disconnect(true);
wifiStaConnectHandler = WiFi.onSoftAPModeStationConnected(wifiStaConnect);
wifiStaDisconnectHandler = WiFi.onSoftAPModeStationDisconnected(wifiStaDisconnect);
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIp, apIp, IPAddress(255, 255, 255, 0));
WiFi.softAP("MT_FREE", nullptr, 1); //const network name wich will be changed after recieving data from serial input
dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
dnsServer.start(53, "*", apIp);
server.on("/", httpHome);
server.on("/chat", httpChat);
server.on("/log", httpLog);
server.onNotFound(httpDefault);
server.begin();
Serial << "ready" << endl;
}
.
.
.
The part in red is giving me problem.
any help?