i'm trying to make a server to read the analogs e digital ports of the arduino. it all ready works, but the ttfb is too high. i'm getting 1,4 seconds per request and I don't know what to do to make it goes faster. I need less than 1s this 400 ms is really boring me up.
wifiSerial.begin(9600);
if (!wifly.begin(&wifiSerial, &Serial)) {
Serial.println(F("Failed to start wifly"));
wifly.terminal();
}
/* Join wifi network if not already associated */
if (!wifly.isAssociated()) {
/* Setup the WiFly to connect to a wifi network */
Serial.println(F("Joining network"));
wifly.setSSID(mySSID);
wifly.setPassphrase(myPassword);
wifly.enableDHCP();
wifly.save();
if (wifly.join()) {
Serial.println(F("Joined wifi network"));
} else {
Serial.println(F("Failed to join wifi network"));
wifly.terminal();
}
} else {
Serial.println(F("Already joined network"));
}
wifly.setProtocol(WIFLY_PROTOCOL_TCP);
if (wifly.getPort() != 80) {
wifly.setPort(80);
/* local port does not take effect until the WiFly has rebooted (2.32) */
wifly.save();
Serial.println(F("Set port to 80, rebooting to make it work"));
wifly.reboot();
delay(3000);
}
Serial.println(F("Ready"));
}
void loop()
{
if (wifly.available() > 0)
{
/* See if there is a request */
if (wifly.gets(buf, sizeof(buf)))
{ //se o wifly recebe alguma coisa
if (strncmpP(buf, PSTR("GET / "), 6) == 0)
{ // if it`s a get do the next line
/* GET request /
Serial.println(F("Got GET request"));
while (wifly.gets(buf, sizeof(buf)) > 0)
{
/_ Skip rest of request */
}
sendIndex(); //enviar a pagina principal
Serial.println(F("Sent index page"));
}
else if (strncmp_P(buf, PSTR("GET /on"), 7) == 0) //testa e o buf é igual a get/on
{ //abre bloco liga led
while (wifly.gets(buf, sizeof(buf)) > 0)
{
}
wifly.flushRx();
mostra_valores();
digitalWrite(3,HIGH);
Serial.println(F("ligando led"));
} //fecha bloco desliga led
else if (strncmp_P(buf, PSTR("GET /off"), 8) == 0) //testa e o buf é igual a get/off
{ //abre bloco desliga led
while (wifly.gets(buf, sizeof(buf)) > 0)
{
}
wifly.flushRx();
mostra_valores();
digitalWrite(3,LOW); // e buf for igual a get/off desliga o led
Serial.println(F("desligando led"));
} //fecha bloco desliga led */
else if (strncmp_P(buf, PSTR("GET /ajax_swicht"), 16) == 0) //testa e o buf é igual a get/ajax_switch
{ //abre bloco ajax
while (wifly.gets(buf, sizeof(buf)) > 0)
{
}
wifly.flushRx();
mostra_valores(); //envia o valores
Serial.println(F("enviando valores"));
} //fecha bloco ajax
else { //abre else
Serial.print(F("Unexpected: "));
Serial.println(buf);
wifly.flushRx(); // discard rest of input
Serial.println(F("Sending 404"));
send404();
} //fecha else
} //fecha o bloco de recebimento do wifly
}//fecha e o wifly eta disponivel
} //fecha o void loop
/* Send an index HTML page with an input box for a username /
void sendIndex()
{
/ Send the header direclty with print /
wifly.println(F("HTTP/1.1 200 OK"));
wifly.println(F("Content-Type: text/html"));
wifly.println(F("Transfer-Encoding: chunked"));
wifly.println(F("Connection: keep-alive"));
wifly.println("\r\n");
wifly.println();
/* Send the body using the chunked protocol so the client knows when
the message is finished.
Note: we're not simply doing a close() because in version 2.32
firmware the close() does not work for client TCP streams.
/
wifly.sendChunkln(F(""));
wifly.sendChunkln(F(""));
wifly.sendChunkln(F("engeBOT-WiFly HTTP Server Example"));
//começa script do ajax
wifly.sendChunkln(F(""));
wifly.sendChunkln(F(""));
i'm trying to make a server to read the analogs e digital ports of the arduino. it all ready works, but the ttfb is too high. i'm getting 1,4 seconds per request and I don't know what to do to make it goes faster. I need less than 1s this 400 ms is really boring me up.
here is the code
undef PROGMEM
define PROGMEM attribute(( section(".progmem.data") ))
undef PSTR
define PSTR(s) (extension({static prog_char c[] PROGMEM = (s); &c[0];}))
include
include
SoftwareSerial wifiSerial(4,5);
include //essa
String httpRequest; //led pin
define ledPin 3 //led pin
WiFly wifly;
/* Change these to match your WiFi network */ //const char mySSID[] = ""; //const char myPassword[] = "";
const char mySSID[] = ""; const char myPassword[] = "";
void sendIndex(); void send404(); void mostra_valores(); char buf[1024];
boolean ledstatus = false;
void setup() {
// Serial.begin(9600); Serial.println(F("Starting")); Serial.print(F("Free memory: ")); Serial.println(wifly.getFreeMemory(),DEC);
wifly.terminal(); }
/* Setup the WiFly to connect to a wifi network */ Serial.println(F("Joining network")); wifly.setSSID(mySSID); wifly.setPassphrase(myPassword); wifly.enableDHCP(); wifly.save();
if (wifly.join()) { Serial.println(F("Joined wifi network")); } else { Serial.println(F("Failed to join wifi network")); wifly.terminal(); } } else { Serial.println(F("Already joined network")); }
wifly.close(); }
/* local port does not take effect until the WiFly has rebooted (2.32) */ wifly.save(); Serial.println(F("Set port to 80, rebooting to make it work")); wifly.reboot(); delay(3000); } Serial.println(F("Ready")); }
void loop() { if (wifly.available() > 0) {
if (wifly.gets(buf, sizeof(buf))) { //se o wifly recebe alguma coisa if (strncmpP(buf, PSTR("GET / "), 6) == 0) { // if it`s a get do the next line /* GET request / Serial.println(F("Got GET request")); while (wifly.gets(buf, sizeof(buf)) > 0) { /_ Skip rest of request */ } sendIndex(); //enviar a pagina principal Serial.println(F("Sent index page"));
} //fecha o bloco de recebimento do wifly }//fecha e o wifly eta disponivel } //fecha o void loop
/* Send an index HTML page with an input box for a username / void sendIndex() { / Send the header direclty with print / wifly.println(F("HTTP/1.1 200 OK")); wifly.println(F("Content-Type: text/html")); wifly.println(F("Transfer-Encoding: chunked")); wifly.println(F("Connection: keep-alive")); wifly.println("\r\n"); wifly.println();
/* Send the body using the chunked protocol so the client knows when
firmware the close() does not work for client TCP streams. / wifly.sendChunkln(F("")); wifly.sendChunkln(F("")); wifly.sendChunkln(F("engeBOT-WiFly HTTP Server Example "));
//começa script do ajax
wifly.sendChunkln(F(""));
wifly.sendChunkln(F(""));
wifly.sendChunkln(F("<body onload=\"GetSwitchState('ajax_swicht')\">"));
// wifly.sendChunkln (F("
wifly.sendChunkln(F("
")); wifly.sendChunkln(F(""));
wifly.sendChunkln(F("
")); wifly.sendChunkln (F("")); wifly.sendChunkln(F("")); wifly.sendChunkln(F("Inovacao Sustentavel e Inteligente.
")); wifly.sendChunkln(F("