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

Dependencies:   CameraC328 SDFileSystem WIZnetInterface mbed-src

Fork of Camera_FTP_WIZwiki-W7500 by Ricky Kwon

Prerequisite

This example send the image data acquired from the camera to an FTP Server.

To implement this function, you need a Platform board, network Interface board, camera module.

Available camera modules are C328, LJ-DSC02. Because the cameras listed above all have similar internal protocols.

This example uses LJ-DSC02.

  • WIZwiki-W7500 from WIZnet (Platform board and Ethernet I/F board)
  • LJ-DSC02 (Camera module)
  • FTP Server(AL FTP)
  • Micro SD Card

Hardware Configuration

/media/uploads/Ricky_Kwon/camera.png

  • connect Ethernet Cable & USB Cable
  • connect Camera module

Software

Init FTP Server information

static char ID[]={"abc"};                   //Set FTPServer Login ID
static char PASSWORD[]={"123"};             //Set FTPServer Login Password
const char ftpServer_control_ip_addr[] = "192.168.1.2";

Caution

Must fix FTP server ip, id, pass

Files at this revision

API Documentation at this revision

Comitter:
Ricky_Kwon
Date:
Tue Jul 21 17:33:35 2015 +0000
Child:
1:7d428f360a0a
Commit message:
FTP+CAMERA

Changed in this revision

