harlequin-tech / WiFlyHQ

WiFly RN-XV Arduino Library
Other
110 stars 68 forks source link

ttfb is too high #46

Closed isaccjr closed 8 years ago

isaccjr commented 8 years ago

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() {

pinMode(3, OUTPUT); // DEFINICAO SAIDA PARA O LED
Serial.begin(115200);

// Serial.begin(9600); Serial.println(F("Starting")); Serial.print(F("Free memory: ")); Serial.println(wifly.getFreeMemory(),DEC);

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.setBroadcastInterval(0);  // Turn off UPD broadcast

//wifly.terminal();

Serial.print(F("MAC: "));
Serial.println(wifly.getMAC(buf, sizeof(buf)));
Serial.print(F("IP: "));
Serial.println(wifly.getIP(buf, sizeof(buf)));

wifly.setDeviceID("Wifly-WebServer");

if (wifly.isConnected()) {
    Serial.println(F("Old connection active. Closing"));

wifly.close(); }

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

")); wifly.sendChunkln (F("
")); wifly.sendChunkln(F("")); wifly.sendChunkln(F("
")); wifly.sendChunkln(F("")); wifly.sendChunkln(F("")); wifly.sendChunkln(); }

/* Send a 404 error / void send404() { wifly.println(F("HTTP/1.1 404 Not Found")); wifly.println(F("Content-Type: text/html")); wifly.println(F("Transfer-Encoding: chunked")); wifly.println(); wifly.sendChunkln(F("")); wifly.sendChunkln(F("404 nao encontrada !!!")); wifly.sendChunkln(F("")); wifly.sendChunkln(F("

Not Found

")); wifly.sendChunkln(F("
")); wifly.sendChunkln(F("")); wifly.sendChunkln(); }

//mostra valores void mostra_valores() { 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();

    snprintf_P(buf, sizeof(buf), PSTR("<center><p>VALOR DO POTENCIOMETRO DO PINO 5=%d</p></center>"), analogRead(A5));
    wifly.sendChunkln(buf);

    snprintf_P(buf, sizeof(buf), PSTR("<center><p>VALOR DO POTENCIOMETRO DO PINO 4=%d</p></center>"), analogRead(A4));
    wifly.sendChunkln(buf);

    snprintf_P(buf, sizeof(buf), PSTR("<center><p>VALOR DA TEMP PINO 3=%d</p></center>"), analogRead(A3));
    wifly.sendChunkln(buf);

    snprintf_P(buf, sizeof(buf), PSTR("<center><p>LUMINOSIDADE PINO 2=%d</center></p>"), analogRead(A2));
    wifly.sendChunkln(buf);

    if ( digitalRead(8))
    {
      wifly.sendChunk("BOTÃO 1= LIGADO<BR>");
    }
    else 
    {
      wifly.sendChunk("BOTÃO 1= DESLIGADO<BR>");
      Serial.print(F("75."));
    }
    if ( digitalRead(7)) 
    {
      wifly.sendChunk("BOTÃO 2= LIGADO<BR>");
      Serial.print(F("85."));
    }
    else 
    {
      wifly.sendChunk("BOTÃO 2= DESLIGADO<BR>");
    }

    wifly.sendChunkln(); // finaliza comunicação html

}

harlequin-tech commented 8 years ago

Problem is not related to the WiFlyHQ library, general programming issue.