Issues Starting Ethernet After SDfat

Hello to all,
I'm having trouble with the Ethernet library and SDfat lib working together.
I'm using a arduino DUE with ethernet shield rev3 and using arduino software 1.5.2.
If used separate both work, or if I start ethernet my IP services work, or if I start card to read and write my sketch also works.

The problem is that I want to collect IP information inside the card, but if I start ethernet after sdfat , the network does not work.
I'm properly exchanging SS pins for each moment and each access but still no success.

below is my Sketch

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

///////////Ethernet Config Initial with no card inserted

static byte arduinoMAC[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
static IPAddress ArduinoIPAddress(192,168,1,230);
static IPAddress ArduinoDns(192,168,1,5); 
static IPAddress ArduinoGw(192,168,1,10); 
static IPAddress ArduinoMask(255,255,255,0); 

EthernetServer telnetServer = EthernetServer(23);


void ethEnable()
{
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);
  pinMode(10, OUTPUT);
  digitalWrite(10, LOW);
}

void sdEnable()
{
  pinMode(4, OUTPUT);
  digitalWrite(4, LOW);
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);
}

void sdCARD_INIT()
{
    //initialize SD card..
    Serial.print("Initializing SD card...");
    if (!sd.begin(chipSelect, SPI_FULL_SPEED)) 
    {
      Serial.println("Error reading SDcard..");
      sd.initErrorHalt();
    }
    else
    {
      Serial.println("Done");
    }
}

void sdCARD_IP_CONFIG()
{
//here I remove the acces to file just for test and insert my new ip directly

      ArduinoIPAddress[0] = 192;
      ArduinoIPAddress[1] = 168;
      ArduinoIPAddress[2] = 1;
      ArduinoIPAddress[3] = 100;
      
       delay(150);
       
        Serial.println(ArduinoIPAddress);
      Serial.println("\nDone");
      configFile.close();
}

setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  
  sdEnable();
  
  sdCARD_INIT();
  sdCARD_IP_CONFIG();
  
  ethEnable();
  Ethernet.begin(arduinoMAC, ArduinoIPAddress,ArduinoDns,ArduinoGw,ArduinoMask);

  telnetServer.begin();
}

I´ll apreciate any ideas.

tks

I forgot to say..

If I comment out the sdCARD_INIT(), method call. the ethernet works with the new IP, but without read the card info

Ola dechen,

Adding the following line at the beginning of your code should resolve your issue.

#define USE_ARDUINO_SPI_LIBRARY 1

This topic has been discussed before here:

http://forum.arduino.cc/index.php?topic=146187.0

Regards!

tks Palliser for your answer,
but your suggestion doenst solve my issue,
I dont know here I´m missing.

here is my code now

#include <SPI.h>
#include <Ethernet.h>
#define USE_ARDUINO_SPI_LIBRARY 1

///////////Ethernet Config Initial with no card inserted

static byte arduinoMAC[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
static IPAddress ArduinoIPAddress(192,168,1,230);
static IPAddress ArduinoDns(192,168,1,5); 
static IPAddress ArduinoGw(192,168,1,10); 
static IPAddress ArduinoMask(255,255,255,0); 

EthernetServer telnetServer = EthernetServer(23);


void ethEnable()
{
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);
  pinMode(10, OUTPUT);
  digitalWrite(10, LOW);
}

void sdEnable()
{
  pinMode(4, OUTPUT);
  digitalWrite(4, LOW);
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);
}

void sdCARD_INIT()
{
    //initialize SD card..
    Serial.print("Initializing SD card...");
    if (!sd.begin(chipSelect, SPI_FULL_SPEED)) 
    {
      Serial.println("Error reading SDcard..");
      sd.initErrorHalt();
    }
    else
    {
      Serial.println("Done");
    }
}

void sdCARD_IP_CONFIG()
{
//here I remove the acces to file just for test and insert my new ip directly

      ArduinoIPAddress[0] = 192;
      ArduinoIPAddress[1] = 168;
      ArduinoIPAddress[2] = 1;
      ArduinoIPAddress[3] = 100;
      
       delay(150);
       
        Serial.println(ArduinoIPAddress);
      Serial.println("\nDone");
      configFile.close();
}

setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  
  sdEnable();
  
  sdCARD_INIT();
  sdCARD_IP_CONFIG();
  
  ethEnable();
  Ethernet.begin(arduinoMAC, ArduinoIPAddress,ArduinoDns,ArduinoGw,ArduinoMask);

  telnetServer.begin();
}

dechen,
I am far from my workbench right now. Thus, I could run some tests tonight. Let's see if in the meantime, someone else could support you here. Regards!

Thanks Palliser

Hello dechen,

Below a sketch I have generated combining your code with the following sample codes:

  • Ethernet WebServer (from Arduino IDE 1.5.2 examples)
  • SD CardInfo (from Arduino IDE 1.5.2 examples)
  • SdFatRead (from SdFat examples by radicalcakes-Arduino Dr. Hardy Project).

This sketch reads a text from the SD card and then modifies the html with it. Very simple and a good start point. Please, try it and let me know.

By the way, I am using Arduino Due with Arduino Ethernet (4GB card) shields and a crossover Ethernet cable between them and my laptop. Regards!

