Arduino + official ethernet shield + SD -> all working together.

hi, i would like to ask for a little help on an issue covered in the spanish forum here...
http://arduino.cc/forum/index.php/topic,53817.0.html

I would like to know if its possible to make the arduino ethernet shield work in both modes: network and storage.

I am trying to put together a little code example which shows this funtionality and later apply it to my projects.
The code should be simple:

  • read analog value
  • do something internet related (like send value to mysql by using GET and a php file)
  • store the value in a file in the SD inserted on the ethernet shield.
//Libraries
#include <SPI.h>
#include <Ethernet.h>

//Server IP address
byte server[] = { xx,xx,xx,xx};   // Godaddy Server

//Setup a client
Client client(server, 80);

byte ip[] = { 192,168,1,69 };  // Using a Router
byte mac[] = { xx, xx, xx, xx, xx, xx};
byte gateway[] = { 192, 168, 1, 1 };   //your router's IP address

void setup()
{
  //Start ethernet, usb serial
Ethernet.begin(mac, ip, gateway);     //, subnet);
Serial.begin(9600); 
Serial.println(" ********************** Arduino Started ********************** ");
}

void loop() {
//Send the data -----------------------------------------------------------------------
  if (client.connect()) {   
    client.print("GET http://website.com/upload.php?P=");
    client.print(apparentPower);
    client.println();
    client.stop();
 else {
    Serial.println(" ***************** Failed to connect to client ***************** ");
  }

// do the store to SD here
// do the store to SD here
// do the store to SD here
// do the store to SD here

}

can anyone help me out please

im sure this could help many people in my situation

thanks a lot

If you have the official ethernet shield, there are two different SS pins, for the Ethernet shield and the SD card. To use the Ethernet shield, you set its SS pin (10) HIGH, and the SD card's SS (4) pin LOW. To use the SD card, you set its SS pin HIGH and the Ethernet shield's SS pin LOW.

If you are not using the official ethernet shield, you can't use both at the same time.

Reading from and writing to the SD card then is just like reading from/writing to any other SD card.

Thanks Paul for your answer.

I do have the official ethernet shield.

so this would be a correct approach??
any issues to be known because of chaning the 'mode' so many times?

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


//Server IP address
byte server[] = { 111,111,111,111};   // Godaddy Server

//Setup a client
Client client(server, 80);

byte ip[] = { 192,168,1,69 };  // Using a Router
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte gateway[] = { 192, 168, 1, 1 };   //your router's IP address

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 4;

void setup()
{
  //Start ethernet, usb serial
Ethernet.begin(mac, ip, gateway);     //, subnet);
Serial.begin(9600); 
Serial.println(" ********************** Arduino Started ********************** ");
}

void loop() {
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ","; 
    }
  }
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


// ----------------------------Send the data -------------------------------------------
//set its SS pin (10) HIGH, and the SD card's SS (4) pin LOW
  pinMode(10, INPUT);
  pinMode(4, OUTPUT);

  if (client.connect()) {   
    client.print("GET http://website.com/upload.php?P=");
    client.print(dataString);
    client.println();
    client.stop();
  }
 else {
    Serial.println(" ***************** Failed to connect to client ***************** ");
  }

// ----------------------------Save the data in the SD -------------------------------------------
//To use the SD card, you set its SS pin HIGH and the Ethernet shield's SS pin LOW
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to output, even if you don't use it:
  pinMode(10, OUTPUT);
  pinMode(4, INPUT); 

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
// do the store to SD here
// open the file. note that only one file can be open at a time, so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  } 
// ---------------- end of saving data to SD -----------------------------

}

The SS pins are OUTPUT pins. You do not change the mode of the pin during the application. You write different values to the pins.

You should initialize the card access in setup, not loop. Every pass through loop, it will initialize access again, wasting a lot of time.

PaulS:
The SS pins are OUTPUT pins. You do not change the mode of the pin during the application. You write different values to the pins.

