Ciao a tutti,
ho appena acquistato un GSM shield(http://arduino.cc/en/Main/ArduinoGSMShield).
Il mio obbiettivo è riuscire a usare la GSM shield per connettermi ad un mio server FTP e uploadare dei file di log.
Usando Arduino Ethernet tutto bene(Arduino Playground - HomePage).
Invece con la GSM shield ho un grosso problema:
/*
FTP passive client for IDE v1.0.1 and w5100/w5200
Posted October 2012 by SurferTim
*/
#include <MemoryFree.h>
#include <SD.h>
#include <SPI.h>
#include <GSM.h>
// comment out next line to write to SD from FTP server
#define FTPWRITE
// PIN Number
#define PINNUMBER ""
// APN data
#define GPRS_APN "mobile.vodafone.it" // replace your GPRS APN
#define GPRS_LOGIN "" // replace with your GPRS login
#define GPRS_PASSWORD "" // replace with your GPRS password
// initialize the library instance
GPRS gprs;
GSM gsmAccess;
GSMClient client;
GSMClient dclient;
// URL, path & port (for example: arduino.cc)
//char server[] = "arduino.cc";
byte server[]= {xxxxxxxxx};
char path[] = "/";
int port = 21; // port 80 is the default for HTTP
char outBuf[30];
char outCount;
// change fileName to your file (8.3 format!)
char fileName[13] = "test.txt";
void setup()
{
Serial.begin(9600);
if(SD.begin(4) == 0) //changed to 53 for use with mega
{
Serial.println(F("SD init fail"));
}
Serial.println("Starting Arduino web client.");
// connection state
boolean notConnected = true;
Serial.print("freeMemory()=");
Serial.println(freeMemory());
// After starting the modem with GSM.begin()
// attach the shield to the GPRS network with the APN, login and password
while(notConnected)
{
if((gsmAccess.begin()==GSM_READY) &
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY))
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("connecting...");
delay(2000);
Serial.println(F("Ready. Press f or r"));
}
void loop()
{
byte inChar;
inChar = Serial.read();
if(inChar == 'f')
{
if(doFTP()) Serial.println(F("FTP OK"));
else Serial.println(F("FTP FAIL"));
}
if(inChar == 'r')
{
readSD();
}
}
File fh;
byte doFTP()
{
fh = SD.open(fileName,FILE_READ);
/*if(!fh)
{
Serial.println(F("SD open fail"));
return 0;
}
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;
}
delay(3000);
if(!eRcv()) return 0;
client.println(F("USER xxx"));
if(!eRcv()) return 0;
client.println(F("PASS xxx"));
if(!eRcv()) return 0;
client.println(F("SYST"));
if(!eRcv()) return 0;
client.println(F("PASV"));
if(!eRcv()) return 0;
char *tStr = strtok(outBuf,"(,");
Serial.print("La risposta e ");
Serial.println(tStr);
int array_pasv[6];
Serial.println(tStr);
for ( int i = 0; i < 5; i++) {
tStr = strtok(NULL,"(,");
array_pasv[i] = atoi(tStr);
if(tStr == NULL)
{
Serial.println(F("Bad PASV Answer"));
}
}
unsigned int hiPort,loPort;
Serial.println(array_pasv[3]);
Serial.println(array_pasv[4]);
hiPort = array_pasv[3] << 8;
loPort = array_pasv[4] & 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;
}
client.print(F("STOR "));
client.println(fileName);
if(!eRcv())
{
dclient.stop();
return 0;
}
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);
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); Serial.print("NON DISPONIBILE");};
respCode = client.peek();
boolean ciao=false;
outCount = 0;
while(client.available())
{
thisByte = client.read();
Serial.write(thisByte);
if(((char)thisByte)=='(')
ciao=true;
if(ciao){
if(outCount < 29)
{
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();
}
Arduino stabilisce la prima connessione, invia username, password...
La connessione dati però non va a buon fine.
Mi chiedo se la libreria sia in grado di stabilire più connessioni simultanee e se ho sbagliato qualcosa in questa unione veloce di codice. Spero di avere più risposte di lui(GSM Shield and FTP - Networking, Protocols, and Devices - Arduino Forum)...