Hi,
I have a really weird issue. I am using an uno with the Ethernet shield and the code I have written for the Ethernet and accessing the SD work separately but not together which I have no clue why?
Here is my code and to see what I mean comment out the initialization of the SD card and then the Ethernet and you can see how they do not work together
#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>
#include <Time.h>
#include <EthernetUdp.h>
const unsigned int LED_PIN = 13;
const unsigned int BAUD_RATE = 9600;
const unsigned int MAX_INPUT = 50;
char F1_FILE[] = "file1.txt";
char F2_FILE[] = "file2.txt";
byte mac[] = {
0xDE, 0xAA, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0,178);
EthernetClient client;
//Time sync
EthernetUDP Udp;
unsigned int localPort = 8886; // local port to listen for UDP packets
const int timeZone = 1; // Central European Time
IPAddress timeServer(132, 163, 4, 102); // time-a.timefreq.bldrdoc.gov
time_t prevDisplay = 0; // when the digital clock was displayed
void setup()
{
pinMode(LED_PIN, OUTPUT);
Serial.begin(BAUD_RATE);
Serial.println("Initializing...");
Serial.println("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
pinMode(10, OUTPUT);
if (!SD.begin(4)) {
Serial.println("initialization of SD failed!");
delay(1000);
return;
}
if(!f1FileCheck())
{
delay(1000);
return;
}
if(!f2FileCheck())
{
delay(1000);
return;
}
Serial.println(String(F1_FILE) + " exists");
Serial.println(String(F2_FILE) + " exists");
Serial.println("Initializing Ethernet...");
// start the Ethernet connection:
if(!ethernetInitialization())
{
delay(1000);
return;
}
Serial.println("initialization done.");
Serial.write("Begin terminal\n");
}
void loop()
{
if(Serial.available() > 0)
{
//read incomming commands
processIncomingByte(Serial.read());
}
if (timeStatus() != timeNotSet) {
if (now() != prevDisplay) { //update the display only if time has changed
prevDisplay = now();
digitalClockDisplay();
}
}
}
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
void printDigits(int digits){
// utility for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
void processIncomingByte (const byte inByte)
{
static char input_line [MAX_INPUT];
static unsigned int input_pos = 0;
switch (inByte)
{
case '\n': // end of text
input_line [input_pos] = 0;
processData (input_line);
input_pos = 0;
break;
case '\r':
break;
default:
if (input_pos < (MAX_INPUT - 1))
input_line [input_pos++] = inByte;
break;
}
}
void processData (const char * data)
{
String inData = data;
if(inData.length() > 2)
{
if(inData.substring(0,2) == "-u")
{
String uu = inData.substring(2,inData.length());
digitalWrite(LED_PIN,HIGH);
Serial.println("uu is "+ uu);
saveuu(uu);
}
if(inData.substring(0,2) == "-i")
{
String ii = inData.substring(2,inData.length());
digitalWrite(LED_PIN,HIGH);
Serial.println("ii is "+ ii);
saveii(ii);
}
else if(inData == "off")
{
digitalWrite(LED_PIN,LOW);
Serial.println("LED off");
}
else
{
Serial.print("Unknown command: ");
Serial.println(inData);
}
}
}
void saveuu (String uu)
{
Serial.println("Saving uu");
char uuChar[uu.length()+1];
uu.toCharArray(uuChar, uu.length()+1);
Serial.println("char uuChar: " + String(uuChar));
SD.remove(F1_FILE);
File f1File = SD.open(F1_FILE, FILE_WRITE);
f1File.write(uuChar);
f1File.close();
Serial.println("Written and close");
}
void saveii (String ii)
{
Serial.println("Saving ii");
char iiChar[ii.length()+1];
ii.toCharArray(iiChar, ii.length()+1);
Serial.println("char ii: " + String(iiChar));
SD.remove(F2_FILE);
File iiFile = SD.open(F2_FILE, FILE_WRITE);
iiFile.write(iiChar);
iiFile.close();
Serial.println("Written and close");
}
boolean f1FileCheck()
{
if(!SD.exists(F1_FILE))
{
Serial.println( String(F1_FILE)+ " does not exist");
File uuFile = SD.open(F1_FILE, FILE_WRITE);
uuFile.write("empty");
uuFile.close();
if(!SD.exists(F1_FILE))
{
Serial.println(String(F1_FILE) + " still does not exist");
return false;
}
Serial.println(String(F1_FILE) + " now exists");
}
return true;
}
boolean f2FileCheck()
{
if(!SD.exists(F2_FILE))
{
Serial.println( String(F2_FILE)+ " does not exist");
File iiFile = SD.open(F2_FILE, FILE_WRITE);
iiFile.write("empty");
iiFile.close();
if(!SD.exists(F2_FILE))
{
Serial.println(String(F2_FILE) + " still does not exist");
return false;
}
Serial.println(String(F2_FILE) + " now exists");
}
return true;
}
boolean ethernetInitialization()
{
delay(1000); // give the Ethernet shield a second to initialize:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
return false;
}
Serial.print("IP number assigned by DHCP is ");
Serial.println(Ethernet.localIP());
Serial.println("Syncing time");
Udp.begin(localPort);
Serial.println("waiting for sync");
setSyncProvider(getNtpTime);
return true;
}
/*-------- NTP code ----------*/
const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
time_t getNtpTime()
{
while (Udp.parsePacket() > 0) ; // discard any previously received packets
Serial.println("Transmit NTP Request");
sendNTPpacket(timeServer);
uint32_t beginWait = millis();
while (millis() - beginWait < 1500) {
int size = Udp.parsePacket();
if (size >= NTP_PACKET_SIZE) {
Serial.println("Receive NTP Response");
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
unsigned long secsSince1900;
// convert four bytes starting at location 40 to a long integer
secsSince1900 = (unsigned long)packetBuffer[40] << 24;
secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
secsSince1900 |= (unsigned long)packetBuffer[43];
return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
}
}
Serial.println("No NTP Response :-(");
return 0; // return 0 if unable to get the time
}
// send an NTP request to the time server at the given address
void sendNTPpacket(IPAddress &address)
{
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
Udp.beginPacket(address, 123); //NTP requests are to port 123
Udp.write(packetBuffer, NTP_PACKET_SIZE);
Udp.endPacket();
}
Thanks for any help