Ethernet SD + wire.h conflicting

Hello I am having some conflict trouble with the Ethernet shield. I have written a program to read two Ic2 temperature chips and display the result on a web page. Using the Ethernet shield on its own with no SD library included the sketch works perfectly. When I add the SD library the web page is lost. I know the SD library works as I have unused it to send a web page during the examples.
What I was hoping to do is show the temperature figures on the web site and record the temperature figures periodically.
My code is below. Can someone please point me in the right direction? I am new to this and still learning so sorry if this turns out to be a silly mistake I’m making.
I have searched the forum and found a number of people with the same conflict but they have not explained how they overcome it.

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

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 125); // IP address, may need to change depending on network
EthernetServer server(80);  // create a server at port 80
int panel_address = 72;
int lower_address = 77;
int pump = 3;
int ledStatus = 5;
int ledState;

String HTTP_req;            // stores the HTTP request

void setup()
{
    Ethernet.begin(mac, ip);  // initialize Ethernet device
    server.begin();
    Wire.begin();
    pinMode (10, OUTPUT);
    digitalWrite (10,HIGH);
    Serial.begin(9600);
    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.");
    
    pinMode (pump, OUTPUT);
    pinMode (ledStatus, OUTPUT);
    ledState = LOW;
}

void loop()
{
    EthernetClient client = server.available();  // try to get client
      if (ledState == LOW)
      ledState = HIGH;
    
      else
      ledState = LOW;
      digitalWrite (ledStatus, ledState);
    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 the HTTP request 1 char at a time
                // 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: keep-alive");
                    client.println();
                    // AJAX request for Temp state
                    if (HTTP_req.indexOf("ajax_Temp") > -1) {
                        // read Temp state and send appropriate paragraph text
                        GetTempState(client);
                    }
                    else {  // HTTP request for web page
                        // send web page - contains JavaScript with AJAX calls
                        client.println("<!DOCTYPE html>");
                        client.println("<html>");
                        client.println("<head>");
                        client.println("<title>Solar Panel</title>");//page heading title
                        client.println("<script>");
                        client.println("function GetTempState() {");
                        client.println("nocache = \"&nocache=\"\
                        + Math.random() * 1000000;");
                        client.println("var request = new XMLHttpRequest();");
                        client.println("request.onreadystatechange = function() {");
                        client.println("if (this.readyState == 4) {");
                        client.println("if (this.status == 200) {");
                        client.println("if (this.responseText != null) {");
                        client.println("document.getElementById(\"Temp_txt\")\
.innerHTML = this.responseText;");
                        client.println("}}}}");
                        client.println(
                        "request.open(\"GET\", \"ajax_Temp\" + nocache, true);");
                        client.println("request.send(null);");
                        client.println("setTimeout('GetTempState()', 1000);");
                        client.println("}");
                        client.println("</script>");
                        client.println("</head>");
                        client.println("<body onload=\"GetTempState()\">");
                        client.println("<h1>Solar Panel Temperatures</h1>");
                        client.println(
                        "<p id=\"Temp_txt\">Temp state: Not requested...</p>");
                        client.println("</body>");
                        client.println("</html>");
                    }
                    // display received HTTP request on serial port
                    Serial.print(HTTP_req);
                    HTTP_req = "";            // finished with request, empty 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)
}

// send the state of the Temp to the web browser
void GetTempState(EthernetClient cl)
{

    
  Wire.beginTransmission(panel_address);
  Wire.write(0);
  Wire.endTransmission();
  Wire.requestFrom(panel_address, 1);
  while (Wire.available () == 0);
  int Panel = Wire.read();
  delay (500); 
  Wire.beginTransmission(lower_address);
  Wire.write(0);
  Wire.endTransmission();
  Wire.requestFrom(lower_address, 1);
  while (Wire.available () == 0);
  int lowerTemp = Wire.read();
  delay (500);
  cl.println("<p>Panel Temp<p>");  
  cl.println(Panel);
  cl.println ("<p>Lower Temp<p>");
  cl.println(lowerTemp);
  int diff = lowerTemp + 2;
  cl.println ("<p>Panel Temperature pump turns on at<p>");
  cl.println (diff);
  cl.println ("<P>Pump Status<P>");
  if (diff < Panel) {
    digitalWrite (pump, HIGH);
    cl.println ("<P> ON<P>");
    delay(1000);
    digitalWrite (pump, LOW);
    delay (1000);
  }
    else{
  digitalWrite (pump, LOW);
    }
  
    }

When I add the SD library the web page is lost.

Likely because you have run out of memory. Have you checked how much you have at appropriate places?

"My code doesn't work!" doesn't give us much to work with. That code does something. You haven't explained what. You want it to do something (else). You haven't explained what.

hello paul thanks for the reply.

I did read about the possibility of running out of memory, I tried removing one of the sensor readings and some of the serial print lines. the code on upload is well lower than what the Arduino can take, but this is not the running memory from what I have read.
So very likely that I am running out of memory.

How do i check this? If i can confirm that this is the problem then I can start looking at how to fix it. Also is there a way to increase the memory. Maybe I should get an Arduino Mega. But first I need to confirm that I am running out of memory.

Sorry for all the question, thanks again for the help

Just tried the code with the Wire sensor remove and taking out all serial print code that is not needed. also the pump control removed. this leaves just the Ethernet connection one sensor and the sd set-up. still the program will not work. the sketch is now 21,847 bytes. I don't think this is a memory problem. is it possibly due to the cs conflicting between the Ethernet and the sd card.

Any one have an idea that i can try as i am hitting a brick wall with this one.

The Arduino has three kinds of memory - EEPROM, Flash, and SRAM. The code, when you upload, goes in Flash memory. The SD card buffer, any arrays or global variables, the heap and the stack go in SRAM.

If you have a 328 based Arduino then you have 2K of SRAM. The SD library has a 512 byte buffer. That alone eats up 25% of your SRAM.

So, you are wrong. It is NOT highly unlikely that you are running out of memory.

Try this, and post the results:
http://playground.arduino.cc/Code/AvailableMemory

Hello Paul

I have had a quick play with one of the memory sketches, with the SD library removed and taking a snap shot at the end of the client list i get just over 500 bytes. so with the SD library in from your calculations this would be just over 1000. I can't run it with the SD library in as it just stops working and I don't get any results.

I will have more of a play later when i get some more time. From what you are saying I might invest in a Mega and see if the sketch works on this.

Sorry paul i read the memory site wrong. The result that i was getting was the amount of memory i have free with would be 500 bytes with out the SD library. so with the SD library i would be over the top for the UNO.

thanks for you help at least now I can see what is going wrong. I will get a mega and try it again.

Quick question does the Ethernet shield fit the mega the same as the uno?

with the SD library removed and taking a snap shot at the end of the client list i get just over 500 bytes.

500 minus 512 is not 1000. Unless my calculator is acting up.