You should initialize the card access in setup, not loop. Every pass through loop, it will initialize access again, wasting a lot of time.

i really appreciate your time to answer me Paul

but arent we supposed to change the working mode of the shield in between the code? inside the loop. so as to be able to use ethernet or SD depending on the situation?

so if you initialize the SD mode in the setup, then you cant use ethernet?

or am i missing something?

how would you make the switch between one mode and the other? (ie; ethernet, and SD)

I have modified the code again, maybe now is better?

thanks a lot !!!

//Libraries
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
/*
 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
*/
//Server IP address
byte server[] = { 111,111,111,111};   // Godaddy Server

//Setup a client
Client client(server, 80);

byte ip[] = { 192,168,1,69 };  // Using a Router
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte gateway[] = { 192, 168, 1, 1 };   //your router's IP address

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 4;

void setup()
{
// --------  Start ethernet --------------------------------
Ethernet.begin(mac, ip, gateway);     //, subnet);
// ----------- end setup ethernet --------------------------


//------------ start of setup SD -------------------------------
//To use the SD card, you set its SS pin HIGH and the Ethernet shield's SS pin LOW
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to output, even if you don't use it:
  pinMode(10, OUTPUT);

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
//------------ end of setup SD -------------------------------

// ---- start setup SERIAL PORT --------------------------------------------------
Serial.begin(9600); 
Serial.println(" ********************** Arduino Started ********************** ");
// ---- end setup SERIAL PORT --------------------------------------------------
}

void loop() {
// ++++++++++++++++++ GET SOME DATA ++++++++++++++++++++++++++
 // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ","; 
    }
  }
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


// ----------------------------Send the data via ethernet ----------------------------
//set its SS pin (10) HIGH, and the SD card's SS (4) pin LOW
digitalWrite(10, HIGH);       // turn on pullup resistors
digitalWrite(4, LOW);       // turn off pullup resistors

  if (client.connect()) {   
    client.print("GET http://website.com/upload.php?P=");
    client.print(dataString);
    client.println();
    client.stop();
  }
 else {
    Serial.println(" ***************** Failed to connect to client ***************** ");
  }

// ----------------------------Save the data in the SD -------------------------------------------
//To use the SD card, you set its SS pin HIGH and the Ethernet shield's SS pin LOW
digitalWrite(10, LOW);       // turn offpullup resistors
digitalWrite(4, HIGH);       // turn on pullup resistors
// do the store to SD here
// open the file. note that only one file can be open at a time, so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  } 
// ---------------- end of saving data to SD -----------------------------

delay(10000);
}

but arent we supposed to change the working mode of the shield in between the code?

Yes, but this is done by swapping which device is active (Ethernet shield or SD card), by changing which SS pin is HIGH.

so if you initialize the SD mode in the setup, then you cant use ethernet?

I didn't say anything about initializing the SD mode. I said you needed to initialize the SD card in setup, just like you initialize the ethernet shield (using ethernet.begin()). Once the card has been successfully initialized, switch the active device (a function to do this would be useful), by switching which SS pin is HIGH, then initialize the ethernet portion of the device. The order doesn't matter, as long as the SS pin states are correctly defined for the device you are trying to initialize/use.

I have modified the code again, maybe now is better?

If the card initialization fails, returning from setup is not a good idea. You need to set a flag that indicates whether the initialization steps succeeded or failed, and continue. If the card initialization fails, don't try to use it in loop().

You do need to define pin 4 as an OUTPUT pin, too.

digitalWrite(10, LOW);       // turn offpullup resistors
digitalWrite(4, HIGH);       // turn on pullup resistors

For output pins, digitalWrite() has nothing to do with pullup resistors.

Other than these issues, the code looks like it should work.

wow once again ... BIG THANKS !!! you cant imagine how much you have helped me !!

  • i have made a flag called SDavailable as you suggested
  • i have defined pin 4 and 10 as output. i still need to understand how these two are working.
    ......when pin 4 is high it selects SD and by putting pin 10 in low you deselect ethernet
    ......when pin 10 is high it selects ethernet and by putting pin 4 in low you deselect SD
    am i right?

