In this example, the image data acquired from the camera is stored on the Micro SD card and displayed through a web browser.
Dependencies: Ethernet_Camera_LS_Y201_SDcard SDFileSystem WIZnetInterface mbed
Fork of HTTP_SDcard_file_server_WIZwiki-W7500 by
main.cpp
00001 #include "mbed.h" 00002 #include "EthernetInterface.h" 00003 #include "Camera_LS_Y201.h" 00004 #include "SDFileSystem.h" 00005 00006 #define EnDebugMSG true //true-> print debug message to PC USB terminal, false->not print 00007 #include "filelib.h" 00008 #define DEBMSG printf 00009 #define NEWLINE() printf("\r\n") 00010 00011 #define USE_SDCARD 1 00012 00013 #if USE_SDCARD 00014 #define FILENAME "/wfs/IMG_%04d.jpg" 00015 SDFileSystem sd(PB_3, PB_2, PB_1, PB_0, "wfs"); 00016 #else 00017 #define FILENAME "/local/IMG_%04d.jpg" 00018 LocalFileSystem fs("local"); 00019 #endif 00020 Camera_LS_Y201 cam1(PA_14, PA_13); 00021 00022 #define IP "192.168.0.100" 00023 #define MASK "255.255.255.0" 00024 #define GATEWAY "192.168.0.1" 00025 #define PORT 80 00026 00027 DigitalIn click(PC_1); 00028 Serial pc (USBTX,USBRX); // tx, rx 00029 00030 char sMethod[7]; 00031 char sURL[250]; 00032 char sProtocol[8]; 00033 00034 EthernetInterface eth; 00035 00036 TCPSocketServer svr; 00037 bool serverIsListened = false; 00038 00039 TCPSocketConnection client; 00040 bool clientIsConnected = false; 00041 00042 char sentBuffer[1608] = {}; // 2*536=1072, 3*536=1608, 4*536=2144 !1500 00043 char line_response[256]= {0}; 00044 char file_path[256] = {0}; 00045 00046 DigitalOut lede (PC_8); 00047 DigitalOut led1 (PC_9); //server listning status 00048 DigitalOut led2 (PC_5); //socket connecting status 00049 00050 Ticker ledTick; 00051 00052 typedef struct work { 00053 FILE *fp; 00054 } work_t; 00055 00056 work_t work; 00057 00058 int take_picture = 0; 00059 00060 /** 00061 * Callback function for readJpegFileContent. 00062 * 00063 * @param buf A pointer to a buffer. 00064 * @param siz A size of the buffer. 00065 */ 00066 void callback_func(int done, int total, uint8_t *buf, size_t siz) { 00067 fwrite(buf, siz, 1, work.fp); 00068 00069 static int n = 0; 00070 int tmp = done * 100 / total; 00071 if (n != tmp) { 00072 n = tmp; 00073 DEBMSG("Writing...: %3d%%", n); 00074 NEWLINE(); 00075 } 00076 } 00077 00078 /** 00079 * Capture. 00080 * 00081 * @param cam A pointer to a camera object. 00082 * @param filename The file name. 00083 * 00084 * @return Return 0 if it succeed. 00085 */ 00086 int capture(Camera_LS_Y201 *cam, char *filename) { 00087 /* 00088 * Take a picture. 00089 */ 00090 if (cam->takePicture() != 0) { 00091 return -1; 00092 } 00093 DEBMSG("Captured."); 00094 NEWLINE(); 00095 00096 /* 00097 * Open file. 00098 */ 00099 work.fp = fopen(filename, "wb"); 00100 if (work.fp == NULL) { 00101 return -2; 00102 } 00103 00104 /* 00105 * Read the content. 00106 */ 00107 DEBMSG("%s", filename); 00108 NEWLINE(); 00109 if (cam->readJpegFileContent(callback_func) != 0) { 00110 fclose(work.fp); 00111 return -3; 00112 } 00113 fclose(work.fp); 00114 00115 /* 00116 * Stop taking pictures. 00117 */ 00118 cam->stopTakingPictures(); 00119 00120 return 0; 00121 } 00122 00123 void ledTickfunc() 00124 { 00125 if(serverIsListened) { 00126 led1 = !led1; 00127 } else { 00128 led1 = false; 00129 } 00130 } 00131 00132 void send_HTTP_header(char* protocol, int code, char* title, char* mime_type, long long lengthBody) 00133 { 00134 snprintf(line_response, sizeof(line_response),"%s %d %s\r\n", protocol, code, title ); 00135 snprintf(sentBuffer, sizeof(sentBuffer),"%s",line_response); 00136 00137 if ( mime_type != NULL ) { 00138 snprintf(line_response, sizeof(line_response), "Content-Type: %s\r\n", mime_type ); 00139 snprintf(sentBuffer, sizeof(sentBuffer), "%s%s",sentBuffer,line_response); //append to sentBuffer 00140 } 00141 if ( lengthBody >= 0 ) { 00142 snprintf(line_response, sizeof(line_response), "Content-Length: %lld\r\n", lengthBody ); 00143 snprintf(sentBuffer, sizeof(sentBuffer), "%s%s",sentBuffer,line_response); //append to sentBuffer 00144 } 00145 snprintf(line_response, sizeof(line_response), "Connection: close\r\n" ); 00146 snprintf(sentBuffer, sizeof(sentBuffer),"%s%s\r\n",sentBuffer,line_response); //append to sentBuffer 00147 00148 if (EnDebugMSG) 00149 printf("\r\n-->sent Header--\r\n"); 00150 00151 client.send_all(sentBuffer,strlen(sentBuffer)); 00152 if (EnDebugMSG) { 00153 printf(sentBuffer); 00154 printf("\r\n--end Header-- bytes:%d",strlen(sentBuffer)); 00155 } 00156 wait(0.2); //200ms important for browser! 00157 } 00158 00159 void send_HTML_line(char* line, unsigned int length_line) 00160 { 00161 client.send_all(line,length_line); 00162 if (EnDebugMSG) 00163 printf("\r\n-->send HTML line:\r\n%s ...Ok!",line); 00164 wait(0.01); 00165 } 00166 00167 void send_HTML_error( int status_code, char* title, char* body_text) 00168 { 00169 send_HTTP_header("HTTP/1.1", status_code, title, "text/html", -1); 00170 if (EnDebugMSG) 00171 printf("\r\n-->send_error...\r\n"); 00172 sentBuffer[0]=NULL; //clear buffer 00173 sprintf(line_response, "<!DOCTYPE html>\r\n<html>\r\n<head>\r\n<title>%d %s</title>\r\n</head>\r\n", status_code, title); 00174 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer 00175 sprintf(line_response, "<body><center><h2><center>%d %s</center></h2>\r\n",status_code, title ); 00176 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer 00177 sprintf(line_response, "%s\r\n", body_text ); 00178 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer 00179 sprintf(line_response, "<p>mbed HTTP File Server</p>\r\n</center></body></html>\r\n"); 00180 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer 00181 send_HTML_line(sentBuffer, strlen(sentBuffer)); 00182 } 00183 00184 int send_file(char *path_file) 00185 { 00186 char *mime_type = {0}; 00187 unsigned int bytes_for_send=0; 00188 long long filesize, all_send_bytes = 0; 00189 00190 mime_type = get_mime_type( path_file ); 00191 snprintf(file_path, sizeof(file_path),"/wfs/%s",path_file); 00192 00193 if (strcmp(file_path,"/wfs//take_a_picture.jpg") == 0) 00194 take_picture = 1; 00195 else take_picture = 0; 00196 00197 if (EnDebugMSG) { 00198 printf("\r\n-->from send_file:%s",file_path); 00199 printf("\r\n-->from send_file mime type:%s",mime_type); 00200 } 00201 00202 if (Mystat(path_file, &myStatBuf)) { //fault with file 00203 send_HTML_error( 403, "Forbidden", "403 - File access forbidden."); 00204 return 403; 00205 } 00206 FILE* fp = NULL; 00207 fp = fopen(file_path,"r"); 00208 if (fp==NULL ) { 00209 send_HTML_error( 403, "Forbidden", "403 - File access forbidden."); 00210 return 403; 00211 } 00212 00213 filesize = myStatBuf.st_size; 00214 send_HTTP_header("HTTP/1.1", 200, "Ok", mime_type, myStatBuf.st_size); 00215 //binary send 00216 all_send_bytes=0; 00217 while(filesize) { //check for EOF !feof(fp) 00218 bytes_for_send = filesize; 00219 if (bytes_for_send > sizeof(sentBuffer)) { 00220 bytes_for_send = sizeof(sentBuffer); 00221 } 00222 fread (sentBuffer,1,bytes_for_send,fp); 00223 filesize -= bytes_for_send; 00224 if (EnDebugMSG) 00225 printf("\r\n---bytes_for_send...%d",bytes_for_send); 00226 client.send_all(sentBuffer,bytes_for_send); 00227 //Thread::wait(10); 00228 all_send_bytes += bytes_for_send; 00229 } 00230 if (EnDebugMSG) 00231 printf("\r\n---buffer fill end - all ...%lld", all_send_bytes); 00232 //binary send 00233 00234 sprintf(line_response, "\r\n"); 00235 client.send_all(line_response,strlen(line_response)); 00236 if ( fp != NULL ) 00237 fclose(fp); 00238 //Thread::wait(10); 00239 return 0; 00240 } 00241 00242 int send_directory(char *path) 00243 { 00244 char process_name[64]= {0}; 00245 00246 char posOfLastSlash; 00247 char *pLS; 00248 00249 struct dirent *p; 00250 struct sMystat sb; 00251 struct tm *timeinfo; 00252 char timeBuf[40]; 00253 00254 if (EnDebugMSG) 00255 printf("\r\n-->from send_directory:%s",path); 00256 snprintf(file_path,sizeof(file_path),"/wfs%s",path); 00257 DIR *d = opendir(file_path); 00258 if (EnDebugMSG && d!=NULL) 00259 printf("\r\n-->from send_directory:%s ...open OK",file_path); 00260 if (d==NULL) { //error open dir 00261 send_HTML_error( 403, "Forbidden", "403 - Directory access forbidden."); 00262 return -1; 00263 } 00264 send_HTTP_header("HTTP/1.1", 200, "Ok",NULL, -1); 00265 sentBuffer[0]=NULL; 00266 sprintf(line_response,"<!DOCTYPE html>\r\n<html>\n<head><title>Index of %s</title>\n",path); 00267 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer 00268 00269 sprintf(line_response,"<meta content=\"text/html; charset=iso-8859-1\" http-equiv=\"Content-Type\"></head>\n"); 00270 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer 00271 00272 00273 sprintf(line_response,"<body><center>\n<h3>Index of %s</h3>\n", path); 00274 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer 00275 send_HTML_line(sentBuffer, strlen(sentBuffer)); 00276 //begin table 00277 sentBuffer[0]=NULL; //clear buffer 00278 sprintf(line_response,"<table border=\"0\">\n"); 00279 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer 00280 sprintf(line_response,"<tr><th align=\"left\" width=\"200\">Name</th><th align=\"right\" width=\"100\">Size(bytes)</th><th align=\"right\" width=\"200\">Date/Time</th></tr>\n"); 00281 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer 00282 //begin table 00283 00284 pLS=strrchr(path,'/'); 00285 posOfLastSlash=pLS-path+1; 00286 if (EnDebugMSG) 00287 printf("\r\n>>posOfLastSlash=%d",posOfLastSlash); 00288 snprintf(process_name,posOfLastSlash+1,"%s",path); 00289 if (EnDebugMSG) 00290 printf("\r\n>>process_name=%s",process_name); 00291 sprintf(line_response,"<tr><td align=\"left\"><a href=\"%s\">../</a></td></tr>\n",process_name); 00292 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer 00293 while((p = readdir(d)) != NULL) { 00294 if (EnDebugMSG) 00295 printf("\r\n :%s",p->d_name); 00296 sprintf(file_path,"%s/%s",path,p->d_name); 00297 Mystat( file_path, &sb ); 00298 if (get_dirInfo(file_path)==0 ) { //this is directory path 00299 if (EnDebugMSG) 00300 printf("\r\nDIR"); 00301 sprintf(line_response, "<tr><td align=\"left\"><a href=\"%s\">%s</a><br></td></tr>\n",file_path,p->d_name); 00302 if (strlen(line_response)>(sizeof(sentBuffer)-strlen(sentBuffer))) { //buffer must be sent 00303 send_HTML_line(sentBuffer, strlen(sentBuffer)); 00304 sentBuffer[0]=NULL; //clear buffer 00305 } 00306 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer 00307 00308 } else { //this is file 00309 if (EnDebugMSG) 00310 printf("\r\nFILE"); 00311 timeinfo = localtime (&sb.st_mtime); 00312 //strftime(timeBuf,40, "%I:%M:%S %p (%Y/%m/%d)\r\n", localtime(&sb.st_mtime)); 00313 strftime(timeBuf, 40, "%c", timeinfo); 00314 sprintf(line_response, "<tr><td align=\"left\"><a href=\"%s\">%s</a></td><td align=\"right\">%lld</td><td align=\"right\">%s</td></tr>\n", file_path, p->d_name,(long long) sb.st_size,timeBuf); // asctime(timeinfo) ); 00315 00316 if (strlen(line_response)>(sizeof(sentBuffer)-strlen(sentBuffer))) { //buffer must be sent 00317 send_HTML_line(sentBuffer, strlen(sentBuffer)); 00318 sentBuffer[0]=NULL; //clear buffer 00319 } 00320 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer 00321 } 00322 } 00323 send_HTML_line(sentBuffer, strlen(sentBuffer)); 00324 closedir(d); 00325 00326 sprintf(line_response, "</table>\n<br><h4>mbed HTTP File Server</h4>\n</center></body></html>\n"); 00327 send_HTML_line(line_response, strlen(line_response)); 00328 00329 return 0; 00330 } 00331 00332 void parseHTTPRequest(char* buffer) 00333 { 00334 char spacePos; 00335 char *tmpBuffer; 00336 spacePos = strcspn(buffer, " ") + 1; //position of first space character 00337 snprintf(sMethod, spacePos,"%s", buffer); 00338 00339 //get Protocol 00340 tmpBuffer=&(buffer[spacePos]); //move pointer to buffer (delete Method) 00341 spacePos = strcspn(tmpBuffer, "\r\n") + 1; 00342 tmpBuffer[spacePos]='\0'; //set end of string ...cut 00343 sprintf(sProtocol, "%s", strrchr(tmpBuffer,' ')); //get string after last (space ) 00344 printf("\r\nsProtocol:%s", tmpBuffer); 00345 buffer = &(sProtocol[1]); //cut first character (space) 00346 sprintf(sProtocol, "%s", buffer); 00347 00348 //get URL 00349 snprintf(sURL,strlen(tmpBuffer)-strlen(sProtocol),"%s\r\n", tmpBuffer); //URL is between Method and Protocol 00350 00351 printf("\r\nParse Method:%s",sMethod); 00352 printf("\r\nParse URL:%s",sURL); 00353 printf("\r\nParse PROTOCOL:%s",sProtocol); 00354 printf("\n\r\n"); 00355 } 00356 00357 int processHTTP(char* sMethod, char* sURL, char* sProtocol) 00358 { 00359 int gdi, gfi; //status of get_dir_info(xxx), and get_file_info(xxx) 00360 00361 if (strcmp(sMethod,"GET")!=0) { 00362 send_HTML_error( 501, "501 Not Implemented", "501 - The server either does not recognize the request method"); 00363 return 501; 00364 } 00365 if (sURL[0]!= '/') { 00366 send_HTML_error( 400, "Bad Request", "400 - The request cannot be fulfilled due to bad syntax."); 00367 return 400; 00368 } 00369 if (sURL[strlen(sURL)-1]=='/') { 00370 sURL[strlen(sURL)-1]=sURL[strlen(sURL)]; //delete last symbol 00371 if (EnDebugMSG) 00372 printf("\r\n delete last:%s",sURL); 00373 } 00374 gdi= get_dirInfo(sURL); 00375 gfi= get_fileInfo(sURL); 00376 if (gfi!=0) { //!=0 file not found 00377 if (gdi==0) { //0-ok this is directory 00378 return send_directory(sURL); 00379 } 00380 if (EnDebugMSG) 00381 printf("\r\n404-br File not found or...(Fresult is:%d)",gfi); 00382 send_HTML_error( 404, "Not Found","404 - The requested resource could not be found."); 00383 return 404; 00384 } else { //==0 found 00385 if (gdi==0) //0-ok this is directory 00386 return send_directory(sURL); 00387 else 00388 return send_file(sURL); 00389 } 00390 } 00391 00392 int main() 00393 { 00394 DEBMSG("Camera module"); 00395 NEWLINE(); 00396 DEBMSG("Resetting..."); 00397 NEWLINE(); 00398 lede = true; 00399 if (cam1.reset() == 0) { 00400 DEBMSG("Reset OK."); 00401 NEWLINE(); 00402 } else { 00403 DEBMSG("Reset fail."); 00404 NEWLINE(); 00405 error("Reset fail."); 00406 lede = false; 00407 } 00408 00409 if (cam1.setImageSize() == 0) { 00410 DEBMSG("Set image OK."); 00411 NEWLINE(); 00412 } else { 00413 DEBMSG("Set image fail."); 00414 NEWLINE(); 00415 error("Set image fail."); 00416 lede = false; 00417 } 00418 wait(1); 00419 00420 int cnt = 0; 00421 while (cnt < 2) { 00422 lede = false; 00423 char fname[64]; 00424 snprintf(fname, sizeof(fname) - 1, FILENAME, cnt); 00425 int r = capture(&cam1, fname); 00426 if (r == 0) { 00427 DEBMSG("[%04d]:OK.", cnt); 00428 NEWLINE(); 00429 } else { 00430 DEBMSG("[%04d]:NG. (code=%d)", cnt, r); 00431 NEWLINE(); 00432 error("Failure."); 00433 } 00434 cnt++; 00435 } 00436 lede = true; 00437 uint8_t MAC_Addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02}; 00438 ledTick.attach(&ledTickfunc,0.5); 00439 //ledTick.detach(); 00440 //setup ethernet interface 00441 //eth.init(); //Use DHCP 00442 eth.init(MAC_Addr,IP,MASK,GATEWAY); //MAC,IP,mask,Gateway 00443 eth.connect(); 00444 printf("IP Address is %s\n\r", eth.getIPAddress()); 00445 00446 //setup tcp socket 00447 if(svr.bind(PORT)< 0) { 00448 printf("tcp server bind failed.\n\r"); 00449 return -1; 00450 } else { 00451 printf("tcp server bind successed.\n\r"); 00452 serverIsListened = true; 00453 } 00454 00455 if(svr.listen(1) < 0) { 00456 printf("tcp server listen failed.\n\r"); 00457 return -1; 00458 } else { 00459 printf("tcp server is listening...\n\r"); 00460 } 00461 00462 //listening for http GET request 00463 while (serverIsListened) { 00464 //blocking mode(never timeout) 00465 if(svr.accept(client)<0) { 00466 printf("failed to accept connection.\n\r"); 00467 } else { 00468 //client.set_blocking(false,5000); //5000=5sec 00469 00470 lede = true; 00471 printf("connection success!\n\rIP: %s\n\r",client.get_address()); 00472 clientIsConnected = true; 00473 led2 = true; 00474 while(clientIsConnected) { 00475 char buffer[1024] = {}; 00476 switch(client.receive(buffer, 1023)) { 00477 case 0: 00478 printf("recieved buffer is empty.\n\r"); 00479 clientIsConnected = false; 00480 break; 00481 case -1: 00482 printf("failed to read data from client.\n\r"); 00483 clientIsConnected = false; 00484 break; 00485 default: 00486 printf("Recieved Data: %d\n\r\n\r%.*s\n\r",strlen(buffer),strlen(buffer),buffer); 00487 parseHTTPRequest(buffer); 00488 //if(buffer[0] == 'G' && buffer[1] == 'E' && buffer[2] == 'T' ) { 00489 if (strcmp(sMethod, "GET" ) == 0 ) { 00490 printf("GET request incomming.\n\r"); 00491 processHTTP(sMethod, sURL, sProtocol); 00492 clientIsConnected = false; 00493 }//if get 00494 break; 00495 } //switch 00496 //ledTick.attach(&ledTickfunc,0.5); 00497 }//while 00498 printf("close connection.\n\rHTTP server is listening...\n\r\n"); 00499 client.close(); 00500 wait(0.05); 00501 00502 if (take_picture) { 00503 ledTick.detach(); 00504 lede = false; 00505 char fname[64]; 00506 snprintf(fname, sizeof(fname) - 1, FILENAME, cnt); 00507 int r = capture(&cam1, fname); 00508 if (r == 0) { 00509 DEBMSG("[%04d]:OK.", cnt); NEWLINE(); 00510 } else { 00511 DEBMSG("[%04d]:NG. (code=%d)", cnt, r); NEWLINE(); 00512 error("Failure."); 00513 } 00514 cnt++; 00515 ledTick.attach(&ledTickfunc,0.5); 00516 take_picture = 0; 00517 } 00518 00519 led2 = false; 00520 } 00521 00522 } 00523 }
Generated on Wed Jul 13 2022 12:16:49 by 1.7.2