How can make sd work while ethernet working?

I need to implement a xml server, which takes some information from SD card, actually contents of 48 files. I understand that I need to switch between sd and Ethernet and I tried every suggestion I could find in here, but I still can accomplish it. Could you please make me a suggestion?(By the way I’m using an Arduino Ethernet Board)

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


File myFile;
boolean state[48];
char ch[3];

EthernetServer server2(80);

void setup(){
  for(int i = 0;i<48;i++)
    state[i] = false;
  Serial.begin(9600);
  uint8_t mac[]     = { 0x90, 0xA2, 0xDA, 0x00, 0x51, 0x06 };
  uint8_t ip[]      = { 192, 168, 1, 7 };
  uint8_t gateway[] = { 192, 168, 1, 255 };
  uint8_t subnet[]  = { 255, 255, 255, 0 };
 

  

  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
 
  
  
  if(!SD.begin(4));
    return; 
  
  
  Ethernet.begin(mac, ip,gateway,subnet);
  digitalWrite(10,HIGH);
  delay(2000);
  server2.begin();
  
}

void loop(){
  
  EthernetClient client2 = server2.available();
  if(client2){
 
    boolean currentLineIsBlank = true;
    while (client2.connected()) {
      if (client2.available()) {
        char c = client2.read();
        
        if (c == '\n' && currentLineIsBlank) {
          client2.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Devices>");
          for(int i = 0; i<48; i++){
            String(i).toCharArray(ch,3);
            myFile = SD.open(ch);
            if (myFile) {
            client2.print("<Device id=\"");
            client2.print(ch);
            client2.print("\">");
            client2.print("<name>");
            
            String s = " ";
            
              //Serial.println(ch);
              // read from the file until there's nothing else in it:
              while (myFile.available()) {
    	        s += (char)myFile.read();
        
              }
              client2.print(s);
              client2.print("</name>");
              client2.print("<state>");
              client2.print(state[i]);
              client2.print("</state>");
              client2.println("</Device>");
              myFile.close();
              s = " ";
              //delay(10);
              } else {
  	      // if the file didn't open, print an error:
                Serial.println("error opening file");
              }
            }
          client2.println("</Devices>");
          //client2.println("
");
          //client2.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client2.stop();
    //Serial.println("client disonnected");
  }
  

}

SPI can connect multi-divices(chips),but can communicate with one of them by setting the corresponding chip select pin to LOW and the others set to HIGH.So if you want use SD card, set Pin 4 to LOW and the other to HIGH, and suggest u to set it to HIGH at once to release it.

This is incorrect.

  Ethernet.begin(mac, ip,gateway,subnet);
  // it should be
  Ethernet.begin(mac, ip, gateway, gateway, subnet);

How large is this file? You may be running out of SRAM.

             String s = " ";

              while (myFile.available()) {
    	        s += (char)myFile.read();
              }

The String variable type is notorious for crashing sketches. A character array would be better until they get the bugs out of String type.

There are other examples of server code around that use files for the webpage. zoomkat has a couple posted here on the forum.

The library code handles the slave select pins once the begin() functions are called. No need to manipulate those after the setup function.

SurferTime, there are just one line of string in the files. Without ethernet connection I can read them very well. Also this format Ethernet.begin(mac, ip,gateway,subnet); is working, I tested and used it other places.

sadoo:
SurferTime, there are just one line of string in the files. Without ethernet connection I can read them very well. Also this format Ethernet.begin(mac, ip,gateway,subnet); is working, I tested and used it other places.

That will not work outside your localnet. If you specify the gateway as 255.255.255.0, then nothing will be routed outside your localnet. Don't say you weren't warned.

This applies to IDE V1.0.1 and higher. Anything below that has its own bug in the ethernet library. What IDE version are you using?

Thank you for your warning.

Zintinger, are you suggesting something like:

void setup(){
  for(int i = 0;i<48;i++)
    state[i] = false;
  Serial.begin(9600);
  uint8_t mac[]     = { 0x90, 0xA2, 0xDA, 0x00, 0x51, 0x06 };
  uint8_t ip[]      = { 192, 168, 1, 7 };
  uint8_t gateway[] = { 192, 168, 1, 255 };
  uint8_t subnet[]  = { 255, 255, 255, 0 };
 

  
  pinMode(4,OUTPUT);
  pinMode(10,OUTPUT);
  digitalWrite(4,LOW);
  digitalWrite(10,HIGH);
 
  
  
  if(!SD.begin(4));
    return; 
  
  digitalWrite(10,LOW);
  digitalWrite(4,HIGH);
  Ethernet.begin(mac, ip,gateway,gateway,subnet);
  
  delay(2000);
  server2.begin();
  
  

}

this?

What IDE version are you using?

1.0.1 on MacBook

I do not have a problem with using the SD and w5100 together. Neither does zoomkat. Here is the setup code I use to initialize both correctly.

void setup() {
  Serial.begin(9600);

  // disable w5100 SPI while starting SD
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);

  Serial.print(F("Starting SD..."));
  if(SD.begin(4) == 0) Serial.println(F("fail"));
  else Serial.println(F("ok"));

  Serial.print(F("Starting w5100..."));
  Ethernet.begin(mac, ip, gateway, gateway, subnet); 
  Serial.println(Ethernet.localIP());

  Serial.println(F("Ready."));
}

edit: I haven't tried this, but it looks ok to me. This is one of zoomkat's latest examples.

Actually I tried almost every combination with, pin 4 and 10, low and high. Still can't get it through.

Have you tried using the devices separately? Here is my server code in the playground. It has the SD startup commented out, but you should be able to understand how to use it.
http://playground.arduino.cc/Code/WebServerST
Uncomment the SD.begin() part and try that. Does that work?

Nope, it's not working. When I commented out the sd read related parts it works

What do you mean by the "SD read related part"? There is no SD.read() function calls in the code in the playground. It only initializes the SD. Do you mean the SD.begin(4) call?

Sounds like you have an SRAM problem. I did not notice that when I posted that code in the playground, I did not include the F() function to reduce SRAM usage. I changed it to include those now.

 while (myFile.available()) {
    	        s += (char)myFile.read();
        
              }

I mean commenting out this part. Not to trying to read anything in the sd.

That is not my code. I never use the String variable type in any of my code. You are not reading (or not understanding) what I am trying to tell you. I normally do not quote myself but here goes...

The String variable type is notorious for crashing sketches. A character array would be better until they get the bugs out of String type.

I am not talking about your code, ok I am trying with char array.

sadoo:
I am not talking about your code, ok I am trying with char array.

Now we are on the same page. :slight_smile: Let me know how it does. zoomkat's code uses a character array to transfer the sd file to the ethernet connection.

Thank you, done.