if this code is good, then i will try to implement some kind of function which sends a string either to the SD or to ethernet.

looks good?

//Libraries
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
/*
 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
*/

//Server IP address
byte server[] = { 111,111,111,111};   // Godaddy Server
//Setup a client
Client client(server, 80);
byte ip[] = { 192,168,1,69 };  // Using a Router
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte gateway[] = { 192, 168, 1, 1 };   //your router's IP address


    boolean SDavailable;

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 4;

void setup()
{
// --------  Start ethernet --------------------------------
Ethernet.begin(mac, ip, gateway);     //, subnet);
// ----------- end setup ethernet --------------------------


//------------ start of setup SD -------------------------------
//To use the SD card, you set its SS pin HIGH and the Ethernet shield's SS pin LOW
  Serial.print("Initializing SD card...");
  // Set pin modes ...
  pinMode(10, OUTPUT);    // make sure that the default chip select pin is set to output, even if you don't use it:
  pinMode(4, OUTPUT);      // On the Ethernet Shield, CS is pin 4

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    //return;
    SDavailable =  false;  // set a flag to indicate that the SD is not available so that the code in the loop knows.
  }
  Serial.println("card initialized.");
//------------ end of setup SD -------------------------------

// ---- start setup SERIAL PORT --------------------------------------------------
Serial.begin(9600); 
Serial.println(" ********************** Arduino Started ********************** ");
// ---- end setup SERIAL PORT --------------------------------------------------
}



void loop() {
// ++++++++++++++++++ GET SOME DATA ++++++++++++++++++++++++++
 // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ","; 
    }
  }
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


// ----------------------------Send the data via ethernet ----------------------------
//set its SS pin (10) HIGH, and the SD card's SS (4) pin LOW
digitalWrite(10, HIGH);       // select ethernet mode
digitalWrite(4, LOW);       // deselect SD mode

  if (client.connect()) {   
    client.print("GET http://website.com/upload.php?P=");
    client.print(dataString);
    client.println();
    client.stop();
  }
 else {
    Serial.println(" ***************** Failed to connect to client ***************** ");
  }



// ----------------------------Save the data in the SD -------------------------------------------
//To use the SD card, you set its SS pin HIGH and the Ethernet shield's SS pin LOW
if (SDavailable != false){
digitalWrite(10, LOW);       // deselect ethernet mode
digitalWrite(4, HIGH);       // select SD mode

// open the file. note that only one file can be open at a time, so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  } 
}
// ---------------- end of saving data to SD -----------------------------

delay(10000);
}

ok i have been reading a lot on SPI and learn some new stuff.
so please let me know if I got this right. is this correct?

 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 * Ethernet chip attached to SPI bus as follows:
 ** SS - pin 10 (by default)

so when doing

digitalWrite(10, HIGH);       // deselect ethernet mode
digitalWrite(4, LOW);       // select SD mode

you are saying, dont use slave ethernet (on pin 10 set to HIGH) and use slave SD (on pin 4 set to LOW)
by default pin 10.
(i have learn that to enable the module it must be set to LOW, not to HIGH as i did on the code before.)

so thanks for any feedback. i think i am getting to understand all this, just need an extra bit.

if anyone has an ethernet shield to try this, i dont have mine available so its really frustrating not seeing if it actually works. :stuck_out_tongue:

//Libraries
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
/*
 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 * Ethernet chip attached to SPI bus as follows:
 ** SS - pin 10 (by default)
*/
// to deselect a peripherial (SD or ethernet) put HIGH to its CS pin.

//Server IP address
byte server[] = { 111,111,111,111};   // Godaddy Server
//Setup a client
Client client(server, 80);
byte ip[] = { 192,168,1,69 };  // Using a Router
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte gateway[] = { 192, 168, 1, 1 };   //your router's IP address


    boolean SDavailable;

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int SDchipSelect = 4;
const int ETHERNETchipSelect = 10;

