i have two code. camera code and FTP code. It works separately.But taken together, it is error.
#include <Adafruit_VC0706.h>
#include <SD.h>
#include <SoftwareSerial.h>
#include <SPI.h>
#include <Ethernet.h>
#define FTPWRITE
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xBD, 0xB9 };
// change to your network settings
IPAddress ip( 192, 168, 1, 5 );
IPAddress gateway( 192, 168, 1, 80 );
IPAddress subnet( 255, 255, 255, 0 );
// FTP server ip
IPAddress server( 192, 168, 1, 80 );
byte doFTP();
byte eRcv();
EthernetClient client;
EthernetClient dclient;
EthernetClient pclient;
char outBuf[128];
char outCount;
char fileName[13];
const int chipSelect = 4;
SoftwareSerial cameraconnection(2, 3);
Adafruit_VC0706 cam = Adafruit_VC0706(&cameraconnection);
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, ip, gateway, gateway, subnet);
delay(2000);
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}
if (cam.begin()) {
Serial.println("Camera Found:");
} else {
Serial.println("No camera found?");
return;
}
char *reply = cam.getVersion();
if (reply == 0) {
Serial.print("Failed to get version");
} else {
Serial.println("-----------------");
Serial.print(reply);
Serial.println("-----------------");
}
cam.setImageSize(VC0706_640x480);
uint8_t imgsize = cam.getImageSize();
Serial.print("Image size: 640x480");
Serial.println("Snap in 1 secs...");
delay(1000);
if (! cam.takePicture())
Serial.println("Failed to snap!");
else
Serial.println("Picture taken!");
char filename[13];
strcpy(filename, "IMAGE00.JPG");
for (int i = 0; i < 100; i++) {
filename[5] = '0' + i/10;
filename[6] = '0' + i%10;
if (! SD.exists(filename)) {
break;
}
}
File imgFile = SD.open(filename, FILE_WRITE);
uint16_t jpglen = cam.frameLength();
Serial.print("Storing ");
Serial.print(jpglen, DEC);
Serial.print(" byte image.");
int32_t time = millis();
byte wCount = 0; // For counting # of writes
while (jpglen > 0) {
uint8_t *buffer;
uint8_t bytesToRead = min(32, jpglen); // change 32 to 64 for a speedup but may not work with all setups!
buffer = cam.readPicture(bytesToRead);
imgFile.write(buffer, bytesToRead);
if(++wCount >= 64) { // Every 2K, give a little feedback so it doesn't appear locked up
Serial.print('.');
wCount = 0;
}
jpglen -= bytesToRead;
}
imgFile.close();
//
time = millis() - time;
Serial.println("done!");
Serial.print(time); Serial.println(" ms elapsed");
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;
void FFTP(){
delay(100000);
doFTP();
}
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 user1\r"));
if(!eRcv()) return 0;
client.println(F("PASS 123456\r"));
if(!eRcv()) return 0;
client.println(F("SYST\r"));
if(!eRcv()) return 0;
client.println(F("PASV\r"));
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"));
if(pclient.connect(server, 80)>0) { // Se conecta al servidor
pclient.print("GET http://192.168.1.80/phpSaveBlob.php?pname="); // Envia los datos utilizando GET
pclient.print(fileName);
Serial.println(fileName);
pclient.println(" HTTP/1.0");
pclient.println("User-Agent: Arduino 1.0");
pclient.println();
Serial.println("Transmission is completed");
}
else
{
Serial.println("Transmission is Faill");
}
if (pclient.connected()) {}
else {
Serial.println("Unable to connect to server");
}
pclient.stop();
pclient.flush();
delay(10000);
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();
}