Pages: 1 [2]   Go Down
Author Topic: Why's SD -> Ethernet Card -> Webbrowser so slow?  (Read 6261 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can i use this system to print html commands like we do with client.print?
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 139
Posts: 5860
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes. It doesn't matter whether client or server. Any time you use a tcp protocol, this will apply.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks again!   smiley-cool
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've tested this with my code, but for some reason it didn't work like expected, and it was slower than my original code, but maybe i've made something wrong
Code:
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <Ethernet.h>
#include <Flash.h>
#include <VirtualWire.h>;
#define ledIndicadorComms 2
#define configConcl 3
#define ledRececao 7

#define BUFSIZ 128
const int pinoRececao = 8;
int leitura = 0;
float percentagem = 0;
float litros = 0;
float dados = 0;
char mensagem[5];
boolean verifica = false;
boolean verifica2 = false;
boolean Log = false;
int nr = 1;

byte mac[] = {
  0x90, 0xA2, 0xDA, 0x00, 0xEA, 0x8C };
IPAddress ip(196,168,0,3);
char rootFileName[] = "index.htm";
char ficheiroLog[] ="&niv_LOG.txt";
EthernetServer server(80);

Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
SdFile LOGfile;

#define error(s) error_P(PSTR(s))
void error_P(const char* str) {
  PgmPrint("Erro: ");
  SerialPrintln_P(str);
  if (card.errorCode()) {
    PgmPrint("Erro cartao SD: ");
    Serial.print(card.errorCode(), HEX);
    Serial.print(',');
    Serial.println(card.errorData(), HEX);
  }
  digitalWrite(configConcl, HIGH);
  while(1);
}

void setup() {

  Serial.begin(9600);
  delay(1);
  pinMode(ledIndicadorComms, OUTPUT);
  pinMode(configConcl, OUTPUT);
 
  PgmPrint("Memoria RAM Livre: ");
  Serial.println(FreeRam());  
    
  pinMode(10, OUTPUT);              
  digitalWrite(10, HIGH);              

  if (!card.init(SPI_FULL_SPEED, 4)) error("Falha no card.init!");

  if (!volume.init(&card)) error("Falha no vol.init!");

  PgmPrint("O volume e FAT");
  Serial.println(volume.fatType(),DEC);
  Serial.println();

  if (!root.openRoot(&volume)) error("Falha ao abrir raiz do cartao!");

  PgmPrintln("Ficheiros na raiz do cartao:");

  root.ls(LS_DATE | LS_SIZE);
  Serial.println();

  PgmPrintln("Ficheiros encontrados em todos os directorios do cartao:");
  root.ls(LS_R);

  Serial.println();
  PgmPrintln("Concluido");

  Log = file.open(&root, ficheiroLog, O_WRITE | O_APPEND);
  if (Log){
    file.println("Leitura  ,  Nivel Tanque  ,  Quantidade Agua");
    file.close();
    Serial.println(F("Escrita do Header no ficheiro LOG concluida!"));
  }else{
    Serial.println(F("ERRO! Nao foi possivel abrir o ficheiro LOG."));

    while(1){
      digitalWrite(configConcl, HIGH);
      delay(1000);
      digitalWrite(configConcl,LOW);
      delay(1000);
    }  
}    

  Ethernet.begin(mac, ip);
  server.begin();
  delay(2);

  pinMode(ledRececao, OUTPUT);
  vw_set_rx_pin(pinoRececao);
  vw_setup(2000);
  vw_rx_start();

  digitalWrite(configConcl, HIGH);
  delay(250);
  digitalWrite(configConcl,LOW);
  delay(250);
  digitalWrite(configConcl, HIGH);
  delay(250);
  digitalWrite(configConcl,LOW);
}