/* Arduino Due SD card test and Web Server samples combined
-Webserver created by David A. Mellis (2009) and modified by Tom Igoe (2012)
-SD card created by Limor Fried (2011) and modified by Tom Igoe (2012)
-SdFatRead created by radicalcakes (Dr. Hardy Project) (2012)
-Enable/Disable Ethernet/SD by dechen and Palliser (2013) 
*/

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

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;

// Matching SD in Arduino Ethernet shield: pin 4
const int chipSelect = 4;  
// Text to be read from SD card
String text1;

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
static byte arduinoMAC[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
static IPAddress ArduinoIPAddress(192,168,1,230);
EthernetServer telnetServer = EthernetServer(80);

// Matching pin to enable SD card and disable Ethernet
void ethEnable()
{
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);
  pinMode(10, OUTPUT);
  digitalWrite(10, LOW);
}

// Matching pin to enable Ethernet and disable SD card
void sdEnable()
{
  pinMode(4, OUTPUT);
  digitalWrite(4, LOW);
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);
}

// Initialization and reading of SD card
void sdCARD_INIT()
{
    Serial.print("\nInitializing SD card...");
    if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed. Things to check:");
    Serial.println("* is a card is inserted?");
    Serial.println("* Is your wiring correct?");
    Serial.println("* did you change the chipSelect pin to match your shield or module?");
    return;
  } else {
   Serial.println("Wiring is correct and a card is present."); 
  }

  // print the type of card
  Serial.print("\nCard type: ");
  switch(card.type()) {
    case SD_CARD_TYPE_SD1:
      Serial.println("SD1");
      break;
    case SD_CARD_TYPE_SD2:
      Serial.println("SD2");
      break;
    case SD_CARD_TYPE_SDHC:
      Serial.println("SDHC");
      break;
    default:
      Serial.println("Unknown");
  }

  // Opening the 'volume'/'partition' - it should be FAT16 or FAT32
  if (!volume.init(card)) {
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    return;
  }

  // print the type and size of the first FAT-type volume
  uint32_t volumesize;
  Serial.print("\nVolume type is FAT");
  Serial.println(volume.fatType(), DEC);
  Serial.println();
  
  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
  volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  volumesize *= 512;                            // SD card blocks are always 512 bytes
  Serial.print("Volume size (bytes): ");
  Serial.println(volumesize);
  Serial.print("Volume size (Kbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);
  Serial.print("Volume size (Mbytes): ");
  volumesize /= 1024;
  Serial.println(volumesize);
  
  Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  root.openRoot(volume);
  
  // list all files in the card with date and size
  root.ls(LS_R | LS_DATE | LS_SIZE);

  // reading a file
  if (file.open(root, "PRINT00.TXT", O_READ)) {
    Serial.println("Opened PRINT00.TXT");
  }
  else{
    Serial.println("error reading PRINT00.TXT");
    //error("file.open");
  }
Serial.println();
  
  // printing text inside the file
  int16_t c;
  while ((c = file.read()) > 0)
  {text1 = text1 + char(c);}
  Serial.println();
  Serial.println(text1);
  }

void sdCARD_IP_CONFIG()
{
//Changing the IP address for test

      ArduinoIPAddress[0] = 192;
      ArduinoIPAddress[1] = 168;
      ArduinoIPAddress[2] = 1;
      ArduinoIPAddress[3] = 101;
      
      delay(150);
       
      Serial.println(ArduinoIPAddress);
      Serial.println("\nNew IP address...Done");
}

void setup()
{
  // Open serial communications:
  Serial.begin(9600);
  // Enable SD card to read text in a file
  sdEnable();
  sdCARD_INIT();
  sdCARD_IP_CONFIG();
    
  // start the Ethernet connection and the server:
  Serial.println("starting the Ethernet connection and the server");
  ethEnable();
  Ethernet.begin(arduinoMAC, ArduinoIPAddress);
  telnetServer.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  // listen for incoming clients
  EthernetClient client = telnetServer.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        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();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // add a meta refresh tag, so the browser pulls again every 5 seconds:
          client.println("<meta http-equiv=\"refresh\" content=\"5\">");
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            // The following line was originally the printing of the text "analog channel"
            // Now will be the text inside the file in SD card "canal analogico"
            client.print(text1);
            client.print(analogChannel);
            client.print(" is ");
            client.print(sensorReading);
            client.println("
");       
          }
          client.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:
    client.stop();
    Serial.println("client disconnected");
  }
}

Hey Palliser, waht version of library are you using.

The code that you send me give compiling errors here.

In file included from C:\Program Files (x86)\Arduino\libraries\SD/SD.h:20,
from sketch_aug19a.ino:10:
C:\Program Files (x86)\Arduino\libraries\SD/utility/SdFat.h:26: fatal error: avr/pgmspace.h: No such file or directory
compilation terminated.

Hey Palliser
I was reading again all the post here, enter in the link below

Ola dechen,

Adding the following line at the beginning of your code should resolve your issue.

Code:
#define USE_ARDUINO_SPI_LIBRARY 1

This topic has been discussed before here:

Due with SD (SdFat) AND Ethernet Shield Problems - Arduino Due - Arduino Forum

Regards!

and see that the line you ask me to insert in the beginning of my sketch, in fact needs to be changed inside the SDfatconfig.h.

thats solve my issue..

tks for all your help man...