Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By NardJ
#29835 I am trying to show info from an esp8266-01 in a browser at a high refresh rate. I use ajax/xmlhttp to request the info (only 4 bytes). However I am not able to get more than 10 refreshes per second. I need at least 16. Am I asking to much from a small esp8266 module? I really hope I am doing something wrong and there is a fix for this.

The code I use:
Code: Select all//XMLHTTP.open using GET seems 2x faster than POST
//xmlhttp.open('POST','/Refresh',true); 5fps
//xmlhttp.open('GET','/Refresh',true); 11fps
//xmlhttp.open('GET','/Refresh',false); (without handler) 10 fps
   
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

const char* ssid = "....";
const char* password = "....";

ESP8266WebServer server(80);

const char html[] PROGMEM =
  "<html><head>\n"
  "<meta name='viewport' content='width=device-width, initial-scale=1' />\n"
  "<meta http-equiv='Cache-Control' content='no-cache, no-store, must-revalidate' />\n"
  "<meta http-equiv='Pragma' content='no-cache' />\n"
  "<meta http-equiv='Expires' content='0' />\n"
  "<script src='script.js'></script>\n"
  "</head>\n"
  "<body>\n<h1 style='font-family:Lucida Console;font-size:32px'>Data request per second</h1>\n"
  "<div id='fps' style='background-color:#FFFFAA;border:1px solid black;width:42px;height:24px;text-align:center;line-height:22px;font-size:12px;'>10 fps</div>"
  "</body></html>\n";

const char js[] PROGMEM =   
    "var interval;\n"
    "var xmlhttp=new XMLHttpRequest();\n"   
    "document.addEventListener('DOMContentLoaded',init,false);\n"
    "var fC=0;\n"
    "setInterval(updateFPS,1000);\n"
    "function updateFPS(){\n"
    "  var fT = document.getElementById('fps');\n"
    "  fT.innerHTML=fC+ ' fps';\n"
    "  fC=0;"
    "}\n"   
    "function init(){\n"   
    "  refresh();\n"
    "}\n"
    "function refresh( ){\n"
    "  xmlhttp.onreadystatechange=function(){\n"
    "    if (xmlhttp.readyState==4 && xmlhttp.status==200){\n"
    "      fC++;\n"
    "      setTimeout(refresh,0);\n"
    "    }\n" //if
    "  }\n" //onreadystatechange
    "  xmlhttp.open('GET','/Refresh',true);\n"
    "  xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');\n"
    "  xmlhttp.send();\n"
    "}\n"
    ;

void handleNotFound(){
  String message = "File Not Found: ";
  server.send(404, "text/plain", message);
  message += "'"+server.uri()+"'";
  Serial.println(message);
}

char msg[4]={'T','E','S','T'};
void Refresh(){
  wdt_disable();
  server.send(200, "text/plain", msg);
  wdt_enable(5000);
}
void setup () {
  delay(1000);
  Serial.begin (115200);
  Serial.println("Setup Start");
}

unsigned long cycles=0;

void initWifi(){
  // Start Wifi
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.print("  ip-address: ");
  Serial.println(WiFi.localIP());
}

void initServer(){
  // set comm. pages   
  server.on ( "/",[]() {
                    Serial.println("Root page called");
                    server.send_P ( 200, "text/html", html);
                   });
  server.on ( "/script.js",[]() {
                    Serial.println("Javascript loaded");
                    server.send_P ( 200, "text/plain", js);
                   });             
  server.on ( "/Refresh", Refresh);

  server.onNotFound(handleNotFound);
 
  // Start the server 
  server.begin();
}

int fase=0;
void loop () {
  if (fase!=2) {Serial.print ("Start boot fase ");Serial.println(fase);}
  if (fase==0) {initWifi();}
  if (fase==1) {initServer();}
  if (fase!=2) {Serial.print ("..finished boot fase ");Serial.println(fase);}
  if (fase!=2) {fase++;return;}
 
  server.handleClient();

}
Last edited by NardJ on Fri Oct 02, 2015 4:21 am, edited 1 time in total.
User avatar
By igrr
#30344 Perhaps you could do some profiling to identify what is taking too much time?
Is the client closing connection at once? Perhaps incorrect content-length is being set, which causes the client to wait longer.
Take a wireshark/tcpdump trace, and measure how much does request handling take on the ESP side.
Chrome developer console can show you how much each phase of HTTP request took as well.

On a separate note, i'm not sure why this works at all:
Code: Select allchar msg[4]={'T','E','S','T'};
void Refresh(){
  wdt_disable();
  server.send(200, "text/plain", msg);
  wdt_enable(5000);


How do you suppose the server.send function will figure out you want it to send 4 characters when you don't zero terminate the string? Also, what's the reason for those calls to wdt_disable/enable? They do not work anyway since SDK 0.9.5...
User avatar
By NardJ
#30363 Thx for your reply. I removed wdt_disable/enable and terminated the string
Code: Select allchar msg[5]={'T','E','S','T','\0'};


Following snapshot of chrome developer tools shows most time is spend in downloading. However if I increase the message to 257 bytes the same time is spend downloading. This makes me believe the esp8266 takes some time accepting the connection.

xhr profile.png


Any ideas how to analyze this problem further?
You do not have the required permissions to view the files attached to this post.