Using Camera module, FTP Client on WIZwiki-W7500, send the image data to FTP Server.

Dependencies:   CameraC328 SDFileSystem WIZnetInterface mbed-src

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SDFileSystem.h"
00003 #include "EthernetInterface.h"
00004 #include "CameraC328.h"
00005 
00006 /* CAMERA */
00007 #define USE_JPEG_HIGH_RESOLUTION  3 //1=80x64 <--- not working -_-;;, 2=160x128, 3=320x240, 4=640x480
00008 
00009 /* FTP */
00010 #define USER                "user "
00011 #define PASS                "pass "
00012 #define PASV                "pasv "
00013 #define PORT                "port "
00014 #define LIST                "list "
00015 #define STOR                "stor "
00016 #define RETR                "retr "
00017 #define END                 "\r\n"
00018 #define FTP_SERVER_PORT     21
00019 #define MAX_SS              256
00020 bool gDataSockReady = false;
00021 bool gDataPutGetStart = false;
00022 bool ftpclientrun = false;
00023 int remote_port;
00024 char ftpServer_data_ip_addr[4]={0,};
00025 char ftpServer_data_ip_addr_str[20]={0,};
00026 static char buf[256];
00027 static char ID[]={"abc"};                   //Set FTPServer Login ID
00028 static char PASSWORD[]={"123"};             //Set FTPServer Login Password
00029 enum CommandFirst {
00030     f_nocmd,
00031     f_put,
00032 };
00033 enum CommandSecond {
00034     s_nocmd,
00035     s_put,
00036 };
00037 enum ftpc_datasock_state{
00038     DATASOCK_IDLE,
00039     DATASOCK_READY,
00040     DATASOCK_START
00041 };
00042 struct Command {
00043     enum CommandFirst First;
00044     enum CommandSecond Second;
00045 };
00046 struct ftpc {
00047     enum ftpc_datasock_state dsock_state;
00048 };
00049 struct ftpc FTPClient;
00050 struct Command FTPCommand;
00051 
00052 /* SD Card filesystem */
00053 SDFileSystem sd(PB_3, PB_2, PB_1, PB_0, "sd"); // WIZwiki-W7500 
00054 static FILE *fp_jpeg;
00055 char fname[32];
00056 char fname_server[16];
00057 char fnamecnt=0;
00058 /* UART */
00059 Serial pc(USBTX, USBRX);
00060 char gMsgBuf[10];
00061 char User_Keyboard_MSG_Cnt;
00062 /* CAMERA */
00063 CameraC328 camera(PA_13, PA_14, CameraC328::Baud115200);
00064 /* Function*/ 
00065 char* User_Keyboard_MSG(void);
00066 int pportc(char * arg);
00067 void jpeg_callback(char *buf, size_t siz);
00068 void sync(void);
00069 void test_jpeg_snapshot_picture(void);
00070 
00071 int main() {
00072 
00073     char Msg_c;
00074     int remain_filesize;
00075     int send_byte;
00076     
00077     
00078     pc.baud(115200);
00079     
00080     pc.printf("Hello Home Security World!\r\n"); 
00081       
00082     uint8_t mac_addr[6] = {0x00, 0x08, 0xdc, 0x12, 0x34, 0x45};
00083     const char ip_addr[] = "192.168.0.123"; 
00084     const char mask_addr[] = "255.255.255.0"; 
00085     const char gateway_addr[] = "192.168.0.1"; 
00086     const char ftpServer_control_ip_addr[] = "192.168.0.2";
00087     
00088     
00089     EthernetInterface eth;
00090     eth.init(mac_addr, ip_addr, mask_addr, gateway_addr); //Use Static
00091     eth.connect();
00092     
00093     TCPSocketConnection FTP_CONTROL_SOCK;
00094     TCPSocketConnection FTP_DATA_SOCK;
00095     
00096     sync();
00097     //while 1
00098     while(true)
00099     {
00100         pc.printf("\r\n----------------------------------------\r\n");
00101         pc.printf("Press menu key\r\n");
00102         pc.printf("----------------------------------------\r\n");
00103         pc.printf("1> Snapshot Picture and Send FTPServer\r\n");
00104         pc.printf("----------------------------------------\r\n");
00105         Msg_c = pc.getc();
00106         if(Msg_c==0x31){
00107             
00108             test_jpeg_snapshot_picture();
00109             ftpclientrun = true;
00110             while(ftpclientrun){
00111                 //while 2
00112                 while(!FTP_CONTROL_SOCK.is_connected()){
00113                     pc.printf("Connecting...FTPServer\r\nIP:%s, PORT:%d\r\n", ftpServer_control_ip_addr, FTP_SERVER_PORT);
00114                     FTP_CONTROL_SOCK.connect(ftpServer_control_ip_addr, FTP_SERVER_PORT);
00115                     FTP_CONTROL_SOCK.set_blocking(false, 15000); // Timeout after (1.5)s
00116                 }//end of while 2
00117                 
00118                 //while 3
00119                 while(true)
00120                 {  
00121                     //gDataSockReady if
00122                     if(gDataSockReady){
00123                         gDataSockReady = false;
00124                         //FTPCommand.First switch case
00125                         switch(FTPCommand.First){
00126                             case f_put:
00127                                 FTP_CONTROL_SOCK.send(STOR, sizeof(STOR)-1);
00128                                 FTP_CONTROL_SOCK.send(fname_server, sizeof(fname_server));
00129                                 FTP_CONTROL_SOCK.send(END, sizeof(END)-1);
00130                                 break;
00131                         }//end of FTPCommand.First switch case
00132                     }//end of gDataSockReady if          
00133                     /* received from FTPServer */
00134                     int n = FTP_CONTROL_SOCK.receive(buf, sizeof(buf));
00135                     if (n <= 0) break;
00136                     buf[n] = '\0';
00137                     pc.printf("\r\nReceived message from server: '%s'\r\n", buf);
00138                     
00139                     //buf if
00140                     if (!strncmp(buf, "220", 3)){
00141                         FTP_CONTROL_SOCK.send(USER, sizeof(USER)-1);
00142                         FTP_CONTROL_SOCK.send(ID, sizeof(ID));
00143                         FTP_CONTROL_SOCK.send(END, sizeof(END)-1);
00144                     }
00145                     else if(!strncmp(buf, "331", 3)){
00146                         FTP_CONTROL_SOCK.send(PASS, sizeof(PASS)-1);
00147                         FTP_CONTROL_SOCK.send(PASSWORD, sizeof(PASSWORD));
00148                         FTP_CONTROL_SOCK.send(END, sizeof(END)-1);        
00149                     }
00150                     else if(!strncmp(buf, "230", 3)){
00151                         FTP_CONTROL_SOCK.send(PASV, sizeof(PASV)-1);
00152                         FTP_CONTROL_SOCK.send(END, sizeof(END)-1);
00153                         FTPCommand.First = f_put;    
00154                     }
00155                     else if(!strncmp(buf, "227", 3)){
00156                         if (pportc(buf) == -1){
00157                             pc.printf("Bad port syntax\r\n");
00158                         }
00159                         else{
00160                             pc.printf("Go Open Data Sock...\r\n ");
00161                             FTPClient.dsock_state = DATASOCK_READY;
00162                         }    
00163                     }
00164                     else if(!strncmp(buf, "150", 3)){
00165                         switch(FTPCommand.First){
00166                         case f_put:
00167                             FTPCommand.First = f_nocmd;
00168                             FTPCommand.Second = s_put;
00169                             gDataPutGetStart = true;
00170                             break;
00171                         }  
00172                     }
00173                     else if(!strncmp(buf, "226", 3)){
00174                         ftpclientrun = false;  
00175                         FTP_CONTROL_SOCK.close();       
00176                     }//end of buf if
00177                     
00178                     
00179                     //DATASOCK_READY if
00180                     if(FTPClient.dsock_state == DATASOCK_READY){
00181                         while(!FTP_DATA_SOCK.is_connected()){
00182                             pc.printf("Connecting...FTPServer Data sock\r\nIP:%s, PORT:%d\r\n", ftpServer_data_ip_addr_str, remote_port);
00183                             FTP_DATA_SOCK.connect(ftpServer_control_ip_addr, remote_port);
00184                             FTP_DATA_SOCK.set_blocking(false, 15000); // Timeout after (1.5)s
00185                         }
00186                         FTPClient.dsock_state = DATASOCK_IDLE;
00187                         gDataSockReady = true;
00188                     }//end of DATASOCK_READY if
00189                     
00190                     //gDataPutGetStart if
00191                     if(gDataPutGetStart){
00192                         //FTPCommand.Second switch case
00193                         switch(FTPCommand.Second){   
00194                         case s_put:
00195                             pc.printf("put waiting...\r\n");
00196                             fp_jpeg = fopen(fname, "r");  
00197                             fseek(fp_jpeg, 0, SEEK_END);            // seek to end of file
00198                             remain_filesize = ftell(fp_jpeg);       // get current file pointer
00199                             fseek(fp_jpeg, 0, SEEK_SET);            // seek back to beginning of file                
00200                             do{
00201                                 memset(buf, 0, sizeof(buf));
00202                                 if(remain_filesize > MAX_SS)
00203                                     send_byte = MAX_SS;
00204                                 else
00205                                     send_byte = remain_filesize;
00206                                 fread (buf, 1, send_byte, fp_jpeg);
00207                                 FTP_DATA_SOCK.send(buf, send_byte);
00208                                 remain_filesize -= send_byte;
00209                                 pc.printf("#");
00210                             }while(remain_filesize!=0);
00211                             fclose(fp_jpeg);
00212                             gDataPutGetStart = false; 
00213                             FTPCommand.Second = s_nocmd;     
00214                             FTP_DATA_SOCK.close(); 
00215                             break;   
00216                                 
00217                         }//end of FTPCommand.Second switch case
00218                     }//end of gDataPutGetStart if
00219                 }//end of while 3                
00220             }
00221         }
00222     }//end of while 1
00223 }
00224 char* User_Keyboard_MSG(void)
00225 {
00226     User_Keyboard_MSG_Cnt = 0;
00227     memset(gMsgBuf, 0, sizeof(gMsgBuf));
00228     do{
00229         gMsgBuf[User_Keyboard_MSG_Cnt] = pc.getc();
00230         if(gMsgBuf[User_Keyboard_MSG_Cnt]==0x08){
00231             User_Keyboard_MSG_Cnt--;
00232         }
00233         else{
00234             User_Keyboard_MSG_Cnt++; 
00235         }
00236     }while(gMsgBuf[User_Keyboard_MSG_Cnt-1]!=0x0d);
00237     return gMsgBuf;
00238 }
00239 int pportc(char * arg)
00240 {
00241     int i;
00242     char* tok=0;
00243     strtok(arg,"(");
00244     for (i = 0; i < 4; i++)
00245     {
00246         if(i==0) tok = strtok(NULL,",\r\n");
00247         else     tok = strtok(NULL,",");
00248         ftpServer_data_ip_addr[i] = (uint8_t)atoi(tok);
00249         if (!tok){
00250             pc.printf("bad pport : %s\r\n", arg);
00251             return -1;
00252         }
00253     }
00254     remote_port = 0;
00255     for (i = 0; i < 2; i++){
00256         tok = strtok(NULL,",\r\n");
00257         remote_port <<= 8;
00258         remote_port += atoi(tok);
00259         if (!tok){
00260             pc.printf("bad pport : %s\r\n", arg);
00261             return -1;
00262         }
00263     }
00264     pc.printf("ip : %d.%d.%d.%d, port : %d\r\n", ftpServer_data_ip_addr[0], ftpServer_data_ip_addr[1], ftpServer_data_ip_addr[2], ftpServer_data_ip_addr[3], remote_port);
00265     sprintf(ftpServer_data_ip_addr_str, "%d.%d.%d.%d", ftpServer_data_ip_addr[0], ftpServer_data_ip_addr[1], ftpServer_data_ip_addr[2], ftpServer_data_ip_addr[3]);
00266     return 0;
00267 }
00268 void jpeg_callback(char *buf, size_t siz) {
00269     for (int i = 0; i < (int)siz; i++) {
00270         fprintf(fp_jpeg, "%c", buf[i]);
00271     }
00272 }
00273 void sync(void) {
00274     CameraC328::ErrorNumber err = CameraC328::NoError;
00275 
00276     err = camera.sync();
00277     if (CameraC328::NoError == err) {
00278         printf("[ OK ] : CameraC328::sync\r\n");
00279     } else {
00280         printf("[FAIL] : CameraC328::sync (Error=%02X)\r\n", (int)err);
00281     }
00282 }
00283 void test_jpeg_snapshot_picture(void) {
00284     CameraC328::ErrorNumber err = CameraC328::NoError;
00285 
00286 #if (USE_JPEG_HIGH_RESOLUTION==1)
00287     err = camera.init(CameraC328::Jpeg, CameraC328::RawResolution80x60, CameraC328::JpegResolution80x64);
00288 #elif (USE_JPEG_HIGH_RESOLUTION==2)
00289     err = camera.init(CameraC328::Jpeg, CameraC328::RawResolution80x60, CameraC328::JpegResolution160x128);
00290 #elif (USE_JPEG_HIGH_RESOLUTION==3)
00291     err = camera.init(CameraC328::Jpeg, CameraC328::RawResolution80x60, CameraC328::JpegResolution320x240);
00292 #elif (USE_JPEG_HIGH_RESOLUTION==4)
00293     err = camera.init(CameraC328::Jpeg, CameraC328::RawResolution80x60, CameraC328::JpegResolution640x480);
00294 #endif
00295     if (CameraC328::NoError == err) {
00296         printf("[ OK ] : CameraC328::init\r\n");
00297     } else {
00298         printf("[FAIL] : CameraC328::init (Error=%02X)\r\n", (int)err);
00299     }
00300 
00301 
00302     snprintf(fname, sizeof(fname), "/sd/jpss%04d.jpg", fnamecnt);
00303     fnamecnt++;
00304     for(int i = 0; i < 12; i++){
00305        fname_server[0+i] = fname[4+i]; 
00306     }
00307     fp_jpeg = fopen(fname, "w");
00308 
00309     err = camera.getJpegSnapshotPicture(jpeg_callback);
00310     if (CameraC328::NoError == err) {
00311         printf("[ OK ] : CameraC328::getJpegSnapshotPicture\r\n");
00312     } else {
00313         printf("[FAIL] : CameraC328::getJpegSnapshotPicture (Error=%02X)\r\n", (int)err);
00314     }
00315 
00316     fclose(fp_jpeg);
00317 
00318 }