void setup()
{
// --------  Start ethernet --------------------------------
Ethernet.begin(mac, ip, gateway);     //, subnet);
// ----------- end setup ethernet --------------------------


//------------ start of setup SD -------------------------------
//To use the SD card, you set its SS pin HIGH and the Ethernet shield's SS pin LOW
  Serial.print("Initializing SD card...");
  // Set pin modes ...
  pinMode(ETHERNETchipSelect, OUTPUT);    // make sure that the default chip select pin is set to output, even if you don't use it:
  pinMode(SDchipSelect, OUTPUT);      // On the Ethernet Shield, CS is pin 4

  // see if the card is present and can be initialized:
  if (!SD.begin(SDchipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    //return;
    SDavailable =  false;  // set a flag to indicate that the SD is not available so that the code in the loop knows.
  }
  Serial.println("card initialized.");
//------------ end of setup SD -------------------------------

// ---- start setup SERIAL PORT --------------------------------------------------
Serial.begin(9600); 
Serial.println(" ********************** Arduino Started ********************** ");
// ---- end setup SERIAL PORT --------------------------------------------------
}



void loop() {
// ++++++++++++++++++ GET SOME DATA ++++++++++++++++++++++++++
 // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ","; 
    }
  }
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

sendtoethernet(dataString);

sendtoSD(dataString);

delay(10000);

}


void sendtoethernet(String dataString){
  // ----------------------------Send the data via ethernet ----------------------------
//set its SS pin (10) HIGH, and the SD card's SS (4) pin LOW
digitalWrite(ETHERNETchipSelect, LOW);       // select ethernet mode -> LOW selects, HIGH deselects module
digitalWrite(SDchipSelect, HIGH);       // deselect SD mode -> LOW selects, HIGH deselects module

  if (client.connect()) {   
    client.print("GET http://website.com/upload.php?P=");
    client.print(dataString);
    client.println();
    client.stop();
  }
 else {
    Serial.println(" ***************** Failed to connect to client ***************** ");
  }
}


void sendtoSD(String dataString){
// ----------------------------Save the data in the SD -------------------------------------------
if (SDavailable != false){
//To use the SD card, you set its SS pin HIGH and the Ethernet shield's SS pin LOW
digitalWrite(ETHERNETchipSelect, HIGH);       // deselect ethernet mode-> LOW selects, HIGH deselects module
digitalWrite(SDchipSelect, LOW);           // select SD mode-> LOW selects, HIGH deselects module

// open the file. note that only one file can be open at a time, so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  } 

}
// ---------------- end of saving data to SD -----------------------------
}

I am also trying to get this to work, but experiencing some problems. I have arduino0022 and official UNO and ethernet shield. Sorry Gixxer, I was going to try your code but it has some godaddy stuff which I did not want to get involved in.

Separately the microSD and ethernet work just fine, it is the mid-programme switch which does not work.

In the following code (borrowed heavily from the Ladyada tutorial), I make a call to the SD card to see if I can read the file, which works ok, then switch on the ethernet which starts responding ok. Then when I use the browser to open the root it just prints "Files" (good, that is all it is supposed to do) but when I specify a file in the url the code is supposed to drop the ethernet, switch to micro SD and print the file contents just as it did successfully in the setup, the file cannot open.

/*
  SD card read/write
 
 This example shows how to read and write data to and from an SD card file 	
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 
 created   Nov 2010
 by David A. Mellis
 updated 2 Dec 2010
 by Tom Igoe
 mods by Colin Beckingham 2011
 
 This example code is in the public domain.
 	 
 */
 
#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>

/************ ETHERNET STUFF ************/
byte mac[] = { 0x... };
byte ip[] = { 192, ... };
Server server(80);

/************ SD STUFF ************/
File myFile;

