Hi all
I'm trying to build a server using an ITEAD ENC28J60 shield on an Arduino UNO to serve a page stored in the sd card.
The shield works fine using the UIPEthernet library when serving a simple text page, but goes into chaos when trying to combine the ethernet function with the sd card reading. I'm simply getting stucked on the setup as the code goes into an infinite loop with no apparent reason (shouldn't the set up code run just once?).
I have moved the jumper on the board as explained in the shield documentation, so CS pin for the ethernet function is now on pin 8, while sd card is on pin 9. I have changed the ENC28J60_CONTROL_CS to pin 8 on file ENC28j60network.h.
When using the simplest setup code to start up the server (with no sd card initialization), the server starts up fine and responds correctly. However when including the sd.h library and the sd card initialization code, the setup code goes into a strange loop and there it remains.
Any help from your experience will be very welcome!
Here's the full code, altough it does not add much as it gets stucked in a loop while executing the set up part. The code is as simple as it could be to try to solve the question.
Once started, the serial prints "Initializing SD card...", "Success...", "Connecting..." and so on on an infinitely loop.
#include <UIPEthernet.h>
#include <SD.h>
byte mac[] = { 0x54, 0x34, 0x41, 0x30, 0x30, 0x31 };
byte ip[] = { 192,168,1,18 };
EthernetServer server(80);
EthernetClient client;
Sd2Card card;
const int chipSelect = 4;
File webFile;
void setup()
{
Serial.begin(9600);
Serial.println("Initializing SD card...");
if (SD.begin(chipSelect)) Serial.println("init with SD.begin: SUCCESS - SD card initialized.");
else Serial.println("init with SD.begin: ERROR - SD card initialization failed!");
delay(1000);
delay(50);
Serial.println("connecting...");
Ethernet.begin(mac,ip);
delay(50),
server.begin();
delay(1000);
Serial.println(Ethernet.localIP());
Serial.println(Ethernet.subnetMask());
Serial.println(Ethernet.gatewayIP());
Serial.println(Ethernet.dnsServerIP());
}
void loop()
{
client = server.available();
if (client)
{
Serial.println("-> New Connection");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected())
{
if (client.available())
{
char c = client.read();
delay(1);
Serial.print(c);
if (c == '\n' && currentLineIsBlank)
{
Serial.println();
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE html>");
client.print("<html><title>My first Arduino Server</title>");
client.print("<body><h3>Hola Hola Hola!!!</h3></body>");
break;
}
if (c == '\n') currentLineIsBlank = true;
else if (c != '\r') currentLineIsBlank = false;
}
}
delay(10);
client.stop();
Serial.println("Disconnected");
}
Have you tried disabling the ENC28J60 SPI by setting its CS as OUTPUT and HIGH before starting the SD? If the ENC28J60 CS is D8, then this should do that.