ftp по wifi
- Войдите на сайт для отправки комментариев
Привет всем.
Стоит задача передачи файлов с Ардуино на компьютер с Линукс (Raspberry Pi) через wifi по протоколу ftp. Имеется: Arduino IDE 1.0.5, Arduino UNO R3, WiFi Shield, скетч, который умеет подключаться к wifi и передавать файл на сервер. Вернее, скетч должен это уметь, на деле он присоединяется, открывает ftp-сессию, логинится на сервер, начинает писать файл, потом передача обрывается с сообщением "Data disconnected". Файл на сервере создаётся, но не всё содержимое исходного файла там оказывается: исходный файл 7558 байт (текст скетча), а на сервере получается файл от 6560 до 7558 (2 раза из четырнадцати попыток) байт. Текст скетча в полученном файле получается с отрезанным финалом, размер потерянного текста случайно изменяется от попытки к попытке.
Прошивка WiFi Shild'а обновлена, загружена прошивка, скачанная в комплекте с IDE.
Что может быть причиной таких результатов?
Скетч:
/*
This sketch connects to a Wifi network.
Then it prints the MAC address of the Wifi shield,
the IP address obtained, and other network details.
Then transfer file from SD card on ftp server (if press f)
Circuit:
* WiFi shield attached
created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
by Tom Igoe -- WiFi part
October 2012 by SurferTim -- ftp part
October 2013 by Pashkot -- put the parts together (Arduino IDE 1.0.5, Arduino UNO R3)
*/
#include <WiFi.h>
#include <SD.h>
#include <SPI.h>
// comment out next line to write to SD from FTP server
#define FTPWRITE
char ssid[] = "dlink38"; // your network SSID (name)
char pass[] = "mypassowrd"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
// change to your server
IPAddress server( 192, 168, 0, 106 );
WiFiClient client;
WiFiClient dclient;
char outBuf[128];
char outCount;
// change fileName to your file (8.3 format!)
char fileName[13] = "test.txt";
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"));
}
if(inChar == 'r')
{
readSD();
}
}
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 pi"));
if(!eRcv()) return 0;
client.println(F("PASS myftppassword"));
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();
}
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();
}
Результат работы:
Attempting to connect to WPA SSID: dlink38 You're connected to the network SSID: dlink38 BSSID: F0:7D:68:9F:E:5A signal strength (RSSI):-24 Encryption Type:4 IP Address: 192.168.0.107 MAC address: 78:C4:E:2:9:98 FTP server:192.168.0.106 Ready. Press f or r SD opened Command connected 220 (vsFTPd 2.3.5) 331 Please specify the password. 230 Login successful. 215 UNIX Type: L8 227 Entering Passive Mode (192,168,0,7,30,106). Data port: 7786 Data connected 150 Ok to send data. Writing Data disconnected
Эту задачу обсуждали здесь: http://forum.arduino.cc/index.php?PHPSESSID=iaksno6frg3h7n3jlno0dkdjj1&topic=193382.0 , но результатов нет.
На Arduino Mega2560 тот же результат
есть старый ftp сервер. ftp.tratata.com.uaб при входе просит пароль и логинб как ему это выдать ???
плюс при подключениии мой адрес сервера скорее всего измениться на что-то типа ftp://admin@ftp.tratata.com.ua