/************ Various *************/
int goon = 1;

void setup()
{
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin 
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output 
  // or the SD library functions will not work. 
  //digitalWrite(10, HIGH); // turn off the W5100 chip!
  switch2microSD();
   
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  
  runtest();
  
  if (goon == 1) {
    switch2eth();
    // Debugging complete, we start the server!
    Ethernet.begin(mac, ip);
    server.begin();
  }
  
}


void runtest() {
  // open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("Test opening test.txt:");
    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
    goon = 1;
  } else {
    // if the file didn't open, print an error:
    Serial.println("Error opening test.txt");
    goon = 0;
  }
}

void switch2microSD() {
  //
  digitalWrite(10,HIGH);
  digitalWrite(4,LOW);
  delay(5);
}

void switch2eth() {
  //
  digitalWrite(10,LOW);
  digitalWrite(4,HIGH);
  delay(5);
}

#define BUFSIZ 100

void loop()
{
  char clientline[BUFSIZ];
  int index = 0;
  //
  Client client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // If it isn't a new line, add the character to the buffer
        if (c != '\n' && c != '\r') {
          clientline[index] = c;
          index++;
          // are we too big for the buffer? start tossing out data
          if (index >= BUFSIZ)
            index = BUFSIZ -1;
          // continue to read more data!
          continue;
        }
        // got a \n or \r new line, which means the string is done
        clientline[index] = 0;
        // Print it out for debugging
        Serial.println(clientline);
        // Look for substring such as a request to get the root file
        if (strstr(clientline, "GET / ") != 0) {
          Serial.print("1 - ");
          Serial.println(clientline);
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<h2>Files:</h2>");
        } else if (strstr(clientline, "GET /") != 0) {
          Serial.print("2 - ");
          Serial.println(clientline);
          // standard http response header + info
          char *filename;
          filename = clientline+5;
          (strstr(clientline, " HTTP"))[0] = 0;
          Serial.println(filename);
          //
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<h3>Specific file</h3>");
          client.println(filename);
          client.println("<hr />");
          // now get the file contents          
          switch2microSD();
          myFile = SD.open("test.txt");
          if (myFile) {
            Serial.println("Reopening test.txt:");
            // read from the file until there's nothing else in it:
            while (myFile.available()) {
    	    Serial.write(myFile.read());
            }
            myFile.close();
            switch2eth();
          } else {
            Serial.println("File open failed");
          }
          //
        } else {
          Serial.print("3 - ");
          Serial.println(clientline);
          // everything else is a 404
          client.println("HTTP/1.1 404 Not Found");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<h2>File Not Found!</h2>");
        }
        break;
      }
    }
    delay(1);
    client.stop();
  } else {
    // Serial.print(".");
  }
}

Here is the sample from the serial output:

InitInitializing SD card...initialization done.

Test opening test.txt:
testing 1, 2, 3.

testing 1, 2, 3.

testing 1, 2, 3.

testing 1, 2, 3.

testing 1, 2, 3.

GET / HTTP/1.1

1 - GET / HTTP/1.1

GET /fred HTTP/1.1

2 - GET /fred HTTP/1.1

fred

File open failed

GET /fred HTTP/1.1

2 - GET /fred HTTP/1.1

fred

File open failed

actually the godaddy ip address is not correct there. i just wrote those numbers so the program could compile.

Unfortunately i will not be able to test my code till 2 weeks. but it would be great if you can tell me if its working for you.
the ethernet part is quite dificult to test as you need to create your own php file and make it do something with the value sent here
client.print("GET http://website.com/upload.php?P=");

