Arduino Mega and Official Wifi shield compatibility

hi guys! i have a question about the compatibility between Arduino mega 2560 board and official wifi shield.
This code is FTP code made by SurferTim and is adapted for Wifi Shield.
http://playground.arduino.cc/Code/FTP

because of the memory problem, i wanna use this code on the Mega board instead of uno board.

when i connected mega board and official wifi shield and compile it, the serial monitor says “SD open fails”. i think this is because of the pin differences when i use uno and mega

so i followed this rule.
“This is on digital pins 11, 12, and 13 on the Uno and pins 50, 51, and 52 on mega.”

i connected wifi pin 11 to mega 50, wifi pin 12 to mega 51 and wifi pin 13 to mega 52.
but it does not work also.

So, if you guys have any advice, please, just let me know
any advice would be appreciated.
Thank you very much!
Below is the code that i am trying to use!

#include <WiFi.h>
#include <SD.h>
#include <SPI.h>
// comment out next line to write to SD from FTP server
#define FTPWRITE

char fileName[13] = “REC00000,wav”;

char ssid = “dongmin”; // your network SSID (name)
char pass = “12345678”; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio’s status

// change to your server
IPAddress server( 192, 168, 0, 0 );

WiFiClient client;
WiFiClient dclient;

char outBuf[128];
char outCount;

// change fileName to your file (8.3 format!)

void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
//while (!Serial) {
//; // wait for serial port to connect. Needed for Leonardo only
//}

pinMode(10,OUTPUT);
digitalWrite(10,HIGH);

if(SD.begin(4) == 0)
{
Serial.println(F(“SD init fail”));
}

// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println(“WiFi shield not present”);
// don’t continue:
while(true);
}

// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);

// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);

// wait 10 seconds for connection:
delay(10000);

// you’re connected now, so print out the data:
Serial.print("You’re connected to the network ");
printCurrentNet();
printWifiData();
Serial.print(“FTP server:”);
Serial.println(server);
Serial.println(F(“Ready. Press f or r”));
}
}

void loop() {

// check the network connection once every 10 seconds:
//delay(10000);
//printCurrentNet();

byte inChar;

inChar = Serial.read();

if(inChar == ‘f’)
{
if(doFTP()) Serial.println(F(“FTP OK”));
else Serial.println(F(“FTP FAIL”));
}
}

File fh;

