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);
}
*/