CameraC328.lib Show annotated file Show diff for this revision Revisions of this file
SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
WIZnetInterface.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-src.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CameraC328.lib	Tue Jul 21 17:33:35 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/shintamainjp/code/CameraC328/#49cfda6c547f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Tue Jul 21 17:33:35 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/mbed_official/code/SDFileSystem/#7b35d1709458
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WIZnetInterface.lib	Tue Jul 21 17:33:35 2015 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/WIZnet/code/WIZnetInterface_Ricky/#5a1969320666
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Jul 21 17:33:35 2015 +0000
@@ -0,0 +1,313 @@
+#include "mbed.h"
+#include "SDFileSystem.h"
+#include "EthernetInterface.h"
+#include "CameraC328.h"
+
+/* CAMERA */
+#define USE_JPEG_HIGH_RESOLUTION  1
+
+/* FTP */
+#define USER                "user "
+#define PASS                "pass "
+#define PASV                "pasv "
+#define PORT                "port "
+#define LIST                "list "
+#define STOR                "stor "
+#define RETR                "retr "
+#define END                 "\r\n"
+#define FTP_SERVER_PORT     21
+#define MAX_SS              256
+bool gDataSockReady = false;
+bool gDataPutGetStart = false;
+bool ftpclientrun = false;
+int remote_port;
+char ftpServer_data_ip_addr[4]={0,};
+char ftpServer_data_ip_addr_str[20]={0,};
+static char buf[256];
+enum CommandFirst {
+    f_nocmd,
+    f_dir,
+    f_put,
+    f_get,
+};
+enum CommandSecond {
+    s_nocmd,
+    s_dir,
+    s_put,
+    s_get,
+};
+enum ftpc_datasock_state{
+    DATASOCK_IDLE,
+    DATASOCK_READY,
+    DATASOCK_START
+};
+struct Command {
+    enum CommandFirst First;
+    enum CommandSecond Second;
+};
+struct ftpc {
+    enum ftpc_datasock_state dsock_state;
+};
+struct ftpc FTPClient;
+struct Command FTPCommand;
+
+/* SD Card filesystem */
+SDFileSystem sd(PB_3, PB_2, PB_1, PB_0, "sd"); // WIZwiki-W7500 
+/* UART */
+Serial pc(USBTX, USBRX);
+char gMsgBuf[10];
+int User_Keyboard_MSG_Cnt;
+/* CAMERA */
+static const int CAPTURE_FRAMES = 1;
+static FILE *fp_jpeg;
+char fname[32];
+char fname_server[16];
+CameraC328 camera(PA_13, PA_14, CameraC328::Baud115200);
+/* Fuction*/ 
+char* User_Keyboard_MSG(void);
+int pportc(char * arg);
+void jpeg_callback(char *buf, size_t siz);
+void sync(void);
+void test_jpeg_snapshot_picture(void);
+
+int main() {
+
+    char Msg_c;
+    int size;
+    int remain_datasize;
+    int remain_filesize;
+    int send_byte;
+    
+    
+    pc.baud(115200);
+    
+    pc.printf("Hello Home Security World!\r\n"); 
+      
+    uint8_t mac_addr[6] = {0x00, 0x08, 0xdc, 0x12, 0x34, 0x45};
+    const char ip_addr[] = "192.168.0.123"; 
+    const char mask_addr[] = "255.255.255.0"; 
+    const char gateway_addr[] = "192.168.0.1"; 
+    const char ftpServer_control_ip_addr[] = "192.168.0.230";
+    
+    
+    EthernetInterface eth;
+    eth.init(mac_addr, ip_addr, mask_addr, gateway_addr); //Use Static
+    eth.connect();
+    
+    TCPSocketConnection FTP_CONTROL_SOCK;
+    TCPSocketConnection FTP_DATA_SOCK;
+    
+    sync();
+    //while 1
+    while(true)
+    {
+        if(pc.getc()==0x31){
+            
+            test_jpeg_snapshot_picture();
+            ftpclientrun = true;
+            while(ftpclientrun){
+                //while 2
+                while(!FTP_CONTROL_SOCK.is_connected()){
+                    pc.printf("Connecting...FTPServer\r\nIP:%s, PORT:%d\r\n", ftpServer_control_ip_addr, FTP_SERVER_PORT);
+                    FTP_CONTROL_SOCK.connect(ftpServer_control_ip_addr, FTP_SERVER_PORT);
+                    FTP_CONTROL_SOCK.set_blocking(false, 15000); // Timeout after (1.5)s
+                }//end of while 2
+                
+                //while 3
+                while(true)
+                {  
+                    //gDataSockReady if
+                    if(gDataSockReady){
+                        gDataSockReady = false;
+                        //FTPCommand.First switch case
+                        switch(FTPCommand.First){
+                            case f_put:
+                                FTP_CONTROL_SOCK.send(STOR, sizeof(STOR)-1);
+                                FTP_CONTROL_SOCK.send(fname_server, sizeof(fname_server));
+                                FTP_CONTROL_SOCK.send(END, sizeof(END)-1);
+                                break;
+                        }//end of FTPCommand.First switch case
+                    }//end of gDataSockReady if          
+                    /* received from FTPServer */
+                    int n = FTP_CONTROL_SOCK.receive(buf, sizeof(buf));
+                    if (n <= 0) break;
+                    buf[n] = '\0';
+                    pc.printf("Received message from server: '%s' \n", buf);
+                    
+                    //buf if
+                    if (!strncmp(buf, "220", 3)){
+                        pc.printf("\r\nInput your User ID > ");
+                        FTP_CONTROL_SOCK.send(USER, sizeof(USER)-1);
+                        //FTP_CONTROL_SOCK.send(gMsgBuf, User_Keyboard_MSG_Cnt-1);
+                        FTP_CONTROL_SOCK.send(END, sizeof(END)-1);
+                    }
+                    else if(!strncmp(buf, "331", 3)){
+                        pc.printf("\r\nInput your Password > ");
+                        FTP_CONTROL_SOCK.send(PASS, sizeof(PASS)-1);
+                        //FTP_CONTROL_SOCK.send(gMsgBuf, User_Keyboard_MSG_Cnt-1);
+                        FTP_CONTROL_SOCK.send(END, sizeof(END)-1);        
+                    }
+                    else if(!strncmp(buf, "230", 3)){
+                        FTP_CONTROL_SOCK.send(PASV, sizeof(PASV)-1);
+                        FTP_CONTROL_SOCK.send(END, sizeof(END)-1);
+                        FTPCommand.First = f_put;    
+                    }
+                    else if(!strncmp(buf, "227", 3)){
+                        if (pportc(buf) == -1){
+                            pc.printf("Bad port syntax\r\n");
+                        }
+                        else{
+                            pc.printf("Go Open Data Sock...\r\n ");
+                            FTPClient.dsock_state = DATASOCK_READY;
+                        }    
+                    }
+                    else if(!strncmp(buf, "150", 3)){
+                        switch(FTPCommand.First){
+                        case f_put:
+                            FTPCommand.First = f_nocmd;
+                            FTPCommand.Second = s_put;
+                            gDataPutGetStart = true;
+                            break;
+                        }  
+                    }
+                    else if(!strncmp(buf, "226", 3)){
+                        ftpclientrun = false;  
+                        FTP_CONTROL_SOCK.close();       
+                    }//end of buf if
+                    
+                    
+                    //DATASOCK_READY if
+                    if(FTPClient.dsock_state == DATASOCK_READY){
+                        while(!FTP_DATA_SOCK.is_connected()){
+                            pc.printf("Connecting...FTPServer Data sock\r\nIP:%s, PORT:%d\r\n", ftpServer_data_ip_addr_str, remote_port);
+                            FTP_DATA_SOCK.connect(ftpServer_control_ip_addr, remote_port);
+                            FTP_DATA_SOCK.set_blocking(false, 15000); // Timeout after (1.5)s
+                        }
+                        FTPClient.dsock_state = DATASOCK_IDLE;
+                        gDataSockReady = true;
+                    }//end of DATASOCK_READY if
+                    
+                    //gDataPutGetStart if
+                    if(gDataPutGetStart){
+                        //FTPCommand.Second switch case
+                        switch(FTPCommand.Second){   
+                        case s_put:
+                            pc.printf("put waiting...\r\n");
+                            fp_jpeg = fopen(fname, "r");  
+                            fseek(fp_jpeg, 0, SEEK_END);            // seek to end of file
+                            remain_filesize = ftell(fp_jpeg);       // get current file pointer
+                            fseek(fp_jpeg, 0, SEEK_SET);            // seek back to beginning of file                
+                            do{
+                                memset(buf, 0, sizeof(buf));
+                                if(remain_filesize > MAX_SS)
+                                    send_byte = MAX_SS;
+                                else
+                                    send_byte = remain_filesize;
+                                fread (buf, 1, send_byte, fp_jpeg);
+                                FTP_DATA_SOCK.send(buf, send_byte);
+                                remain_filesize -= send_byte;
+                                pc.printf("#");
+                            }while(remain_filesize!=0);
+                            fclose(fp_jpeg);
+                            gDataPutGetStart = false; 
+                            FTPCommand.Second = s_nocmd;     
+                            FTP_DATA_SOCK.close(); 
+                            break;   
+                                
+                        }//end of FTPCommand.Second switch case
+                    }//end of gDataPutGetStart if
+                }//end of while 3                
+            }
+        }
+    }//end of while 1
+}
+char* User_Keyboard_MSG(void)
+{
+    User_Keyboard_MSG_Cnt = 0;
+    memset(gMsgBuf, 0, sizeof(gMsgBuf));
+    do{
+        gMsgBuf[User_Keyboard_MSG_Cnt] = pc.getc();
+        if(gMsgBuf[User_Keyboard_MSG_Cnt]==0x08){
+            User_Keyboard_MSG_Cnt--;
+        }
+        else{
+            User_Keyboard_MSG_Cnt++; 
+        }
+    }while(gMsgBuf[User_Keyboard_MSG_Cnt-1]!=0x0d);
+    return gMsgBuf;
+}
+int pportc(char * arg)
+{
+    int i;
+    char* tok=0;
+    strtok(arg,"(");
+    for (i = 0; i < 4; i++)
+    {
+        if(i==0) tok = strtok(NULL,",\r\n");
+        else     tok = strtok(NULL,",");
+        ftpServer_data_ip_addr[i] = (uint8_t)atoi(tok);
+        if (!tok){
+            pc.printf("bad pport : %s\r\n", arg);
+            return -1;
+        }
+    }
+    remote_port = 0;
+    for (i = 0; i < 2; i++){
+        tok = strtok(NULL,",\r\n");
+        remote_port <<= 8;
+        remote_port += atoi(tok);
+        if (!tok){
+            pc.printf("bad pport : %s\r\n", arg);
+            return -1;
+        }
+    }
+    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);
+    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]);
+    return 0;
+}
+void jpeg_callback(char *buf, size_t siz) {
+    for (int i = 0; i < (int)siz; i++) {
+        fprintf(fp_jpeg, "%c", buf[i]);
+    }
+}
+void sync(void) {
+    CameraC328::ErrorNumber err = CameraC328::NoError;
+
+    err = camera.sync();
+    if (CameraC328::NoError == err) {
+        printf("[ OK ] : CameraC328::sync\n");
+    } else {
+        printf("[FAIL] : CameraC328::sync (Error=%02X)\n", (int)err);
+    }
+}
+void test_jpeg_snapshot_picture(void) {
+    CameraC328::ErrorNumber err = CameraC328::NoError;
+
+#if USE_JPEG_HIGH_RESOLUTION
+    err = camera.init(CameraC328::Jpeg, CameraC328::RawResolution80x60, CameraC328::JpegResolution640x480);
+#else
+    err = camera.init(CameraC328::Jpeg, CameraC328::RawResolution80x60, CameraC328::JpegResolution320x240);
+#endif
+    if (CameraC328::NoError == err) {
+        printf("[ OK ] : CameraC328::init\n");
+    } else {
+        printf("[FAIL] : CameraC328::init (Error=%02X)\n", (int)err);
+    }
+
+    for (int i = 0; i < CAPTURE_FRAMES; i++) {
+        snprintf(fname, sizeof(fname), "/sd/jpss%04d.jpg", i);
+        for(int i = 0; i < 12; i++){
+           fname_server[0+i] = fname[4+i]; 
+        }
+        fp_jpeg = fopen(fname, "w");
+
+        err = camera.getJpegSnapshotPicture(jpeg_callback);
+        if (CameraC328::NoError == err) {
+            printf("[ OK ] : CameraC328::getJpegSnapshotPicture\n");
+        } else {
+            printf("[FAIL] : CameraC328::getJpegSnapshotPicture (Error=%02X)\n", (int)err);
+        }
+
+        fclose(fp_jpeg);
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-src.lib	Tue Jul 21 17:33:35 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-src/#523654f2b367