I have placed a formatted empty microSD card into the microSD slot.
I have combined code from Webserver sketch of Ethernet library and listfiles sketch of the SD library.
In setup(), I
a.) list the files on the SD card
b.) display the IP address of the Ethernet shield
c.) list the files on the SD card
c.) display the IP address of the Ethernet shield
Trying to understand how to access both.
But I am not getting a valid IP address.
Then when the sketch goes back to the SD card, no files are displayed.
What am I doing wrong?
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
File root;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] =
{
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 2, 50);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup()
{
Serial.begin(9600);
while (!Serial)
{
; // wait for serial port to connect. Needed for native USB port only
}
pinMode(4, OUTPUT);
pinMode(10, OUTPUT);
pinMode(10, LOW);
//pinMode(4, HIGH);
delay(5);
Serial.print("\nInitializing SD card...");
if (!SD.begin(4))
{
Serial.println("failed!");
return;
}
else
{
Serial.println("done");
}
//display list of files
root = SD.open("/");
printDirectory(root, 0);
pinMode(10, HIGH);
pinMode(4, LOW);
delay(5);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.println("-------------------------"); //added 16Feb2016
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
Serial.println("-------------------------"); //added 16Feb2016
pinMode(10, LOW);
pinMode(4, HIGH);
delay(5);
Serial.println("back to SD");
//go back to SD
root = SD.open("/");
printDirectory(root, 0);
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
Serial.println("-------------------------"); //added 16Feb2016
}
void loop()
{
}
void printDirectory(File dir, int numTabs) {
while (true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
break;
}
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs + 1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}