void loop() {
  char clientline[BUFSIZ];
  char *filename;
  int image = 0;
  int index = 0;
  verifica = false;
  Log = false;

uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;

  if (vw_get_message(buf, &buflen)){
    int i;
    for (i=0; i<buflen; i++){
    mensagem[i] = buf[i];
    
    if (mensagem[0] == 'R'){
      leitura = atoi(&mensagem[1]);
      percentagem = (leitura * 100)/465;
      litros = (percentagem*700000)/100;

  Serial.print(F("Nivel do Tanque:"));
  Serial.print(percentagem, 1);
  Serial.println(F("%"));          
  Serial.print(F("Quantidade de agua no Tanque:"));
  Serial.print(litros, 1);
  Serial.println(F("litros"));

    Log = file.open(&root, ficheiroLog, O_WRITE | O_APPEND);
    if (Log){
    file.print(nr);
    file.print("  ,  ");
    file.print(percentagem, 1);
    file.print("  ,  ");
    file.println(litros, 1);
    file.close();
    
    Serial.println(F("Valores gravados com sucesso!"));
    nr++;
    Log = false;
    } else {
     Serial.println(F("Nao foi possivel gravar dados no ficheiro LOG!"));
    }
  }
  memset(&mensagem, 0, sizeof(mensagem)); //APAGA A MENSAGEM RECEBIDA
  }
  }

  EthernetClient client = server.available();
  if (client) {
    boolean current_line_is_blank = true;

    index = 0;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        if (c != '\n' && c != '\r') {
          clientline[index] = c;
          index++;

          if (index >= BUFSIZ)
            index = BUFSIZ -1;
            continue;
        }

        clientline[index] = 0;
        filename = 0;

        Serial.println(clientline);

        digitalWrite(ledIndicadorComms, HIGH);
        delay(1);
        digitalWrite(ledIndicadorComms, LOW);
        delay(1);

        if (strstr(clientline, "GET / ") != 0) {
          filename = rootFileName;
          }

        if (strstr(clientline, "GET /") != 0) {

          if (strstr(clientline, "GET /iframe") != 0) {
            verifica = true;
          }
 
          if (strstr(clientline, "GET /2iframe") != 0) {
            verifica2 = true;
          }
          if(strstr(clientline, "GET/&niv") !=0){
            break;
          }
          if (!filename) filename = clientline + 5;

          (strstr(clientline, " HTTP"))[0] = 0;

          Serial.println(filename);

          if (! file.open(&root, filename, O_READ)) {
            client.println(F("HTTP/1.1 404 Not Found"));
            client.println(F("Content-Type: text/html"));
            client.println();
            client.println(F("<h2>Erro 404</h2>"));
            client.println(F("<s2>O ficheiro nao existe.<s2>"));
            client.println(F(""));
            break;
          }
          Serial.println("Ligado!");
« Last Edit: February 26, 2013, 02:56:18 pm by blastboot » Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
client.println("HTTP/1.1 200 OK");
          if (strstr(filename, ".htm") != 0)
            client.println("Content-Type: text/html");
          else if (strstr(filename, ".css") != 0)
            client.println("Content-Type: text/css");
          else if (strstr(filename, ".png") != 0)
            client.println("Content-Type: text/php");
          else if (strstr(filename, ".php") != 0)
            client.println("Content-Type: image/png");
          else if (strstr(filename, ".jpg") != 0)
            client.println("Content-Type: image/jpeg");
          else if (strstr(filename, ".gif") != 0)
            client.println("Content-Type: image/gif");
          else if (strstr(filename, ".3gp") != 0)
            client.println("Content-Type: video/mpeg");
          else if (strstr(filename, ".pdf") != 0)
            client.println("Content-Type: application/pdf");
          else if (strstr(filename, ".js") != 0)
            client.println("Content-Type: application/x-javascript");
          else if (strstr(filename, ".xml") != 0)
            client.println("Content-Type: application/xml");
          else
            client.println("Content-Type: text");
            client.println();

            byte clientBuf[64];
            int clientCount = 0;
           while(file.available()){
              clientBuf[clientCount] = file.read();
              clientCount++;
              if(clientCount > 63) {
                Serial.println("Pacote de dados de 64 bytes enviado!");
                client.write(clientBuf,64);
                clientCount = 0;
              }
            }
              if(clientCount > 0) client.write(clientBuf,clientCount);
            
           client.print("<html><body>");

          if (verifica){
          client.println(F("<div id=\"outer\">"));
          client.print(F("<div id=\"inner\" style=\"height:"));
          client.print(percentagem, 1);
          client.println(F("%\">"));
          client.println(F("</div></div>"));
          client.println("</p></body></html>");
          }  
          if (verifica2){
           if (percentagem >= 90 || percentagem <=10){
           client.println(F("<table width=\"210\" height=\"42\" border=\"0\" cellpadding=\"1\" cellspacing=\"1\">"));
           client.println(F("<tr>"));
           client.print(F(" <td width=\"69\" height=\"40\" class=\"iframe2\"><div align=\"center\"><b><font color=#E60000>"));
           client.print(percentagem, 1);
           client.println(F("%</font></b></div></td>"));
           client.print(F("<td width=\"134\" class=\"iframe2\"><div align=\"center\"><b><font color=#E60000>"));
           client.print(litros, 1);
           client.print(F("</font></b></div></td>"));
           client.println(F("</tr>"));
           client.println(F("</table>"));
           client.println("</p></body></html>");
           } else {
           client.println(F("<table width=\"210\" height=\"42\" border=\"0\" cellpadding=\"1\" cellspacing=\"1\">"));
           client.println(F("<tr>"));
           client.print(F(" <td width=\"69\" height=\"40\" class=\"iframe2\"><div align=\"center\"><b>"));
           client.print(percentagem, 1);
           client.println(F("%</b></div></td>"));
           client.print(F("<td width=\"134\" class=\"iframe2\"><div align=\"center\"><b>"));
           client.print(litros, 1);
           client.print(F("</b></div></td>"));
           client.println(F("</tr>"));
           client.println(F("</table>"));
           client.println("</p></body></html>");
       }
          }
          verifica = false;
          verifica2 = false;
          file.close();

        }
        else {
          client.println("HTTP/1.1 404 Not Found");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<h2>Error 404</h2>");
          client.println("");
        }
        break;
      }
          if(strstr(clientline, "GET/&niv") !=0){
            Log = file.open(&root, ficheiroLog, O_READ | O_APPEND);
            if (Log){
              Serial.println(F("A enviar ficheiro LOG para download!"));
              byte clientBuffer[64];
              int clientCounter = 0;
                while(file.available()) {
                clientBuffer[clientCounter] = file.read();
                clientCounter++;
                if(clientCounter > 63) {
                Serial.println("Pacote de dados de 64 bytes enviado!");
                client.println("Content-Disposition: attachment; filename=\"ficheiroLOG.txt\"");
                client.write(clientBuffer,64);
                clientCounter = 0;
              }
            }
              if(clientCounter > 0) client.write(clientBuffer,clientCounter);
          file.close();
      } else {
        client.println("HTTP/1.1 404 Not Found");
        client.println("Content-Type: text/html");
        client.println();
        client.println("<h2>ERRO! Ficheiro LOG não existe!</h2>");
        client.println("");
    }
    }    
  }

