Tuesday, 24 February 2015

FYP2 - Week 4

Test the SD card in the Ethernet shield

The Arduino Uno board and official Arduino Ethernet shield are used. The Ethernet shield contains the WIZnet W5100 Ethernet chip. To set up the hardware, first plug the Arduino Ethernet shield into the Arduino board and then plug a micro SD card into the SD card socket of the Ethernet shield. Power the Arduino from a USB cable plugged into the host PC computer.

The Arduino, Arduino Ethernet shield and micro SD card are used to make a web server that hosts a web page on the SD card. When a browser requests a web page from the Arduino web server, the Arduino will fetch the web page from the SD card.

Creating the Web Page
Because the web page is to be stored on the SD card, it must first be created using a text editor and then copied to the SD card.

Web Page
Create the following web page in a text editor. When you save the text file, give it the name: index.htm


<!DOCTYPE html>
<html>
    <head>
        <title>Arduino SD Card Web Page</title>
    </head>
    <body>
        <h1>Hello from the Arduino SD Card!</h1>
        <p>A web page from the Arduino SD card server.</p>
    </body>
</html>


Nothing new here, it is the same as the web page from the first web server in this tutorial with just the text changed. Test this web page by opening it in a web browser
.
Copying the Web Page
You will need a micro SD card slot on your computer or a card reader that is capable of reading and writing a micro SD card.

Insert the micro SD card into the slot on the computer or card reader that is plugged into the computer and copy the index.htm file to the micro SD card.

Now plug the SD card into the micro SD card slot on the Ethernet shield.


SD Card Web Server

Hardware
You should now have the micro SD card with web page copied to it inserted into the card slot on the Arduino Ethernet shield. The Ethernet shield should be plugged into a compatible Arduino and into an Ethernet cable connected to your network. The Arduino / Ethernet shield should be powered from a USB cable.

Arduino Sketch
The Arduino sketch that fetches the web page from the SD card and sends it to the browser is shown below.


#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(192, 168, 0, 2); // IP address, may need to change depending on network
EthernetServer server(80);  // create a server at port 80

File webFile;

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
                // 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("Connection: close");
                    client.println();
                    // send web page
                    webFile = SD.open("index.htm");        // open web page file
                    if (webFile) {
                        while(webFile.available()) {
                            client.write(webFile.read()); // send web page to client
                        }
                        webFile.close();
                    }
                    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)
}


Using the Sketch
Copy the above sketch and paste it into the Arduino IDE. Load the sketch to the Arduino and then surf to the IP address set in the sketch with your web browser. The web page that you created should be displayed in the browser as it is served up by the Arduino SD card web server.


No comments:

Post a Comment