Hello,
I am working on a project that has the requirement of both a web interface for current performance information and SD card to hold trending and historical data. I was working along and had the project working up until I attempted to add the data logging to SD Card. I found numerous examples both in the IDE, on the Web and here in the forums. I have followed what I thought was the proper code, See Below. When ever I remark out the code to init the SD Card and I/O Pins the Ethernet Web Server works with no issue, when the SD code is not remarked the web server does not function nor does the SD card, it fails to init.
#include <avr/pgmspace.h>
#include <string.h>
#include <Time.h>
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
//SD Card Stuff
const int chipSelectSD = 4;
const int chipSelectEth = 53;
Sd2Card card;
SdVolume volume;
SdFile root;
// 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(57600);
Serial.println("Initializing InPuts card...");
//Set Digital Input and Output Modes
Serial.println("Initializing OutPuts card...");
Serial.println("Initializing SD card...");
pinMode(chipSelectEth,OUTPUT);
//Temp Disable Ethernet to bring up SD
digitalWrite(chipSelectEth,HIGH);
//SD Init
if (!card.init(chipSelectSD)) {
Serial.println("card.init failed!");
}
// initialize a FAT volume
if (!volume.init(&card)) {
Serial.println("vol.init failed!");
}
//SD Card Info
// 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");
}
// Now we will try to open 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);
attachInterrupt(5,High_Alert,RISING);
attachInterrupt(5,High_Alert,FALLING);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
}
void loop (){
WebSession{};
}
/*
Web Pages
*/
void WebSession(){
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// 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();
client.println("<head><title>Status</title><META HTTP-EQUIV=REFRESH CONTENT=5><META HTTP-EQUIV=PRAGMA CONTENT=""NO-CACHE""></head><body bgcolor=""#455ac0"">");
client.println("<h1 align=center>System Status</h1>");
client.print("<p>Operation Mode: ");
client.println("
");
client.println("</body>");
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();
}
}
Does anyone have some guidance on where I have steered wrong?
Thanks
Chris