//PISCA LED PARA INDICAR QUE ESTÁ CONCLUÍDA A COMUNICAÇÃO SERVIDOR-CLIENTE E CLIENTE-SERVIDOR
    digitalWrite(ledIndicadorComms, HIGH);
    delay(1);
    digitalWrite(ledIndicadorComms, LOW);
    delay(1);
    client.stop();
  }
}

/*FIM DO PROGRAMA*/
Where is
Code:
//COLOCA EM BUFFER OS BYTES LIDOS DO FICHEIRO DO CARTÃO SD
            byte clientBuf[64];
            int clientCount = 0;
           while(file.available()){
              clientBuf[clientCount] = file.read();
              clientCount++;
              if(clientCount > 63) {
                Serial.println("Pacote de dados de 64 bytes enviado!");
                client.write(clientBuf,64);
                clientCount = 0;
              }
            }
              if(clientCount > 0) client.write(clientBuf,clientCount);
I had on my original code
Code:
int16_t c;
          while ((c = file.read()) >= 0) {
            client.print((char)c);
          }
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 139
Posts: 5860
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You must be more specific about how it works and what you expected. That code appears to add html to the end of every file. You shouldn't do that.

Comment out this line in your sketch. It will slow stuff up a bit.
Code:
                Serial.println("Pacote de dados de 64 bytes enviado!");
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Serial monitor keeps showing 64bytes package sended a lot of times and images (108Kb is the biggest one) were much slower than it was before... It makes no sense because zoomkat tested in his sketch and says it worked, but in mine i really love it to work too, because it takes about 15 seconds to load that image...
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 139
Posts: 5860
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Serial monitor keeps showing 64bytes package sended a lot of times and images (108Kb is the biggest one) were much slower than it was before... It makes no sense because zoomkat tested in his sketch and says it worked, but in mine i really love it to work too, because it takes about 15 seconds to load that image...
Then you did not comment out that line. The Serial.println() call is slowing up your upload. Comment it out like this:
Code:
                // Serial.println("Pacote de dados de 64 bytes enviado!");