i believe this line could point to any web site ip, as long as it working (ie:google) as you are setting the whole website path in the line above ( client.print("GET http://website.com/upload.php?P="):wink:
byte server[] = { 111,111,111,111}; // Godaddy Server

so is my version not working ? not even the SD part?

ill look at your code in about an hour.

There is a demo program showing all of this working together. The web page serving up SD files and allowing downloads etc and starting a sensor which logs to the board -- I know cause I looked at it...

Is there a reason nobody mentions it? Is it not helpful?

WillR:
There is a demo program showing all of this working together. The web page serving up SD files and allowing downloads etc and starting a sensor which logs to the board -- I know cause I looked at it...

Is there a reason nobody mentions it? Is it not helpful?

really? do you have the link or the code?

That would had saved me lots of time and headaches.

please see if you have the link

thanks a lot

WillR - is this the one you read?

http://www.ladyada.net/learn/arduino/ethfiles.html

Even though a recent contribution and a good guide to server operation, this seems to be a bit out of date, the first thing you get in Arduino0022 is that include SPI.h is required since Ethernet.h needs it.

Also the example uses #include <SdFat.h> and #include <SdFatUtil.h>, whereas there is now an SD.h used in the examples in the Arduino package. If I understand correctly SD.h is simply derived from the others, however there may be some subtle overlaps and differences that we have not taken into account.

if thats the code.

then this is a good reason to try and build a nicer and more updated one.

anyone is invited to help me out by

  • checking my code above as a first example
  • get the code above and expand it to include the functions of server etc. as described by Will

The examples I was referring to are supplied with the version 022 software.

That's why I was so surprised... I will go even further and say that many questions on the forum could be answered if everyone downloaded and set up the latest version of the software.You can stick with your old stuff if you are tied to libraries -- but maybe start writing new programs with the new compiler...

I encourage everyone not to plug in their new toy but spend two or three days (or whatever it takes) to review all the material on the forum and review this board. You don't have to read an understand all -- but when the time comes maybe you will remember that you saw something useful...

WillR:
The examples I was referring to are supplied with the version 022 software.

That's why I was so surprised... I will go even further and say that many questions on the forum could be answered if everyone downloaded and set up the latest version of the software.You can stick with your old stuff if you are tied to libraries -- but maybe start writing new programs with the new compiler...

I encourage everyone not to plug in their new toy but spend two or three days (or whatever it takes) to review all the material on the forum and review this board. You don't have to read an understand all -- but when the time comes maybe you will remember that you saw something useful...

I would be interested to know which example does what this post intended to.

  • be able to use in a simple manner, both ethernet part and SD part of the ethernet shield.

plus at the time of opening this post i was very limited in knowledge on some aspects that were pointed out in this thread and know i have been able to learn from it. hopefully giving back to the community a working example of my initial aim so that anyone on the same situation can benefit from it.

so...i dont see anything wrong in this thread, only very kind users helping out another Arduino enthusiast.

in terms of the aim of the original post...

I am currently waiting to be able to test my code when i have my arduino and ethernet shield available again (cant use it right now).

I will try to confirm the code is working + try to clean it up + and try to make it as user friendly as possible with some functions or something.

once again, thanks to the community for their feedback, you are great !!

Hey,

Take a look at the "TinyWebServer" examples. Google it as you need to download them, they are not part of the default example package.
Within the TinyWebServer package, is an example called "FileUpload", which does what you want.

However, the code isn't very user friendly, your approach, if it works, seems way easier.

Let us know.

aboshan excellent contribution !!
i am going to try to make my version work with the new information you have me, because as you said, it looks much easier to me also

im not sure if this code will work as although it compiles, i am not able to test it yet.
just in case anyone is interested. i will post a final code when i am able to test it and its working

//Libraries
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
/*
 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 * Ethernet chip attached to SPI bus as follows:
 ** SS - pin 10 (by default)
*/
// to deselect a peripherial (SD or ethernet) put HIGH to its CS pin.

//Server IP address
byte server[] = { 173,194,33,104 }; // Google
//Setup a client
Client client(server, 80);
byte ip[] = { 192,168,1,69 };  // Using a Router
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte gateway[] = { 192, 168, 1, 1 };   //your router's IP address


    boolean SDavailable;

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int SDchipSelectPin = 4;
const int ETHERNETchipSelectPin = 10;

void setup()
{
// ---- start setup SERIAL PORT --------------------------------------------------
Serial.begin(9600); 
Serial.println(" ********************** Arduino Started ********************** ");
// ---- end setup SERIAL PORT --------------------------------------------------


// ------------ Set pin modes ... ----------------------------------------------
  pinMode(ETHERNETchipSelectPin, OUTPUT);    // pin 10 -> make sure that the default chip select pin is set to output, even if you don't use it:  pinMode(10, OUTPUT); // set the SS pin as an output (necessary!)
  pinMode(SDchipSelectPin, OUTPUT);      //    pin 4 -> On the Ethernet Shield, CS is pin 4  pinMode(4, OUTPUT); // set the SS pin as an output (necessary!)
  // both units are turned off here.
  //digitalWrite(10, HIGH); // but turn off the W5100 chip!
  //digitalWrite(4, HIGH); // but turn off the SD card functionality!
// ------------ End Set pin modes ... -------------------------------------------

  
// --------  Start ethernet --------------------------------
  digitalWrite(ETHERNETchipSelectPin, LOW); // turn on the W5100 chip! by default it will be ON, so unnecessary.
  digitalWrite(SDchipSelectPin, HIGH); // but turn off the SD card functionality!
  Ethernet.begin(mac, ip, gateway);     //, subnet);
// ----------- end setup ethernet --------------------------


//------------ start of setup SD -------------------------------
  // initialize the SD card
  Serial.println("Setting up SD card...");    	//To use the SD card, you set its SS pin HIGH and the Ethernet shield's SS pin LOW
    digitalWrite(ETHERNETchipSelectPin, HIGH); 	// turn off the W5100 chip!
	digitalWrite(SDchipSelectPin, LOW); 		// turn on the SD chip!
	
  Serial.print("Initializing SD card...");
  // see if the card is present and can be initialized:
  if (!SD.begin(SDchipSelectPin)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    //return;
    SDavailable =  false;  // set a flag to indicate that the SD is not available so that the code in the loop knows.
  }
  Serial.println("card initialized.");
//------------ end of setup SD -------------------------------


}

void loop() {
// ++++++++++++++++++ GET SOME DATA ++++++++++++++++++++++++++
 // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ","; 
    }
  }
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

