Ethernet and SD issue

Combining the power of the ATmega2560 MCU with onboard Ethernet, a microSD card slot, an efficient switchmode power supply, and a small prototyping area. [Product page]
Post Reply
ercl87
Posts: 4
Joined: Mon Aug 25, 2014 9:08 pm

Ethernet and SD issue

Post by ercl87 » Mon Aug 25, 2014 9:26 pm

Hello, I have been working on some code with the Ethermega that needs to use the SD card and Ethernet together. I got the webServer example working, and i have also been able to get the SD card working. I tried to integrate them both together in a project i have been working on lately, but every time I connect to the Ethermega through the local network, the SD card stops working. I am using arduino 1.5.6-r2 because most of my development is done with the arduino Due.

To make sure I was not messing anything up, I reloaded the webserver example and added some simple SD code to list the files on the card. What is really weird is that if i have enough client.print()'s before I access the SD, I can get it to work. I have read a massive amount of posts on about the etherMega and ethernet shields. I have tried manually writing to the CS pins(as you can see from the last function in the code), I have gone into the libraries for the SD card and Ethernet chip, I just cannot find what the problem is. Could this be a problem with Arduino 1.5.6-r2? I am using the Ethermega 1.1

Thank you for your help!

Code: Select all

/*
  Web Server

 A simple web server that shows the value of the analog input pins.
 using an Arduino Wiznet Ethernet shield.

 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * Analog inputs attached to pins A0 through A5 (optional)

 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe

 */

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

File tmpPath;
File logFile;

const byte SD_CARD = 0;
const byte ETHERNET = 1;

/////////////////////////////////////////////////////////////////////
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
 1, 1, 1, 1, 1, 1 };

IPAddress ip(192, 168, 13, 11);

IPAddress    gateway( 192, 168, 15, 1);                       // the IP of the router or acsesspoint
IPAddress    subnet(255, 255, 255, 0 );

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(25565);

void setup() {
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  pinMode(53,OUTPUT);
   delay(50);
  if (!SD.begin(4))
  {
    Serial.println("initialization failed!");
  }
  else
  {
    Serial.println("initialization done.");
  }

  Ethernet.begin(mac, ip);
  server.begin();
  // Open serial communications and wait for port to open:
  Serial.begin(57600);
  


  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  // listen for incoming clients
  EthernetClient client = server.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') {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            client.print("analog input ");
            client.print(analogChannel);
           client.print(" is ");                  //If i comment this line, it no longer works
            client.print(sensorReading);
            // client.println("<br />");
          }

          tmpPath = SD.open("/"); //set the tmpFile to the root
          tmpPath.rewindDirectory();
          Serial.println("ls");
          File entry = tmpPath.openNextFile();
          while (entry)
          {
    
            Serial.printf("\t/%s\n",entry.name(),entry.size());
            entry.close();
            entry = tmpPath.openNextFile();
          }
          tmpPath.close();
        
          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");
  }
}


void switchTo(byte _choice)
{
  if(_choice == SD_CARD ) 
  {
    digitalWrite(10,HIGH);
    digitalWrite(4,LOW);
  }
  else if( _choice == ETHERNET)
  {
    digitalWrite(10,LOW);
    digitalWrite(4,HIGH);
  }
  
}


andrew
Freetronics Staff
Freetronics Staff
Posts: 978
Joined: Sun Jul 14, 2013 7:06 am
Location: Melbourne, Australia
Contact:

Re: Ethernet and SD issue

Post by andrew » Tue Aug 26, 2014 12:41 am

Try it with the stable IDE v1.0.5. You can have two or more versions of the IDE on the one machine.

ercl87
Posts: 4
Joined: Mon Aug 25, 2014 9:08 pm

Re: Ethernet and SD issue

Post by ercl87 » Tue Aug 26, 2014 12:58 am

Wow thank you for the fast response! Unfortunately 1.0.5 had the same affect. Am i understanding this correctly. pin 53 needs to be set as an output, but pin 4 and 10 don't need to be touched because the Ethernet and SD libraries take care of the CS pins? I know I need to use SD.begin(4), but what about pin 10?

I am pretty sure the ethernet library is controlling its CS pin because even if i set pin 10 to high, it still operates fine. i.e. it is setting it low when it needs to.

angusgr
Freetronics Staff
Freetronics Staff
Posts: 853
Joined: Tue Apr 09, 2013 11:19 pm
Location: Melbourne, Australia
Contact:

Re: Ethernet and SD issue

Post by angusgr » Wed Aug 27, 2014 4:57 am

I tried the sketch you posted in 1.5.6 on an EtherMega R3 and it worked for me with a couple of tweaks. It even worked with the line marked "If i comment this line, it no longer works" commented out.

The tweaks were:

* "Serial.begin(57600);" should go before the line with SD.begin(4), so you can see the SD initialization success/fail message.

* Ethernet.begin() and server.begin() are repeated twice (this doesn't seem to make any difference, but might as well remove the first set).

* "Serial.printf("\t/%s\n",entry.name(),entry.size());" doesn't compile for me. I replaced it with the following lines:

Code: Select all

Serial.print("\t");
Serial.print(entry.name());
Serial.print(" - ");
Serial.println(entry.size());
Which is more code but much less resource-intensive than a printf statement.

I've posted the whole sketch I ran here as well: http://pastebin.com/raw.php?i=wZLk9RDr

At a guess, the SD.begin() may be failing and it's not printing out because of the Serial.begin() coming later. You may need to reformat the SD card.

Hope this helps you get things working,


Angus

ercl87
Posts: 4
Joined: Mon Aug 25, 2014 9:08 pm

Re: Ethernet and SD issue

Post by ercl87 » Wed Aug 27, 2014 5:30 pm

Thank you for the awesome support! My bad with the code I posted. I had been experimenting with so many things that it ended up getting all messed up. I loaded your code and it worked. I could see all of the files in the root directory. But after commenting out some of the client.println()'s, it fails to get the SD card directory. This worries me if it is this unstable. I wonder if the chip select pins are not being controlled right and having different amounts of client.prints changes the timing of the communication so it works sometimes. I also found that with your code, sometimes(rarely) it doesn't get the whole directory or it fails to get any files at all.

Thanks in advance.

Code: Select all

void loop() {
  // listen for incoming clients
  EthernetClient client = server.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') {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // output the value of each analog input pin
          // for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
          //   int sensorReading = analogRead(analogChannel);
          //   client.print("analog input ");
          //   client.print(analogChannel);
          //  client.print(" is ");                 
          //   client.print(sensorReading);
          //   client.println("<br />");
          // }
          
          tmpPath = SD.open("/"); //set the tmpFile to the root
          tmpPath.rewindDirectory();
          Serial.println("ls");
          File entry = tmpPath.openNextFile();
          while (entry)
          {
            Serial.print("\t");
            Serial.print(entry.name());
            Serial.print(" - ");
            Serial.println(entry.size());
            entry.close();
            entry = tmpPath.openNextFile();
          }
          tmpPath.close();
       
          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");
  }
}

angusgr
Freetronics Staff
Freetronics Staff
Posts: 853
Joined: Tue Apr 09, 2013 11:19 pm
Location: Melbourne, Australia
Contact:

Re: Ethernet and SD issue

Post by angusgr » Mon Sep 01, 2014 1:37 am

Hi ercl,

Unfortunately the code you posted works fine for me as well. Perhaps it's something with the SD card itself?


Angus

Post Reply