I've been trying to upload a file from my SD shield to my ftp server using the GSM shield. First I tried to alter the ftp server ethernet shield code posted on the website but it doesn't work. If anyone has an answer for this please do tell. This is the code
#include <SD.h>
#include <GSM.h>
// 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 = 8;
// PIN Number
#define PINNUMBER ""
// APN data
#define GPRS_APN "epc.tmobile.com" // replace your GPRS APN
#define GPRS_LOGIN "" // replace with your GPRS login
#define GPRS_PASSWORD "" // replace with your GPRS password
// initialize the library instance:
GSMClient client;
GSMClient dclient;
GPRS gprs;
GSM gsmAccess(true);
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
// IPAddress server(216,52,233,121); // numeric IP for api.pachube.com
char server[] = "stginternational.org"; // name address for pachube API
char outBuf[128];
char outCount;
// change fileName to your file (8.3 format!)
char fileName[13] = "Test.txt";
void setup()
{
Serial.begin(9600);
Serial.print("Intializing SD Card....");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10,OUTPUT);
if(SD.begin(chipSelect) == 0)
{
Serial.println(F("SD init fail"));
//Stop Don't do anything more
return;
}
Serial.println("card initialized.");
// connection state
boolean notConnected = true;
boolean GsmConnect=true;
// After starting the modem with GSM.begin()
// attach the shield to the GPRS network with the APN, login and password
while(GsmConnect)
{
if(gsmAccess.begin(PINNUMBER)==GSM_READY)
{
GsmConnect=false;
Serial.println("Has Service");
delay(1000);
}
else
{
Serial.println("No Service");
delay(1000);
}
}
while(notConnected)
{
if(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY)
{
notConnected = false;
Serial.print("connected");
}
else
{
Serial.println("Not connected");
delay(1000);
}
}
digitalWrite(10,HIGH);
delay(2000);
Serial.println("done");
}
void loop()
{
// make a string for assembling the data to log:
String dataString = "";
// read sensors and append to the string:
dataString= "hie soon we will have sensors";
// 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");
}
Serial.println("InLoop");
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("username ftp server"));
if(!eRcv()) return 0;
client.println(F("password"));
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[i] = 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();
}