Camera_WebStreaming with WIZwiki-W7500

Dependencies:   CameraC328 SDFileSystem WIZnetInterface_Ricky 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 
00007 #define EnDebugMSG  true
00008 #define NEWLINE()   printf("\r\n")
00009 /* CAMERA */
00010 #define USE_JPEG_HIGH_RESOLUTION    2//1=80x64 <--- not working -_-;;, 2=160x128, 3=320x240, 4=640x480
00011 SDFileSystem sd(PB_3, PB_2, PB_1, PB_0, "fs"); // the pinout on the mbed Cool Components workshop board
00012 CameraC328 camera(PA_13, PA_14, CameraC328::Baud115200);
00013 static FILE *fp_jpeg;
00014 /* TCP/IP */
00015 #define WEB_SERVER_PORT   80
00016 char echoHeader[512] = {0,};
00017 char line_response[8]= {0};
00018 char sentBuffer[512] = {0,}; 
00019 char recv_buffer[1024] = {0,};
00020 
00021 uint8_t mac_addr[6] = {0x00, 0x08, 0xdc, 0x12, 0x34, 0x45};
00022 const char ip_addr[] = "192.168.0.123"; 
00023 const char mask_addr[] = "255.255.255.0"; 
00024 const char gateway_addr[] = "192.168.0.1"; 
00025 
00026 EthernetInterface eth;
00027 TCPSocketServer server;
00028 
00029 
00030 /* UART */
00031 Serial pc(USBTX, USBRX);
00032 
00033 /* Function*/ 
00034 void jpeg_callback(char *buf, size_t siz);
00035 void sync(void);
00036 void test_jpeg_snapshot_picture(void);
00037 void init(void);
00038 
00039 int main() {
00040 
00041     unsigned int bytes_for_send=0;
00042     long long filesize, all_send_bytes = 0;
00043     
00044     printf("Hello WIZwiki-W7500 Camera WebStreaming World!\r\n"); 
00045       
00046     eth.init(mac_addr, ip_addr, mask_addr, gateway_addr); //Use Static
00047     eth.connect();
00048     
00049     sync();
00050     init();
00051     
00052     printf("WIZwiki-W7500 IP Address is %s\r\n", eth.getIPAddress());
00053     
00054     server.bind(WEB_SERVER_PORT);
00055     printf("bind complete\r\n");
00056     server.listen();
00057     printf("listen complete\r\n");
00058     
00059     while(true)
00060     {
00061         TCPSocketConnection Streaming;
00062         printf("Wait for new connection...\r\n");
00063         server.accept(Streaming);
00064         Streaming.set_blocking(false, 15000); // Timeout after (1.5)s
00065         
00066         if (EnDebugMSG)
00067         printf("Connection from: %s\r\n", Streaming.get_address());
00068         
00069         while (true) {        
00070             test_jpeg_snapshot_picture();    
00071             int n = Streaming.receive(recv_buffer, sizeof(recv_buffer));
00072             if (n <= 0) break; // n = n < 0
00073             
00074             // print received message to terminal
00075             recv_buffer[n] = '\0';
00076             if (EnDebugMSG)
00077             printf("Received message from Client :'%s'\r\n",recv_buffer);
00078             if(recv_buffer[0] == 'G' && recv_buffer[1] == 'E' && recv_buffer[2] == 'T')
00079             {
00080                 char fname[64];
00081                 snprintf(fname, sizeof(fname), "/fs/Stream.jpg");
00082                 fp_jpeg = fopen(fname, "r");
00083                 fseek(fp_jpeg, 0, SEEK_END);            // seek to end of file
00084                 filesize = ftell(fp_jpeg);              // get current file pointer
00085                 fseek(fp_jpeg, 0, SEEK_SET);            // seek back to beginning of file  
00086                 
00087                 sprintf(echoHeader,"HTTP/1.1 200 OK\r\nContent-Type: image/jpeg\r\nContent-Length: %lld\r\nConnection: close\r\nRefresh: 0.5\r\n\r\n", filesize);
00088                 if (EnDebugMSG)
00089                 printf("%s\r\n", echoHeader);
00090                 Streaming.send_all(echoHeader,strlen(echoHeader));
00091                 
00092                 //wait(0.2);  //200ms important for browser!
00093                 if (EnDebugMSG)
00094                 printf("---filesize = %lld\r\n",filesize);
00095                 all_send_bytes = 0;
00096                 while(filesize)  
00097                 {  //check for EOF !feof(fp)
00098                     
00099                     bytes_for_send = filesize;
00100                     if (bytes_for_send > sizeof(sentBuffer)) 
00101                     {
00102                         bytes_for_send = sizeof(sentBuffer);
00103                     }
00104                     fread (sentBuffer,1,bytes_for_send,fp_jpeg);
00105                     filesize -= bytes_for_send;
00106                     
00107                     if (EnDebugMSG)
00108                     printf("\r\n---bytes_for_send...%d",bytes_for_send);
00109                     
00110                     Streaming.send_all(sentBuffer,bytes_for_send);
00111                     //Thread::wait(10);
00112                     all_send_bytes += bytes_for_send;
00113                 }
00114                 if (EnDebugMSG)
00115                 printf("\r\n---buffer fill end - all ...%lld\r\n", all_send_bytes);
00116                 //binary send
00117                 
00118                 sprintf(line_response, "\r\n");
00119                 Streaming.send_all(line_response,strlen(line_response));
00120                 if ( fp_jpeg != NULL )
00121                 fclose(fp_jpeg);       
00122             }
00123         }
00124     }
00125 }
00126 
00127 void jpeg_callback(char *buf, size_t siz) {
00128     for (int i = 0; i < (int)siz; i++) {
00129         fprintf(fp_jpeg, "%c", buf[i]);
00130     }
00131 }
00132 void sync(void) {
00133     CameraC328::ErrorNumber err = CameraC328::NoError;
00134 
00135     err = camera.sync();
00136     if (CameraC328::NoError == err) {
00137         if (EnDebugMSG)
00138         printf("[ OK ] : CameraC328::sync\r\n");
00139     } else {
00140         if (EnDebugMSG)
00141         printf("[FAIL] : CameraC328::sync (Error=%02X)\r\n", (int)err);
00142     }
00143 }
00144 void init() {
00145     CameraC328::ErrorNumber err = CameraC328::NoError;
00146 
00147 #if (USE_JPEG_HIGH_RESOLUTION==1)
00148     err = camera.init(CameraC328::Jpeg, CameraC328::RawResolution80x60, CameraC328::JpegResolution80x64);
00149 #elif (USE_JPEG_HIGH_RESOLUTION==2)
00150     err = camera.init(CameraC328::Jpeg, CameraC328::RawResolution80x60, CameraC328::JpegResolution160x128);
00151 #elif (USE_JPEG_HIGH_RESOLUTION==3)
00152     err = camera.init(CameraC328::Jpeg, CameraC328::RawResolution80x60, CameraC328::JpegResolution320x240);
00153 #elif (USE_JPEG_HIGH_RESOLUTION==4)
00154     err = camera.init(CameraC328::Jpeg, CameraC328::RawResolution80x60, CameraC328::JpegResolution640x480);
00155 #endif
00156     if (CameraC328::NoError == err) {
00157         if (EnDebugMSG)
00158         printf("[ OK ] : CameraC328::init\r\n");
00159     } else {
00160         if (EnDebugMSG)
00161         printf("[FAIL] : CameraC328::init (Error=%02X)\r\n", (int)err);
00162     }
00163 }
00164 void test_jpeg_snapshot_picture() {
00165     CameraC328::ErrorNumber err = CameraC328::NoError;
00166       
00167     char fname[64];
00168     snprintf(fname, sizeof(fname), "/fs/Stream.jpg");
00169     fp_jpeg = fopen(fname, "w");
00170 
00171     err = camera.getJpegSnapshotPicture(jpeg_callback);
00172     if (CameraC328::NoError == err) {
00173         if (EnDebugMSG)
00174         printf("[ OK ] : CameraC328::getJpegSnapshotPicture\n");
00175     } else {
00176         if (EnDebugMSG)
00177         printf("[FAIL] : CameraC328::getJpegSnapshotPicture (Error=%02X)\n", (int)err);
00178     }
00179 
00180     fclose(fp_jpeg);
00181 }