-->
Page 1 of 1

Speed Issues with page loading in Firefox with SD Webserver

PostPosted: Wed Sep 27, 2017 2:21 am
by TimoFPV
Hi I'm have speed issues with my SD Webserver. The loading of an html page with css takes about 10 seconds with Firefox - with my Iphone Safari it goes way faster.
It's kinda the Windows Browser is waiting for more Data and stucks while loading.

Here is the sample Code:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <SPI.h>
#include <SD.h>
#include <mcp_can.h>

#define DBG_OUTPUT_PORT Serial

const char *ssid = "Heisenberg";
const char *password = "Heisenberg";
IPAddress ip(192, 168, 0, 1);

static bool hasSD = false;

ESP8266WebServer server(80);

MCP_CAN CAN0(D2);

void setup() {
//Serielle Schnittstelle einstellen
Serial.begin(115200);
Serial.println("Serielle Schnittstelle initialisiert");
// WiFi einstellen
WiFi.mode(WIFI_AP);
// WiFi.mode(WIFI_AP_STA);
WiFi.softAP(ssid,password);
WiFi.softAPConfig(ip, ip, IPAddress(255, 0, 0, 0));
WiFi.setPhyMode(WIFI_PHY_MODE_11N);;
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server initialisiert");

//SD-Card einstellen
if (SD.begin(SS)){
DBG_OUTPUT_PORT.println("SD Card initialisiert");
hasSD = true;
}
//CAN-Schnittstelle einstellen
if(CAN0.begin(CAN_500KBPS,MCP_8MHz) == CAN_OK) Serial.println("CAN initialisiert");
else Serial.println("CAN Fehler");
}

void loop() {
server.handleClient();
}

bool loadFromSdCard(String path){
String dataType = "text/plain";
if(path.endsWith("/")) path += "index.htm";

if(path.endsWith(".src")) path = path.substring(0, path.lastIndexOf("."));
else if(path.endsWith(".htm")) dataType = "text/html";
else if(path.endsWith(".css")) dataType = "text/css";
else if(path.endsWith(".js")) dataType = "application/javascript";
else if(path.endsWith(".png")) dataType = "image/png";
else if(path.endsWith(".gif")) dataType = "image/gif";
else if(path.endsWith(".jpg")) dataType = "image/jpeg";
else if(path.endsWith(".ico")) dataType = "image/x-icon";
else if(path.endsWith(".xml")) dataType = "text/xml";
else if(path.endsWith(".pdf")) dataType = "application/pdf";
else if(path.endsWith(".zip")) dataType = "application/zip";

File dataFile = SD.open(path.c_str());
if(dataFile.isDirectory()){
path += "/index.htm";
dataType = "text/html";
dataFile = SD.open(path.c_str());
}

if (!dataFile)
return false;

if (server.hasArg("download")) dataType = "application/octet-stream";

if (server.streamFile(dataFile, dataType) != dataFile.size()) {
DBG_OUTPUT_PORT.println("Sent less data than expected!");
}

dataFile.close();
return true;
}

void handleNotFound(){

// Send CAN message from POST
int16_t ID = strtol(server.arg("ID").c_str(), NULL, 16);
if(ID > 1)
{
unsigned char DATA[8] = {0, 1, 2, 3, 4, 5, 6, 7};
int8_t ML = strtol(server.arg("ML").c_str(), NULL, 16);
DATA[0] = strtol(server.arg("D1").c_str(), NULL, 16);
DATA[1] = strtol(server.arg("D2").c_str(), NULL, 16);
DATA[2] = strtol(server.arg("D3").c_str(), NULL, 16);
DATA[3] = strtol(server.arg("D4").c_str(), NULL, 16);
DATA[4] = strtol(server.arg("D5").c_str(), NULL, 16);
DATA[5] = strtol(server.arg("D6").c_str(), NULL, 16);
DATA[6] = strtol(server.arg("D7").c_str(), NULL, 16);
DATA[7] = strtol(server.arg("D8").c_str(), NULL, 16);
CAN0.sendMsgBuf(ID, 0, ML, DATA);
}

if(hasSD && loadFromSdCard(server.uri())) return;
String message = "SDCARD Not Detected\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i=0; i<server.args(); i++){
message += " NAME:"+server.argName(i) + "\n VALUE:" + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
DBG_OUTPUT_PORT.print(message);
}

Re: Speed Issues with page loading in Firefox with SD Webser

PostPosted: Fri Sep 29, 2017 11:48 am
by QuickFix
From the back of my head (and just an idea): the WiFi-stack of the ESP can only handle 5 or so connections at a time.
When there's a max + 1 request, I can imagine something like this might occur.

Re: Speed Issues with page loading in Firefox with SD Webser

PostPosted: Fri Sep 29, 2017 12:14 pm
by rudy
QuickFix wrote:From the back of my head (and just an idea): the WiFi-stack of the ESP can only handle 5 or so connections at a time.
When there's a max + 1 request, I can imagine something like this might occur.

Browsers will look at the files it needs to load and ask for them and expect the server should be able to handle it. The ESP8266 can not process many, and then you get delays in response.

If this is what the problem is then some options are to reduce the number of files that are needed to load. This can be done by embedding CSS and JavaScript into the HTML file. Another option is to write the server code so that requested files need to be loaded serially. Requiring the current file to be loaded before additional requests to the server are made.