Moderator: igrr
#include <ESP8266WiFi.h>
const char* ssid = "...";
const char* password = "...";
WiFiServer 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:112px;height:48px;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 <br>'+aB;\n"
" fC=0;"
"}\n"
"function init(){\n"
" refresh();\n"
"}\n"
"var aB='';\n"
"function refresh( ){\n"
" xmlhttp.onreadystatechange=function(){\n"
" if (xmlhttp.readyState==4 && xmlhttp.status==200){\n"
" fC++;\n"
" aB=xmlhttp.response;\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 setup() {
Serial.begin(115200);
delay(10);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
}
char msg[]={'T','E','S','T','\0'};
char* Refresh(){
return msg;
}
WiFiClient client;
void loop() {
// Check if a client has connected
client = server.available();
if (!client) return;
Serial.print("Client found: ");
Serial.println(client);
// Wait until the client sends some data
while(!client.available()){
delay(1);
}
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println("url: "+req);
client.flush();
// Match the request
int val;
String ret="";
if (req.equals("GET / HTTP/1.1")) ret=html;
if (req.equals("GET /script.js HTTP/1.1")) ret=js;
if (req.equals("GET /Refresh HTTP/1.1")) ret=Refresh();
if (ret.equals("")){
Serial.println("invalid request");
ret="invalid request";
client.stop();
return;
}
client.flush();
// Prepare the response
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";//<!DOCTYPE HTML>\r\n";
s=s+ret+"\n";
// Send the response to the client
client.print(s);
//delay(1);
//Serial.println("Client disconnected");
// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}
Pinging results in output below, so no network delay.
Pinging 192.168.0.7 with 32 bytes of data:
Reply from 192.168.0.7: bytes=32 time=5ms TTL=254
Reply from 192.168.0.7: bytes=32 time=11ms TTL=254
Reply from 192.168.0.7: bytes=32 time=4ms TTL=254
Reply from 192.168.0.7: bytes=32 time=4ms TTL=254
Ping statistics for 192.168.0.7:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 4ms, Maximum = 11ms, Average = 6ms
I think the esp8266 is not capable of responding fast enough....or should I use another type of connection instead of xmlhttp to periodically resend updated info to a browser?
NardJ wrote:Am I asking to much from a small esp8266 module?
In a word, yes.
If a high data call rate is essential, maybe you should consider one of the streaming protocols?