That was just for debugging.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok, so no serial prints mixed with that part of the code, is it?
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 139
Posts: 5860
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok, so no serial prints mixed with that part of the code, is it?
Correct. That was just so I could see how many packets were being sent, and it really slows up the transfer. Once you get the file sending ok, then no more Serial.println() in that loop.

Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks again. I'll test it tomorrow and give a feedback. thanks! (again) smiley-lol
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, its working fine! Many thanks for this post   smiley-wink
Logged

Holland
Offline Offline
Newbie
*
Karma: 2
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

While quite busy working on my Arduino home automation project, forgot to look at this forum.
And see what happened: lots of discussion and good tips, thanks everbody, I'm gonna study all comments!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have some problems with my server. When I want to acces the index.htm he didn't show me nothing and i don't know how to fix it (i use the transfer code from here , i'm new on this) :

Code:

          Serial.println("Opened!");
          //File types
          client.println("HTTP/1.1 200 OK");
          if (strstr(filename, ".htm") != 0)
            client.println("Content-Type: text/html");
          else if (strstr(filename, ".css") != 0)
            client.println("Content-Type: text/css");
            else if (strstr(filename, ".php") != 0)
            client.println("Content-Type: text/php");
          else if (strstr(filename, ".png") != 0)
            client.println("Content-Type: image/png");
          else if (strstr(filename, ".jpg") != 0)
            client.println("Content-Type: image/jpeg");
          else if (strstr(filename, ".gif") != 0)
            client.println("Content-Type: image/gif");
          else if (strstr(filename, ".3gp") != 0)
            client.println("Content-Type: video/mpeg");
          else if (strstr(filename, ".pdf") != 0)
            client.println("Content-Type: application/pdf");
          else if (strstr(filename, ".js") != 0)
            client.println("Content-Type: application/x-javascript");
          else if (strstr(filename, ".xml") != 0)
            client.println("Content-Type: application/xml");
          else
            client.println("Content-Type: text");
          client.println();

            
          byte clientBuf[64];
            int clientCount = 0;
           while(file.available()){
              clientBuf[clientCount] = file.read();
              clientCount++;
              if(clientCount > 63) {
                client.write(clientBuf,64);
                clientCount = 0;
              }
            }
              if(clientCount > 0) client.write(clientBuf,clientCount);
            

          client.print("<html><body>"); //HTML code starts here
          client.print("<P align=\"center\">");
          client.print("Hits since reset: <b>");  
          client.print(hits); //Print hits to client
          client.print("</b><br>");

          photocellReading = analogRead(photocellPin);
          client.print("Light reading: ");
          client.print(photocellReading); //Prints light reading to client

          // A few threshholds
          if (photocellReading < 10) {
            client.print(" - Dark");
          }
          else if (photocellReading < 200) {
            client.print(" - Dim");
          }
          else if (photocellReading < 500) {
            client.print(" - Light");
          }
          else if (photocellReading < 800) {
            client.print(" - Bright");
          }
          else {
            client.print(" - Very bright");
          }

          client.print("</p></body></html>"); //HTML code ends here
          //End hit counter and light value

          if(strstr(clientline, "GET/&niv") !=0){
            Log = file.open(&root, rootFileName, O_READ | O_APPEND);
            if (Log){
              Serial.println(F("A enviar ficheiro LOG para download!"));
              byte clientBuffer[64];
              int clientCounter = 0;
                while(file.available()) {
                clientBuffer[clientCounter] = file.read();
                clientCounter++;
                if(clientCounter > 63) {
                client.println("Content-Disposition: attachment; filename=\"ficheiroLOG.txt\"");
                client.write(clientBuffer,64);
                clientCounter = 0;
              }
            }
              if(clientCounter > 0) client.write(clientBuffer,clientCounter);
            file.close();
        }
        else {
          client.println("HTTP/1.1 404 Not Found");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<h2>Error 404</h2>");
          client.println("");
        }
        break;
      }
    }
    digitalWrite(greenLEDandBEEP, HIGH);
    delay(1);
    digitalWrite(greenLEDandBEEP, LOW);
    client.stop();
  }

}
  }
}
//The End /*

Logged

Pages: 1 [2]   Go Up
Jump to: