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
Prerequisite
In this example, the image data acquired from the camera is stored on the Micro SD card and displayed through a web browser.
To implement this function, you need a Platform board, network Interface board, camera module.
This example uses LS-Y201 Camera module.
- WIZwiki-W7500 from WIZnet (Platform board and Ethernet I/F board)
- LS-Y201 (Camera module)
- Micro SD Card
Hardware Configuration
- connect Ethernet Cable & USB Cable
- connect Camera module
Software
Init network information
#define IP "192.168.0.100" #define MASK "255.255.255.0" #define GATEWAY "192.168.0.1" #define PORT 80
Caution
Must fix network information
Revision 2:f2d70f552a3e, committed 2015-06-29
- Comitter:
- WizLeo
- Date:
- Mon Jun 29 07:36:30 2015 +0000
- Parent:
- 1:7d3ed406dd3d
- Child:
- 3:e28c4dd19cc0
- Commit message:
- SD card file server with ethernet camera
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Camera_LS_Y201.lib Mon Jun 29 07:36:30 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/shintamainjp/code/Camera_LS_Y201/#e32cc85c8bd8
--- a/filelib.h Tue Jun 23 03:44:18 2015 +0000
+++ b/filelib.h Mon Jun 29 07:36:30 2015 +0000
@@ -72,9 +72,9 @@
FRESULT res = f_stat(path,&finfo); /* Get file status */
if (EnDebugMSG)
if (res)
- printf("\n-->get_fileInfo:%s ,res=%d ,Not Found!",path,res);
+ printf("\r\n-->get_fileInfo:%s ,res=%d ,Not Found!",path,res);
else
- printf("\n-->get_fileInfo:%s ,res=%d ,Found!",path,res);
+ printf("\r\n-->get_fileInfo:%s ,res=%d ,Found!",path,res);
return res;
}
@@ -83,9 +83,9 @@
FRESULT res= f_opendir (&dinfo,path); /* FR_OK(0): successful, !=0: error code */
if (EnDebugMSG)
if (res)
- printf("\n-->get_dirInfo :%s res=%d ,This is Not Directory!",path,res);
+ printf("\r\n-->get_dirInfo :%s res=%d ,This is Not Directory!",path,res);
else
- printf("\n-->get_dirInfo :%s res=%d ,This is Directory!",path,res);
+ printf("\r\n-->get_dirInfo :%s res=%d ,This is Directory!",path,res);
return res;
}
@@ -98,7 +98,7 @@
buf->st_mode = 4; //?
}
if (EnDebugMSG)
- printf("\n--Mystat Path:%s ,filesize:%14lld ,res=%d",path, buf->st_size, res);
+ printf("\r\n--Mystat Path:%s ,filesize:%14lld ,res=%d",path, buf->st_size, res);
return res;
}
@@ -108,7 +108,7 @@
char* extension;
extension = strrchr( filename, '.' ); //get string after last .
if (EnDebugMSG)
- printf("\n-->get_mime_tipe filename:%s, extension:%s",filename, extension);
+ printf("\r\n-->get_mime_tipe filename:%s, extension:%s",filename, extension);
if (extension !=NULL) {
if (strcasecmp(extension,".htm")==0 || strcasecmp(extension,".html") ==0)
return "text/html; charset=iso-8859-1";
--- a/main.cpp Tue Jun 23 03:44:18 2015 +0000
+++ b/main.cpp Mon Jun 29 07:36:30 2015 +0000
@@ -1,17 +1,31 @@
#include "mbed.h"
#include "EthernetInterface.h"
+#include "Camera_LS_Y201.h"
#include "SDFileSystem.h"
-#define EnDebugMSG false //true-> print debug message to PC USB terminal, false->not print
+#define EnDebugMSG true //true-> print debug message to PC USB terminal, false->not print
#include "filelib.h"
+#define DEBMSG printf
+#define NEWLINE() printf("\r\n")
+
+#define USE_SDCARD 1
+
+#if USE_SDCARD
+#define FILENAME "/wfs/IMG_%04d.jpg"
+SDFileSystem sd(PB_3, PB_2, PB_1, PB_0, "wfs");
+#else
+#define FILENAME "/local/IMG_%04d.jpg"
+LocalFileSystem fs("local");
+#endif
+Camera_LS_Y201 cam1(PA_14, PA_13);
-#define IP "192.168.240.100"
+#define IP "192.168.217.100"
#define MASK "255.255.255.0"
-#define GATEWAY "192.168.240.1"
+#define GATEWAY "192.168.0.1"
#define PORT 80
+DigitalIn click(PC_1);
Serial pc (USBTX,USBRX); // tx, rx
-SDFileSystem sd(PB_3, PB_2, PB_1, PB_0, "wfs"); // the pinout on the mbed
char sMethod[7];
char sURL[250];
@@ -29,11 +43,83 @@
char line_response[256]= {0};
char file_path[256] = {0};
-DigitalOut led1(LED1); //server listning status
-DigitalOut led2(LED2); //socket connecting status
+DigitalOut lede (PC_8);
+DigitalOut led1 (PC_9); //server listning status
+DigitalOut led2 (PC_5); //socket connecting status
Ticker ledTick;
+typedef struct work {
+ FILE *fp;
+} work_t;
+
+work_t work;
+
+int take_picture = 0;
+
+/**
+ * Callback function for readJpegFileContent.
+ *
+ * @param buf A pointer to a buffer.
+ * @param siz A size of the buffer.
+ */
+void callback_func(int done, int total, uint8_t *buf, size_t siz) {
+ fwrite(buf, siz, 1, work.fp);
+
+ static int n = 0;
+ int tmp = done * 100 / total;
+ if (n != tmp) {
+ n = tmp;
+ DEBMSG("Writing...: %3d%%", n);
+ NEWLINE();
+ }
+}
+
+/**
+ * Capture.
+ *
+ * @param cam A pointer to a camera object.
+ * @param filename The file name.
+ *
+ * @return Return 0 if it succeed.
+ */
+int capture(Camera_LS_Y201 *cam, char *filename) {
+ /*
+ * Take a picture.
+ */
+ if (cam->takePicture() != 0) {
+ return -1;
+ }
+ DEBMSG("Captured.");
+ NEWLINE();
+
+ /*
+ * Open file.
+ */
+ work.fp = fopen(filename, "wb");
+ if (work.fp == NULL) {
+ return -2;
+ }
+
+ /*
+ * Read the content.
+ */
+ DEBMSG("%s", filename);
+ NEWLINE();
+ if (cam->readJpegFileContent(callback_func) != 0) {
+ fclose(work.fp);
+ return -3;
+ }
+ fclose(work.fp);
+
+ /*
+ * Stop taking pictures.
+ */
+ cam->stopTakingPictures();
+
+ return 0;
+}
+
void ledTickfunc()
{
if(serverIsListened) {
@@ -60,12 +146,12 @@
snprintf(sentBuffer, sizeof(sentBuffer),"%s%s\r\n",sentBuffer,line_response); //append to sentBuffer
if (EnDebugMSG)
- printf("\n-->sent Header--\n");
+ printf("\r\n-->sent Header--\r\n");
client.send_all(sentBuffer,strlen(sentBuffer));
if (EnDebugMSG) {
printf(sentBuffer);
- printf("\n--end Header-- bytes:%d",strlen(sentBuffer));
+ printf("\r\n--end Header-- bytes:%d",strlen(sentBuffer));
}
wait(0.2); //200ms important for browser!
}
@@ -74,7 +160,7 @@
{
client.send_all(line,length_line);
if (EnDebugMSG)
- printf("\n-->send HTML line:\n%s ...Ok!",line);
+ printf("\r\n-->send HTML line:\r\n%s ...Ok!",line);
wait(0.01);
}
@@ -82,7 +168,7 @@
{
send_HTTP_header("HTTP/1.1", status_code, title, "text/html", -1);
if (EnDebugMSG)
- printf("\n-->send_error...\n");
+ printf("\r\n-->send_error...\r\n");
sentBuffer[0]=NULL; //clear buffer
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);
snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
@@ -103,9 +189,14 @@
mime_type = get_mime_type( path_file );
snprintf(file_path, sizeof(file_path),"/wfs/%s",path_file);
+
+ if (strcmp(file_path,"/wfs//take_a_picture.jpg") == 0)
+ take_picture = 1;
+ else take_picture = 0;
+
if (EnDebugMSG) {
- printf("\n-->from send_file:%s",file_path);
- printf("\n-->from send_file mime type:%s",mime_type);
+ printf("\r\n-->from send_file:%s",file_path);
+ printf("\r\n-->from send_file mime type:%s",mime_type);
}
if (Mystat(path_file, &myStatBuf)) { //fault with file
@@ -131,13 +222,13 @@
fread (sentBuffer,1,bytes_for_send,fp);
filesize -= bytes_for_send;
if (EnDebugMSG)
- printf("\n---bytes_for_send...%d",bytes_for_send);
+ printf("\r\n---bytes_for_send...%d",bytes_for_send);
client.send_all(sentBuffer,bytes_for_send);
//Thread::wait(10);
all_send_bytes += bytes_for_send;
}
if (EnDebugMSG)
- printf("\n---buffer fill end - all ...%lld", all_send_bytes);
+ printf("\r\n---buffer fill end - all ...%lld", all_send_bytes);
//binary send
sprintf(line_response, "\r\n");
@@ -161,11 +252,11 @@
char timeBuf[40];
if (EnDebugMSG)
- printf("\n-->from send_directory:%s",path);
+ printf("\r\n-->from send_directory:%s",path);
snprintf(file_path,sizeof(file_path),"/wfs%s",path);
DIR *d = opendir(file_path);
if (EnDebugMSG && d!=NULL)
- printf("\n-->from send_directory:%s ...open OK",file_path);
+ printf("\r\n-->from send_directory:%s ...open OK",file_path);
if (d==NULL) { //error open dir
send_HTML_error( 403, "Forbidden", "403 - Directory access forbidden.");
return -1;
@@ -201,12 +292,12 @@
snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
while((p = readdir(d)) != NULL) {
if (EnDebugMSG)
- printf("\n :%s",p->d_name);
+ printf("\r\n :%s",p->d_name);
sprintf(file_path,"%s/%s",path,p->d_name);
Mystat( file_path, &sb );
if (get_dirInfo(file_path)==0 ) { //this is directory path
if (EnDebugMSG)
- printf("\nDIR");
+ printf("\r\nDIR");
sprintf(line_response, "<tr><td align=\"left\"><a href=\"%s\">%s</a><br></td></tr>\n",file_path,p->d_name);
if (strlen(line_response)>(sizeof(sentBuffer)-strlen(sentBuffer))) { //buffer must be sent
send_HTML_line(sentBuffer, strlen(sentBuffer));
@@ -216,7 +307,7 @@
} else { //this is file
if (EnDebugMSG)
- printf("\nFILE");
+ printf("\r\nFILE");
timeinfo = localtime (&sb.st_mtime);
//strftime(timeBuf,40, "%I:%M:%S %p (%Y/%m/%d)\r\n", localtime(&sb.st_mtime));
strftime(timeBuf, 40, "%c", timeinfo);
@@ -257,9 +348,9 @@
//get URL
snprintf(sURL,strlen(tmpBuffer)-strlen(sProtocol),"%s\r\n", tmpBuffer); //URL is between Method and Protocol
- printf("\nParse Method:%s",sMethod);
- printf("\nParse URL:%s",sURL);
- printf("\nParse PROTOCOL:%s",sProtocol);
+ printf("\r\nParse Method:%s",sMethod);
+ printf("\r\nParse URL:%s",sURL);
+ printf("\r\nParse PROTOCOL:%s",sProtocol);
printf("\n\r\n");
}
@@ -278,7 +369,7 @@
if (sURL[strlen(sURL)-1]=='/') {
sURL[strlen(sURL)-1]=sURL[strlen(sURL)]; //delete last symbol
if (EnDebugMSG)
- printf("\n delete last:%s",sURL);
+ printf("\r\n delete last:%s",sURL);
}
gdi= get_dirInfo(sURL);
gfi= get_fileInfo(sURL);
@@ -287,7 +378,7 @@
return send_directory(sURL);
}
if (EnDebugMSG)
- printf("\n404-br File not found or...(Fresult is:%d)",gfi);
+ printf("\r\n404-br File not found or...(Fresult is:%d)",gfi);
send_HTML_error( 404, "Not Found","404 - The requested resource could not be found.");
return 404;
} else { //==0 found
@@ -300,7 +391,49 @@
int main()
{
+ DEBMSG("Camera module");
+ NEWLINE();
+ DEBMSG("Resetting...");
+ NEWLINE();
+ lede = true;
+ if (cam1.reset() == 0) {
+ DEBMSG("Reset OK.");
+ NEWLINE();
+ } else {
+ DEBMSG("Reset fail.");
+ NEWLINE();
+ error("Reset fail.");
+ lede = false;
+ }
+ if (cam1.setImageSize() == 0) {
+ DEBMSG("Set image OK.");
+ NEWLINE();
+ } else {
+ DEBMSG("Set image fail.");
+ NEWLINE();
+ error("Set image fail.");
+ lede = false;
+ }
+ wait(1);
+
+ int cnt = 0;
+ while (cnt < 2) {
+ lede = false;
+ char fname[64];
+ snprintf(fname, sizeof(fname) - 1, FILENAME, cnt);
+ int r = capture(&cam1, fname);
+ if (r == 0) {
+ DEBMSG("[%04d]:OK.", cnt);
+ NEWLINE();
+ } else {
+ DEBMSG("[%04d]:NG. (code=%d)", cnt, r);
+ NEWLINE();
+ error("Failure.");
+ }
+ cnt++;
+ }
+ lede = true;
uint8_t MAC_Addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02};
ledTick.attach(&ledTickfunc,0.5);
//ledTick.detach();
@@ -333,6 +466,8 @@
printf("failed to accept connection.\n\r");
} else {
//client.set_blocking(false,5000); //5000=5sec
+
+ lede = true;
printf("connection success!\n\rIP: %s\n\r",client.get_address());
clientIsConnected = true;
led2 = true;
@@ -363,8 +498,26 @@
printf("close connection.\n\rHTTP server is listening...\n\r\n");
client.close();
wait(0.05);
+
+ if (take_picture) {
+ ledTick.detach();
+ lede = false;
+ char fname[64];
+ snprintf(fname, sizeof(fname) - 1, FILENAME, cnt);
+ int r = capture(&cam1, fname);
+ if (r == 0) {
+ DEBMSG("[%04d]:OK.", cnt); NEWLINE();
+ } else {
+ DEBMSG("[%04d]:NG. (code=%d)", cnt, r); NEWLINE();
+ error("Failure.");
+ }
+ cnt++;
+ ledTick.attach(&ledTickfunc,0.5);
+ take_picture = 0;
+ }
+
led2 = false;
}
+
}
-
}