sendtoethernet(dataString);

sendtoSD(dataString);

delay(10000);

}


void sendtoethernet(String dataString){
  // ----------------------------Send the data via ethernet ----------------------------
//set its SS pin (10) HIGH, and the SD card's SS (4) pin LOW
digitalWrite(ETHERNETchipSelectPin, LOW);       // select ethernet mode -> LOW selects, HIGH deselects module
digitalWrite(SDchipSelectPin, HIGH);       // deselect SD mode -> LOW selects, HIGH deselects module

  if (client.connect()) {   
    client.print("GET http://website.com/upload.php?P=");
    client.print(dataString);
    client.println();
    client.stop();
  }
 else {
    Serial.println(" ***************** Failed to connect to client ***************** ");
  }
}


void sendtoSD(String dataString){
// ----------------------------Save the data in the SD -------------------------------------------
if (SDavailable != false){
//To use the SD card, you set its SS pin HIGH and the Ethernet shield's SS pin LOW
digitalWrite(ETHERNETchipSelectPin, HIGH);       // deselect ethernet mode-> LOW selects, HIGH deselects module
digitalWrite(SDchipSelectPin, LOW);           // select SD mode-> LOW selects, HIGH deselects module

// open the file. note that only one file can be open at a time, so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  } 

}
// ---------------- end of saving data to SD -----------------------------
}

// to be implemented later
/*
void switch2microSD() {
  //
  digitalWrite(10,HIGH);
  digitalWrite(4,LOW);
  delay(5);
}

void switch2eth() {
  //
  digitalWrite(10,LOW);
  digitalWrite(4,HIGH);
  delay(5);
}
*/