I am making a web-client with an Arduino mega and Ethernet Shield. I am trying to receive data from a web-server running a php script and saving that data to the SD card located in the Arduino Ethernet shield. I have separately tested to read and write to SD and to Get data from the the php script using the Ethernet. But when i tried to merge those two code into one, i found that Ethernet is unable to connect to the web-server(php Script).But the Ethernet connects fine when i remove the SD Card from its slot. During my research i found that Arduino mega has different SPI pin configuration that the uno. And the libraries are supposed to handle the SPI data-line independently. But i still tried to manually controlled the SPI bus, but it did not work. As far as i know, i am now unable to change the SPI bus using the D4 pin for SD card and D10 pin for Ethernet.
I am using native Ethernet library and SDFat library.(I Even used the native SD library but the result is the same).
#include <Ethernet.h>
#include <SPI.h>
#include <SdFat.h>
#define Ethernet_ss_pin 53
#define Sd_ss_pin 4
// On the Ethernet Shield, CS is pin 4. SdFat handles setting SS
/////////////////////////////////////////////////////////////////////////////////
////////SD Config
/////////////////////////////////////////////////////////////////////////////////
SdFat sd;
SdFile myFile;
//const int chipSelect = 4;
const char* fileName="database.txt";
/////////////////////////////////////////////////////////////////////////////////
////////Ethernet Config
/////////////////////////////////////////////////////////////////////////////////
//String server = "rfid.unisec-bd.org"; //ip Address of the server you will connect to
//The location to go to on the server
//make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is
String location = "/getData.php HTTP/1.0";
// if need to change the MAC address (Very Rare)
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
////////////////////////////////////////////////////////////////////////
EthernetClient client;
char inString[3200]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?
void setup() {
Serial.begin(9600);
pinMode(53,OUTPUT);
pinMode(4, OUTPUT);
pinMode(10,OUTPUT);
ChipSelect(0);
Ethernet.begin(mac);
String pageValue;
pageValue=connectAndRead(); //connect to the server and read the output
Serial.println(pageValue); //print out the findings.
ChipSelect(1);
// Initialize SdFat or print a detailed error message and halt
// Use half speed like the native library.
// change to SPI_FULL_SPEED for more performance.
if (!sd.begin(Sd_ss_pin, SPI_HALF_SPEED)) {
sd.initErrorHalt("Sd initialization failed");
}else{
Serial.println("Sd card Initialized");
}
ChipSelect(0);
}
void loop() {
Serial.println("Type any character to start");
while (Serial.read() <= 0) {}
delay(1000); // catch Due reset problem
ChipSelect(2);
String pageValue = connectAndRead(); //connect to the server and read the output
Serial.println(pageValue); //print out the findings.
ChipSelect(1);
writeSd(pageValue);
readSd();
ChipSelect(0);
}
void readSd(){
//const char path=(const char)fileName;
if (!myFile.open(fileName, O_READ)) {
sd.errorHalt("file read error");
}
Serial.println(fileName);
// read from the file until there's nothing else in it:
char data;
int c=0;
while ((data = myFile.read()) >= 0) {
data=char(data);
Serial.print(data);
//Serial.println(c);
c++;
}
// close the file:
myFile.close();
}
void writeSd(String storeData){
// open the file for write at end like the Native SD library
if (!myFile.open(fileName, O_RDWR | O_CREAT | O_AT_END)) {
sd.errorHalt("opening database.txt for write failed");
}
// if the file opened okay, write to it:
Serial.print("Writing to database.txt...");
myFile.println(storeData);
// close the file:
myFile.close();
Serial.println("done.");
}
String connectAndRead(){
///////// Pin 10,11,12,13 are used as SPI Bus of the ethernet Shield
/// MOSI- pin 11
/// MISO- pin 12
/// CLK- pin 13
/// CS for sdCard-pin 4
/// ss- pin 10
// 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(Sd_ss_pin,HIGH); //SD is not active
/* USE THIS CODE FOR PROXY SERVER IN BRAC UNIVERSITY
// if you get a connection, report back via serial:
if (client.connect("192.168.1.5", 8080)) { // This is connecting to the proxy
Serial.println("connected");
// Make a HTTP request through proxy:
client.println("GET rfid.unisec-bd.org/getData.php HTTP/1.0");
client.println();
}
*/
//connect to the server
Serial.println("connecting...");
while(!client){;}
//port 80 is typical of a www page
if (client.connect("rfid.unisec-bd.org", 80)) {
Serial.println("connected");
client.print("GET ");
client.println(location);
client.println("Host: rfid.unisec-bd.org"); // SERVER ADDRESS HERE TOO
client.println("Content-Type: application/x-www-form-urlencoded");
client.println();
//Connected - Read the page
return readPage(); //go and read the output
}else{
return "connection failed";
}
}
String readPage(){
//read the page, and capture & return everything between '<' and '>'
stringPos = 0;
memset( &inString, 0, 3200 ); //clear inString memory
while(true){
if (client.available()) {
char c = client.read();
if (c == '!' ) { //'!' is our begining character
startRead = true; //Ready to start reading the part
}else if(startRead){
if(c != '@'){ //'@' is our ending character
inString[stringPos] = c;
stringPos ++;
}else{
//got what we need here! We can disconnect now
startRead = false;
client.stop();
client.flush();
Serial.println("disconnecting.");
return inString;
}
}
}
}
}
void ChipSelect(int s){
if(s=0){ //Both Device is off
//digitalWrite(53,HIGH);
digitalWrite(10,HIGH);
digitalWrite(4,HIGH);
}else if(s=1){ //Only SD is active
//digitalWrite(53,HIGH);
digitalWrite(10,HIGH);
digitalWrite(4,LOW);
}else if(s=2){ //Only Ethernet is Active
//digitalWrite(53,HIGH);
digitalWrite(10,LOW);
digitalWrite(4,HIGH);
}
Serial.print("Ethernet SS:");
Serial.println(digitalRead(10));
Serial.print("SD SS:");
Serial.println(digitalRead(4));
}