Go Down

Topic: Load pages from SD (W5100) (Read 1 time) previous topic - next topic

Eduort

Hi, i want to do a webserver whit 5 pages, i used a example code and it works, i can enter to the main page then open other, but the problem is that after open pages for a few times it start to load slow or dont open the page, then start to work correctly again,this always happen andt i dont know if is problem whit the code or my arduino/ethernet shield, i would apreciate if someone can help me to know what happen, thanks.
Code:
Quote
/*--------------------------------------------------------------
  Program:      eth_websrv_SD_link

  Description:  Arduino web server that serves up a basic web
                page that links to a second page. Clicking the
                link will open the second page. The second page
                links back to the first page.
 
  Hardware:     Arduino Uno and official Arduino Ethernet
                shield. Should work with other Arduinos and
                compatible Ethernet shields.
                2Gb micro SD card formatted FAT16
               
  Software:     Developed using Arduino 1.0.3 software
                Should be compatible with Arduino 1.0 +
               
                Requires index.htm and page2.htm to be on the
                micro SD card in the Ethernet shield micro
                SD card socket.
 
  References:   - WebServer example by David A. Mellis and
                  modified by Tom Igoe
                - SD card examples by David A. Mellis and
                  Tom Igoe
                - Ethernet library documentation:
                  http://arduino.cc/en/Reference/Ethernet
                - SD Card library documentation:
                  http://arduino.cc/en/Reference/SD

  Date:         2 March 2013

  Author:       W.A. Smith, http://startingelectronics.com
--------------------------------------------------------------*/

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10, 0, 0, 20); // IP address, may need to change depending on network
EthernetServer server(80);  // create a server at port 80

File webFile;

String HTTP_req = "";     // stores the received HTTP request

void setup()
{
    Ethernet.begin(mac, ip);  // initialize Ethernet device
    server.begin();           // start to listen for clients
    Serial.begin(9600);       // for debugging
   
    // initialize SD card
    Serial.println("Initializing SD card...");
    if (!SD.begin(4)) {
        Serial.println("ERROR - SD card initialization failed!");
        return;    // init failed
    }
    Serial.println("SUCCESS - SD card initialized.");
    // check for index.htm file
    if (!SD.exists("index.htm")) {
        Serial.println("ERROR - Can't find index.htm file!");
        return;  // can't find index file
    }
    Serial.println("SUCCESS - Found index.htm file.");
}

void loop()
{
    EthernetClient client = server.available();  // try to get client

    if (client) {  // got client?
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {   // client data available to read
                char c = client.read(); // read 1 byte (character) from client
                HTTP_req += c;          // save HTTP request character
                Serial.print(c);    // print HTTP request character to serial monitor
                // last line of client request is blank and ends with \n
                // respond to client only after last line received
                if (c == '\n' && currentLineIsBlank) {
                    // send a standard http response header
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connnection: close");
                    client.println();
                    // send web page
                    if ((HTTP_req.indexOf("GET / ") > -1)
                                 || (HTTP_req.indexOf("GET /index.htm") > -1)) {
                        webFile = SD.open("index.htm");        // open web page file
                    }
                    else if (HTTP_req.indexOf("GET /page2.htm") > -1) {
                        webFile = SD.open("page2.htm");        // open web page file
                    }
                    if (webFile) {
                        while(webFile.available()) {
                            client.write(webFile.read()); // send web page to client
                        }
                        webFile.close();
                    }
                    HTTP_req = "";  // empty the string
                    break;
                }
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                    // last character on line of received text
                    // starting new line with next character read
                    currentLineIsBlank = true;
                }
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
                }
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
    } // end if (client)
}

Erdin

You have a Ethernet, SD and String class in a Arduino Uno, that uses a lot of ram.

Is it an existing example ? Can you give a link to it ?

In the sketch, every ethernet byte is echoed to the serial monitor, which is only 9600 baud. Could you try without the "Serial.print(c);" ?

Eduort


You have a Ethernet, SD and String class in a Arduino Uno, that uses a lot of ram.

Is it an existing example ? Can you give a link to it ?

In the sketch, every ethernet byte is echoed to the serial monitor, which is only 9600 baud. Could you try without the "Serial.print(c);" ?

Thanks for answer, im using a Arduino Mega 2560, and this is the link of the code http://startingelectronics.com/tutorials/arduino/ethernet-shield-web-server-tutorial/SD-card-web-server-links/, i will try the code whitout the serial print and edit whit the result.

Eduort

I tried removing the serial monitor and the problem still.

Erdin

I don't know what causes the slow down.
The Mega has enough ram, that should not be a problem.
Are you using the newest Arduino 1.0.4 ?
In the sketch I read 1.0.3, but you should use the newest version.
If you are using only USB power, you could try a power supply for the Mega board. Allthough a low voltage would cause other problems.

SurferTim

It is because of the way you are sending it. One byte per packet is very inefficient.
Code: [Select]
if (webFile) {
 while(webFile.available()) {

// Here is your holdup. You are sending one byte per packet.
// 48 bytes of header, and one byte payload.
   client.write(webFile.read()); // send web page to client
 }
 webFile.close();
}

Here is another topic that covers this, and the cure.
http://forum.arduino.cc/index.php?topic=134868.0
Watch for me on National Geographic Wild "When Sharks Attack: Gulf Coast Killers". Airs on June 22nd.

Erdin

Thanks for that.
I tried using a buffer (Arduino 1.5.2), but I still got 4kbyte per second.
Perhaps I have to try it again with that example.
zoomkat writes: "... it reduced the upload time of a 253k jpg file from 62 sec. to 15 sec.".
That is from 4kbyte per second to 16kbyte per second.

SurferTim

I have not tried v1.5.2 yet. Each version seems to have its own quirks. However, it seems lately we are upgrading from a lot of quirks to just a few quirks. I guess if we keep at it... :)

My results with v1.0.4 agree with zoomkat. About 4 times as fast.
Watch for me on National Geographic Wild "When Sharks Attack: Gulf Coast Killers". Airs on June 22nd.

Erdin

It works !
I have Arduino 1.5.2 and a Mega 2560 board with the Ethernet Shield.
I did not use the sketch by Eduort, but have my own webserver code, that started with a Adafruit example.
Data is read from the SD card and stored in a buffer of 128 byte. I write that buffer to the client.
The result is a four times higher speed. About 16 kbyte per second.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy