I am having issues with my ESP8266-12e crashing when a client requests '/Logs' on a webserver. My intent is to send the client the last 20 lines from a Log.csv file embedded in , and each line is 22 bytes in length not including the carriage return and linefeed.
#include <SPI.h>
#include "SdFat.h"
#include <ESP8266WiFi.h>
const char* ssid = "XXX";
const char* password = "XXX";
const int WiFiTimeout = 5;
WiFiServer server(85);
unsigned long currentTime = millis();
unsigned long previousTime = 0;
const long timeoutTime = 2000; //server timeout 2s
using namespace sdfat;
SdFat sd;
const uint8_t chipSelect = SS;
SdFile file;
void setup() {
server.begin();
if (!sd.begin(chipSelect)) {
sd.initErrorHalt();
}
if (!file.open("Log.csv", O_WRONLY | O_CREAT)) {
file.println("Date,Time,Event");
file.close();
}
}
void loop() {
WiFiClient client = server.available();
if (client) {
String currentLine = "";
String header = "";
currentTime = millis();
previousTime = currentTime;
while (client.connected() && currentTime - previousTime <= timeoutTime) {
currentTime = millis();
if (client.available()) {
char c = client.read();
header += c;
if (c == '\n') {
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Content-Language: en-US");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE html><html>");
client.println("<body><ul>");
if (header.indexOf("GET /Logs HTTP/1.1") >= 0) {
client.println("<h1>Log Page</h1><hr>");
client.println("<table>");
client.println("<tr><th><b>Date</b></th><th><b>Time</b></th><th><b>Event</b></th></tr>");
/*
file.open("Log.csv", FILE_READ);
int pointer = file.fileSize() - 20 * 24;
file.seekSet(pointer);
for (int i = 1; i <= 20; i++) {
char c[] = "";
for (int j = 0;j < 22; j++) {
c[j] = file.read();
file.seekSet(pointer + j);
}
c[22] = 0;
client.print("</tr></td>");
client.print(c);
client.println("</tr></td>");
pointer += 24;
file.seekSet(pointer);
}
file.close();
*/
client.println("</table>
");
}
client.println("</body></html>");
client.println();
break;
}
else {
currentLine = "";
}
}
else if (c != '\r') {
currentLine += c;
}
}
}
header = "";
currentLine = "";
client.stop(); //crashes here
}
}
If I run the code above code fully un-commented, I see the lines of interest from Log.csv appear in a browser but the ESP8266 crashes and resets (error codes below). The connection is not closed by the server, so the browser eventually times out the connection and closes. I have debugged this event and the furthest downstream line that gets executed is currentLine = "", and it is crashing when client.stop() is called.
Another curious thing I've noticed is that if I request other pages (omitted these to save space) such as '/' or '/About' with the GET /Logs section un-commented, the server doesn't crash but is much slower to load and occasionally times out. This is puzzling to me because that section of the code isn't even called when a client requests another page.
23:38:11.929 -> Exception (3):
23:38:11.929 -> epc1=0x40100715 epc2=0x00000000 epc3=0x00000000 excvaddr=0x40043330 depc=0x00000000
23:38:11.929 ->
23:38:11.929 -> >>>stack>>>
23:38:11.929 ->
23:38:11.929 -> ctx: cont
23:38:11.929 -> sp: 3ffffd20 end: 3fffffc0 offset: 01a0
23:38:11.929 -> 3ffffec0: 000b55ae 000003ad 3fff1fdc 00000000
23:38:11.929 -> 3ffffed0: 00000016 3fff0b18 00000020 401009bf
23:38:11.929 -> 3ffffee0: 4020547c 3fff1dbc 00000000 402131a5
23:38:11.964 -> 3ffffef0: 00000016 3fff0b18 3fffff50 40208f9a
23:38:11.964 -> 3fffff00: 3ffe8d50 00000001 000194a8 40208fb0
23:38:11.964 -> 3fffff10: 00000016 3fff0b18 000194a8 40201f37
23:38:11.964 -> 3fffff20: 4020bb70 00000000 00001388 40208c00
23:38:11.964 -> 3fffff30: 00000000 3fff1dbc 3fff2400 0018001f
23:38:11.964 -> 3fffff40: 80006d70 3fff2000 000c000f 80303230
23:38:11.964 -> 3fffff50: 322c3332 00002c35 0a00322c 3fff299c
23:38:11.964 -> 3fffff60: 0000008f 0a002073 3ffe8600 40200001
23:38:11.999 -> 3fffff70: 000194a8 0000000a 00000002 00000000
23:38:11.999 -> 3fffff80: 00000000 00000016 00000001 4010017c
23:38:11.999 -> 3fffff90: 3fffdad0 00000000 3fff0fa0 3fff0fe0
23:38:11.999 -> 3fffffa0: 3fffdad0 00000000 3fff0fa0 40209c14
23:38:11.999 -> 3fffffb0: feefeffe feefeffe 3ffe8508 40100c81
23:38:11.999 -> <<<stack<<<
23:38:12.034 ->
23:38:12.034 -> ets Jan 8 2013,rst cause:2, boot mode:(3,6)
23:38:12.034 ->
23:38:12.034 -> load 0x4010f000, len 1392, room 16
23:38:12.034 -> tail 0
23:38:12.034 -> chksum 0xd0
23:38:12.034 -> csum 0xd0
23:38:12.034 -> v3d128e5c
23:38:12.034 -> ~ld
I realize that using Strings is bad practice and can lead to memory leaks and I am in the process of learning how to substitute all Strings for char arrays in my code. But this problem seems to be only attributed to using the SdFat commands and disappears when not using that section of code.