TH_ftp.zip



http://cafe.naver.com/arduinostory/31761


http://swa.esy.es/

http://31.170.164.122


#include <SPI.h>

#include <Ethernet.h>

#include <minWire.h>

#include <minRTClib.h>

#include <dht.h>



//-------------------------------------DS-1307

RTC_DS1307 rtc;

DateTime  tod;


//-------------------------------------DHT-11

#define DHT11_PIN A0

#define UPLOAD_INTERVAL (uint32_t)60*1000     //1�п� �ѹ� sesnor read. Arduino default�� 16bit int, (uint32_t)�� ���صθ� -5536���� �ǹ�����.


dht DHT;


float humidity;

float temperature;


void DHTPoll();

//-------------------------------------Ethernet

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x25, 0xC4 };


IPAddress server(31, 170, 164, 122);             //   FTP 서버 IP 입니다. 

EthernetClient client;

EthernetClient dclient;


char outBuf[128];

char outCount;


//-------------------------------------Sensor Identification

char fileName[32];

char sectorID[8] = "T0001";

char sensorID[8] = "S0001";


//-------------------------------------working

uint32_t lastPollTime = 0;


/*-----------------------------------------------------------------------------

   S E T  U P

  -----------------------------------------------------------------------------*/

void setup(){

  Serial.begin(9600);

  Wire.begin();

  rtc.begin();

  Ethernet.begin(mac);

  //rtc.adjust(DateTime(2014, 9, 27, 15, 02, 0));

}

/*-----------------------------------------------------------------------------

   L O O P

  -----------------------------------------------------------------------------*/

void loop(){


  //for test

  if(Serial.read() != -1) DHTPoll();


  if((millis() - lastPollTime) >= UPLOAD_INTERVAL){

    lastPollTime = millis();

    DHTPoll();

  }

}

/*-----------------------------------------------------------------------------

   D H T - 1 1   h a n d l e r

  -----------------------------------------------------------------------------*/

void DHTPoll() {

  if(DHT.read11(DHT11_PIN) == DHTLIB_OK) {

    humidity = DHT.humidity;

    temperature = DHT.temperature;

    makeSensorLog();


    if(doFTP()) Serial.println(F("FTP Upload OK"));

    else Serial.println(F("FTP Upload FAIL"));

  }

}


/*-----------------------------------------------------------------------------

   F T P  File Form


  SectorId_YYMMDD.log


    <SensorID> hh:mm tt.tC hh.h%


    tt.t : temperature

    hh.h : humidity

  -----------------------------------------------------------------------------*/


/*-----------------------------------------------------------------------------

   F T P  Upload

  -----------------------------------------------------------------------------*/

byte doFTP(){


  //--------------------

  // command port connection (21)

  //--------------------

  if (client.connect(server,21)) {

    Serial.println(F("Command connected"));

  } 

  else {

    Serial.println(F("Command connection failed"));

    return 0;

  }

  if(!eRcv(0)) return 0;


  //--------------------

  // USER

  //--------------------

  client.println(F("USER swa"));         //사용자 아이디

  if(!eRcv(0)) return 0;


  //--------------------

  // PASS

  //--------------------

  client.println(F("PASS 951753"));                //사용자 비밀번호

  if(!eRcv(0)) return 0;


  //--------------------

  // CWD

  // goto log folder

  //--------------------

  client.println(F("CWD /Temp_Humidity_log/"));        //파일을 저장할 경로

  if(!eRcv(0)) return 0;


  //--------------------

  // SIZE

  // check file exist

  //--------------------

  makeLogFileName();

  client.print(F("SIZE "));

  client.println(fileName);

  byte openType = eRcv(1);


  //--------------------

  // PASV

  // Passive mode, get data port number

  //--------------------

  client.println(F("PASV"));

  if(!eRcv(0)) 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);


  //--------------------

  // Upload

  //--------------------

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

    Serial.println(F("Data connected"));

  } 

  else {

    Serial.println(F("Data connection failed"));

    client.stop();

    return 0;

  }


  if(openType) client.print(F("APPE "));  

  else client.print(F("STOR "));

  

  client.println(fileName);

  if(!eRcv(0)){

    dclient.stop();

    return 0;

  }


  Serial.println(F("Writing"));


  makeSensorLog();

  dclient.write(outBuf,strlen(outBuf));


  dclient.stop();

  Serial.println(F("Data disconnected"));

  if(!eRcv(0)) return 0;


  client.println(F("QUIT"));

  if(!eRcv(0)) return 0;


  client.stop();

  Serial.println(F("Command disconnected"));


  return 1;

}


byte eRcv(byte errRet){

  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')

  {

    if(!errRet)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"));

}


//-----------------------------------------------------------------------------

void makeLogFileName(void){


  int i;

  char *pc = fileName;


  for(i=0;i<strlen(sectorID);i++){

    *pc++ = sectorID[i];

  }

  *pc++ = '_';


  DateTime now = rtc.now();

  simpleInt2Arry((byte)(now.year() - 2000), pc);

  pc += 2;

  simpleInt2Arry(now.month(), pc);

  pc += 2;  

  simpleInt2Arry(now.day(), pc);  

  pc += 2;    

  *pc++ = '.';

  *pc++ = 'l';

  *pc++ = 'o';

  *pc++ = 'g';  

  *pc = 0x00;    

}

//-----------------------------------------------------------------------------

void makeSensorLog(void){

  byte i;


  char *pc = outBuf;


  *pc++ = '<';

  for(i=0;i<strlen(sensorID);i++){

    *pc++ = sensorID[i];

  }

  *pc++ = '>';  

  *pc++ = ' ';


  DateTime now = rtc.now();

  simpleInt2Arry(now.hour(), pc);

  pc += 2;  

  *pc++ = ':';

  simpleInt2Arry(now.minute(), pc);  

  pc += 2;    

  *pc++ = ' ';


  dtostrf(temperature, 5, 1, (char *)pc);  //-12.3C

  pc = outBuf + strlen(outBuf);

  *pc++ = 'C';

  *pc++ = ' ';  


  dtostrf(humidity, 5, 1, (char *)pc);  //100.3%

  pc = outBuf + strlen(outBuf);

  *pc++ = '%';


  *pc++ = 0x0d;  

  *pc++ = 0x0a;  

  *pc = 0x00;  

}  


//-----------------------------------------------------------------------------

void simpleInt2Arry(byte i, char* pc){

  *(pc + 1) = (i % 10) + 0x30;

  i /= 10;

  *pc = (i % 10) + 0x30;

}