Arduino UNO + SDcard shield + ethernet shield webserver

As i was working with a SDcard shield and a ethernet shield with the UNO i found out some problems that this two have when they are plugged in together. For starters, i used a seeedstudio SDcard shield and ethernet shield. The problem, when these two are connected only the one that can function properly is the ethernet shield. The SDcard shield cant initialize the SD card therefore wont allow to use a HTML file on the SD. To resolve this i implemented the following code. Be advise that this won’t work on the seeedstudio V2 that utilize the ethernet controller W5200 or ANY one that has this controller.

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

#define SS_SD_CARD   4  //use pin 4 as SS
#define SS_ETHERNET 10  //use pin 10 as SS

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

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 4;

// set up variables using the SD utility library functions:
Sd2Card card;

File webFile;

void setup()
{
   // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
    pinMode(SS_SD_CARD, OUTPUT);
    pinMode(SS_ETHERNET, OUTPUT);
    digitalWrite(SS_SD_CARD, HIGH);  // SD Card not active
    digitalWrite(SS_ETHERNET, HIGH); // Ethernet not active
    uint8_t r = card.init(SPI_HALF_SPEED, 4);  // Use digital 4 as the SD SS line
    
    Ethernet.begin(mac, ip);  // initialize Ethernet device
    server.begin();           // start to listen for clients
    Serial.print("server is at ");
    Serial.println(Ethernet.localIP());
    digitalWrite(SS_SD_CARD, LOW); //SD Card now active

  
    // initialize SD card
    Serial.println("Initializing SD card...");
    
    if (SD.begin(4)) {
      Serial.println("SUCCESS - SD card initialized.");    
    }else{
      Serial.println("ERROR - SD card initialization failed!");
    }     
    
    // check for index.htm file
    if (SD.exists("solarIn.htm")) {
      Serial.println("solarIn.htm exists.");
    }
    else {
      Serial.println("solarIn.htm doesn't exist.");
    }
    digitalWrite(SS_SD_CARD, HIGH); // SD Card not active
}

void loop()
{
    pinMode(SS_SD_CARD, OUTPUT);
    pinMode(SS_ETHERNET, OUTPUT);
    digitalWrite(SS_SD_CARD, HIGH);  // SD Card not active
    digitalWrite(SS_ETHERNET, HIGH); // Ethernet not active
    
    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("solarIn.htm");  // open web page file
                    if (webFile) {
                        while(webFile.available()) {
                            client.write(webFile.read()); // send web page to client
                        }
                        webFile.close();
                    }else {
                      // if the file didn't open, print an error:
                      Serial.println("error opening solarIndex.htm");
                      }
                    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)
}

To explain, the W5100 ethernet shield and the SDcard shield uses the same SS (Slave Select) from the SPI. Here is the definition of this from the SPI library:

“With an SPI connection there is always one master device (usually a microcontroller) which controls the peripheral devices. Typically there are three lines common to all the devices:

  • MISO (Master In Slave Out) - The Slave line for sending data to the master.
  • MOSI (Master Out Slave In) - The Master line for sending data to the peripherals.
  • SCK (Serial Clock) - The clock pulses which synchronize data transmission generated by the master.
  • SS (Slave Select) - the pin on each device that the master can use to enable and disable specific devices.”

This defines the pins for the shields, pin 4 for the SD and pin 10 for the ethernet.
#define SS_SD_CARD 4
#define SS_ETHERNET 10

Sets the SS pins as output for both shields.
pinMode(SS_SD_CARD, OUTPUT);
pinMode(SS_ETHERNET, OUTPUT);

Deactivates the shields SS pin ( don’t worry, your library handles the activation at the appropriate time).
digitalWrite(SS_SD_CARD, HIGH); // SD Card not active
digitalWrite(SS_ETHERNET, HIGH); // Ethernet not active

Activates the SD pin.
digitalWrite(SS_SD_CARD, LOW);

On a rare note, when i named my HTML files as index.html or ANY name that has index on it,the UNO failed to recognize the file. Try to avoid the name, maybe is the shield or the UNO in any case feel free to share your experience.