byte doFTP()
{
#ifdef FTPWRITE
fh = SD.open(fileName,FILE_READ);
#else
SD.remove(fileName);
fh = SD.open(fileName,FILE_WRITE);
#endif

if(!fh)
{
Serial.println(F(“SD open fail”));
return 0;
}

#ifndef FTPWRITE
if(!fh.seek(0))
{
Serial.println(F(“Rewind fail”));
fh.close();
return 0;
}
#endif

Serial.println(F(“SD opened”));

if (client.connect(server,21)) {
Serial.println(F(“Command connected”));
}
else {
fh.close();
Serial.println(F(“Command connection failed”));
return 0;
}

if(!eRcv()) return 0;

client.println(F(“USER dongmin”));

if(!eRcv()) return 0;

client.println(F(“PASS 12345678”));

if(!eRcv()) return 0;

client.println(F(“SYST”));

if(!eRcv()) return 0;

client.println(F(“PASV”));

if(!eRcv()) return 0;

char *tStr = strtok(outBuf,"(,");
int array_pasv[6];
for ( int i = 0; i < 6; i++) {
tStr = strtok(NULL,"(,");
array_pasv = atoi(tStr);

  • if(tStr == NULL)*

  • {*

  • Serial.println(F(“Bad PASV Answer”)); *

  • }*

  • }*

  • unsigned int hiPort,loPort;*

  • hiPort = array_pasv[4] << 8;*

  • loPort = array_pasv[5] & 255;*

  • Serial.print(F("Data port: "));*

  • hiPort = hiPort | loPort;*

  • Serial.println(hiPort);*

  • if (dclient.connect(server,hiPort)) {*

  • Serial.println(F(“Data connected”));*

  • }*

  • else {*

  • Serial.println(F(“Data connection failed”));*

  • client.stop();*

  • fh.close();*

  • return 0;*

  • }*
    #ifdef FTPWRITE

  • client.print(F("STOR "));*

  • client.println(fileName);*
    #else

  • client.print(F("RETR "));*

  • client.println(fileName);*
    #endif

  • if(!eRcv())*

  • {*

  • dclient.stop();*

  • return 0;*

  • }*
    #ifdef FTPWRITE

  • Serial.println(F(“Writing”));*

  • byte clientBuf[64];*

  • int clientCount = 0;*

  • while(fh.available())*

  • {*

  • clientBuf[clientCount] = fh.read();*

  • clientCount++;*

  • if(clientCount > 63)*

  • {*

  • dclient.write(clientBuf,64);*

  • clientCount = 0;*

  • }*

  • }*

  • if(clientCount > 0) dclient.write(clientBuf,clientCount);*
    #else

  • while(dclient.connected())*

  • {*

  • while(dclient.available())*

  • {*

  • char c = dclient.read();*

  • fh.write(c); *

  • Serial.write(c);*

  • }*

  • }*
    #endif

  • dclient.stop();*

  • Serial.println(F(“Data disconnected”));*

  • if(!eRcv()) return 0;*

  • client.println(F(“QUIT”));*

  • if(!eRcv()) return 0;*

  • client.stop();*

  • Serial.println(F(“Command disconnected”));*

  • fh.close();*

  • Serial.println(F(“SD closed”));*

  • return 1;*
    }
    byte eRcv()
    {

  • byte respCode;*

  • byte thisByte;*

  • while(!client.available()) delay(1);*

  • respCode = client.peek();*

  • outCount = 0;*

  • while(client.available())*

  • { *

  • thisByte = client.read(); *

  • Serial.write(thisByte);*

  • if(outCount < 127)*

  • {*

  • outBuf[outCount] = thisByte;*

  • outCount++; *

  • outBuf[outCount] = 0;*

  • }*

  • }*

  • if(respCode >= ‘4’)*

  • {*

  • efail();*

  • return 0; *

  • }*

  • return 1;*
    }
    void efail()
    {

  • byte thisByte = 0;*

  • client.println(F(“QUIT”));*

  • while(!client.available()) delay(1);*

  • while(client.available())*

  • { *

  • thisByte = client.read(); *

  • Serial.write(thisByte);*

  • }*

  • client.stop();*

  • Serial.println(F(“Command disconnected”));*

  • fh.close();*

  • Serial.println(F(“SD closed”));*
    }
    void readSD()
    {

  • fh = SD.open(fileName,FILE_READ);*

  • if(!fh)*

  • {*

  • Serial.println(F(“SD open fail”));*

  • return; *

  • }*

  • while(fh.available())*

  • {*

  • Serial.write(fh.read());*

  • }*

  • fh.close();*
    }
    void printWifiData() {

  • // print your WiFi shield’s IP address:*

  • IPAddress ip = WiFi.localIP();*

  • Serial.print("IP Address: ");*

  • Serial.println(ip);*

  • // print your MAC address:*

  • byte mac[6]; *

  • WiFi.macAddress(mac);*

  • Serial.print("MAC address: ");*

  • Serial.print(mac[5],HEX);*

  • Serial.print(":");*

  • Serial.print(mac[4],HEX);*

  • Serial.print(":");*

  • Serial.print(mac[3],HEX);*

  • Serial.print(":");*

  • Serial.print(mac[2],HEX);*

  • Serial.print(":");*

  • Serial.print(mac[1],HEX);*

  • Serial.print(":");*

  • Serial.println(mac[0],HEX);*
    }
    void printCurrentNet() {

  • // print the SSID of the network you’re attached to:*

  • Serial.print("SSID: ");*

  • Serial.println(WiFi.SSID());*

  • // print the MAC address of the router you’re attached to:*

  • byte bssid[6];*

  • WiFi.BSSID(bssid); *

  • Serial.print("BSSID: ");*

  • Serial.print(bssid[5],HEX);*

  • Serial.print(":");*

  • Serial.print(bssid[4],HEX);*

  • Serial.print(":");*

  • Serial.print(bssid[3],HEX);*

  • Serial.print(":");*

  • Serial.print(bssid[2],HEX);*

  • Serial.print(":");*

  • Serial.print(bssid[1],HEX);*

  • Serial.print(":");*

  • Serial.println(bssid[0],HEX);*

  • // print the received signal strength:*

  • long rssi = WiFi.RSSI();*

  • Serial.print(“signal strength (RSSI):”);*

  • Serial.println(rssi);*

  • // print the encryption type:*

  • byte encryption = WiFi.encryptionType();*

  • Serial.print(“Encryption Type:”);*

  • Serial.println(encryption,HEX);*

  • Serial.println();*
    }

I had no problems with the Ethernet or wifi shield on the mega.

What's this ? pinMode(10,OUTPUT); digitalWrite(10,HIGH);

And this REC00000,wav Since when do we use commas for file extensions.

The wifi shield does not use D11 to D13. The SPI data lines are on the ICSP pins only, just like the ethernet shield.

edit: But that doesn't matter. The wifi shield is not capable of having two connections open simultaneously. That is required to open a FTP command and data channel. http://forum.arduino.cc/index.php?topic=225734.msg1633820#msg1633820

mistergreen: I had no problems with the Ethernet or wifi shield on the mega.

What's this ? pinMode(10,OUTPUT); digitalWrite(10,HIGH);

That is required on a Mega to avoid a SPI bus collision with the ethernet or wifi IC while starting the SD card first.