Program for VC0706 with FTP and MQTT
Dependencies: Ethernet_Camera_LS_Y201_SDcard FTPClient MQTT SDFileSystem WIZnetInterface mbed-src
Fork of FTP_Client_Configuration by
Revision 3:cd4afee8b093, committed 2018-07-16
- Comitter:
- Albinarackal
- Date:
- Mon Jul 16 09:25:11 2018 +0000
- Parent:
- 2:a185787cdddf
- Commit message:
- Program for VC0706 with MQTT and FTP
Changed in this revision
diff -r a185787cdddf -r cd4afee8b093 Camera_LS_Y201.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Camera_LS_Y201.lib Mon Jul 16 09:25:11 2018 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/teams/WIZnet/code/Ethernet_Camera_LS_Y201_SDcard/#e32cc85c8bd8
diff -r a185787cdddf -r cd4afee8b093 FTPClient.lib --- a/FTPClient.lib Thu Dec 14 05:35:51 2017 +0000 +++ b/FTPClient.lib Mon Jul 16 09:25:11 2018 +0000 @@ -1,1 +1,1 @@ -https://os.mbed.com/users/akshaytom/code/FTPClient/#8ecc32e7c69b +https://os.mbed.com/users/Albinarackal/code/FTPClient/#7d5a68d42a0b
diff -r a185787cdddf -r cd4afee8b093 MQTT.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MQTT.lib Mon Jul 16 09:25:11 2018 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/jamesabruce/code/MQTT/#d3feba7f242a
diff -r a185787cdddf -r cd4afee8b093 WIZnetInterface.lib --- a/WIZnetInterface.lib Thu Dec 14 05:35:51 2017 +0000 +++ b/WIZnetInterface.lib Mon Jul 16 09:25:11 2018 +0000 @@ -1,1 +1,1 @@ -https://os.mbed.com/users/akshaytom/code/WIZnetInterface/#584efa51ee60 +https://os.mbed.com/users/Albinarackal/code/WIZnetInterface/#01ea4b74d8eb
diff -r a185787cdddf -r cd4afee8b093 filelib.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/filelib.h Mon Jul 16 09:25:11 2018 +0000 @@ -0,0 +1,127 @@ +#ifndef filelib +#define filelib + +#include "mbed.h" +#include <time.h> +#include <ff.h> //found in lib SDFileSystem->FATFileSystem/ChaN +#include "SDFileSystem.h" + + +/* +typedef enum { + FR_OK = 0, // (0) Succeeded + FR_DISK_ERR, // (1) A hard error occurred in the low level disk I/O layer + FR_INT_ERR, // (2) Assertion failed + FR_NOT_READY, // (3) The physical drive cannot work + FR_NO_FILE, // (4) Could not find the file + FR_NO_PATH, // (5) Could not find the path + FR_INVALID_NAME, // (6) The path name format is invalid + FR_DENIED, // (7) Access denied due to prohibited access or directory full + FR_EXIST, // (8) Access denied due to prohibited access + FR_INVALID_OBJECT, // (9) The file/directory object is invalid + FR_WRITE_PROTECTED, // (10) The physical drive is write protected + FR_INVALID_DRIVE, // (11) The logical drive number is invalid + FR_NOT_ENABLED, // (12) The volume has no work area + FR_NO_FILESYSTEM, // (13) There is no valid FAT volume + FR_MKFS_ABORTED, // (14) The f_mkfs() aborted due to any parameter error + FR_TIMEOUT, // (15) Could not get a grant to access the volume within defined period + FR_LOCKED, // (16) The operation is rejected according to the file sharing policy + FR_NOT_ENOUGH_CORE, // (17) LFN working buffer could not be allocated + FR_TOO_MANY_OPEN_FILES, // (18) Number of open files > _FS_SHARE + FR_INVALID_PARAMETER // (19) Given parameter is invalid +} FRESULT; + +*/ +/* +mode_t values in octal + S_IFREG 0100000 + S_IFDIR 040000 + S_IRUSR 0400 read permission, owner + S_IWUSR 0200 write permission, owner + S_IXUSR 0100 execute/search permission, owner + S_IRGRP 040 read permission, group + S_IWGRP 020 write permission, group + S_IXGRP 010 execute/search permission, group + S_IROTH 04 read permission, others + S_IWOTH 02 write permission, others + S_IXOTH 01 execute/search permission, others +*/ +struct sMystat { +// dev_t st_dev; /* ID of device containing file */ +// ino_t st_ino; /* inode number */ + mode_t st_mode; /* protection */ +// nlink_t st_nlink; /* number of hard links */ +// uid_t st_uid; /* user ID of owner */ +// gid_t st_gid; /* group ID of owner */ +// dev_t st_rdev; /* device ID (if special file) */ + // off_t st_size; /* total size, in bytes */ + DWORD st_size; /* total size, in bytes */ +// blksize_t st_blksize; /* blocksize for file system I/O */ +// blkcnt_t st_blocks; /* number of 512B blocks allocated */ +// time_t st_atime; /* time of last access */ + time_t st_mtime; /* time of last modification */ +// time_t st_ctime; /* time of last status change */ +}; + +sMystat myStatBuf; //store info for file +FILINFO finfo; //global file info struct see ff.h +FATFS_DIR dinfo; //global directoty info struct see ff.h + +FRESULT get_fileInfo(const char *path) //use finfo for get result fields +{ + FRESULT res = f_stat(path,&finfo); /* Get file status */ + if (EnDebugMSG) + if (res) + printf("\r\n-->get_fileInfo:%s ,res=%d ,Not Found!",path,res); + else + printf("\r\n-->get_fileInfo:%s ,res=%d ,Found!",path,res); + return res; +} + +FRESULT get_dirInfo(const char *path) +{ + FRESULT res= f_opendir (&dinfo,path); /* FR_OK(0): successful, !=0: error code */ + if (EnDebugMSG) + if (res) + printf("\r\n-->get_dirInfo :%s res=%d ,This is Not Directory!",path,res); + else + printf("\r\n-->get_dirInfo :%s res=%d ,This is Directory!",path,res); + return res; +} + +FRESULT Mystat(const char *path, struct sMystat *buf) +{ + FRESULT res = f_stat(path,&finfo); /* Get file status */ + if (res == FR_OK) { + buf->st_size = finfo.fsize; + buf->st_mtime = finfo.ftime; //fdate; + buf->st_mode = 4; //? + } + if (EnDebugMSG) + printf("\r\n--Mystat Path:%s ,filesize:%14lld ,res=%d",path, buf->st_size, res); + return res; +} + + +static char* get_mime_type( char* filename ) +{ + char* extension; + extension = strrchr( filename, '.' ); //get string after last . + if (EnDebugMSG) + 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"; + if (strcasecmp(extension,".png")==0) + return "image/png"; + if (strcasecmp(extension,".css")==0) + return "text/css"; + if (strcasecmp(extension,".gif")==0) + return "image/gif"; + if (strcasecmp(extension,".jpg")==0 || strcasecmp(extension,".jpeg" )==0) + return "image/jpeg"; + } + return "text/plain; charset=iso-8859-1"; +} + +#endif
diff -r a185787cdddf -r cd4afee8b093 main.cpp --- a/main.cpp Thu Dec 14 05:35:51 2017 +0000 +++ b/main.cpp Mon Jul 16 09:25:11 2018 +0000 @@ -1,66 +1,258 @@ #include "mbed.h" #include "SDFileSystem.h" +#include "Camera_LS_Y201.h" + + +#define EnDebugMSG true //true-> print debug message to PC USB terminal, false->not print +#include "filelib.h" + +#define NEWLINE() pc.printf("\r\n") +#define DEBMSG pc.printf + + +#include "MQTTEthernet.h" +#include "MQTTClient.h" +#define ECHO_SERVER_PORT 7 + #include "EthernetInterface.h" #include "FTPClient.h" #include <string.h> -#include <stdio.h> #define FTP_SERVER_PORT 21 - static char buf[256]; -static char ID[]={"FTP"}; //Set FTPServer Login ID -static char PASSWORD[]={"user"}; user //Set FTPServer Login Password -FTPClient FTP(PB_3, PB_2, PB_1, PB_0, "sd"); // WIZwiki-W7500 -Serial pc(USBTX, USBRX); -int main() { - pc.baud(9600); +#define FILENAME "/sd/IMG_%04d.jpg" +#define FILENAME_FTP "IMG_%d.jpg" + +Serial pc(USBTX,USBRX); + + +FTPClient FTP(PB_3, PB_2, PB_1, PB_0, "sd"); // WIZwiki-W7500 + +Camera_LS_Y201 cam1(D1,D0); //rx tx + + + +Timer t; + +typedef struct work { + FILE *fp; +} work_t; + +/*****************MQTT fun def START******************/ +MQTTEthernet ipstack = MQTTEthernet(); + +MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack); +MQTT::Message message; + +void messageArrived(MQTT::MessageData& md) +{ + MQTT::Message &message = md.message; + printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id); + printf("Payload %.*s\n", message.payloadlen, (char*)message.payload); +} + +/*****************MQTT fun def END******************/ + +work_t work; + +int take_picture = 0; +char fname[64]; +char fname_FTP[64]; +/** + * 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; +} + + +int main() +{ + pc.baud(9600); - pc.printf("------------------------------FTP Client Example-------------------------------------------!\r\n"); - - - char ftpServer_control_ip_addr[] = "192.168.0.100"; // FTP Server location - char* userid = "FTP"; //FTP Server User ID - char* pass = "user"; //FTP Server Password + + char* copyFileName; + char *fileName ; + + //CAMERA MODULE CAPTURE IMAGE + + 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); + + /*****************************FTP CLIENT CONFIGURATION START HERE AND CONNECTING TO SERVER*******************/ + + pc.printf("------------------------------FTP Client Example-------------------------------------------!\r\n"); + + + char ftpServer_control_ip_addr[] = "172.16.73.33"; // FTP Server location + //char* userid = "FTP"; //FTP Server User ID + //char* pass = "user"; //FTP Server Password EthernetInterface eth; uint8_t mac_addr[6] = {0x00, 0x08, 0xdc, 0x12, 0x34, 0x45}; - char IP_Addr[] = "192.168.0.101"; + char IP_Addr[] = "172.16.73.37"; char IP_Subnet[] = "255.255.255.0"; - char IP_Gateway[] = "192.168.0.1"; + char IP_Gateway[] = "172.16.73.254"; eth.init(mac_addr, IP_Addr, IP_Subnet, IP_Gateway); //Use Static eth.connect(); pc.printf("\nThe IP address of the client is %s\r\n",eth.getIPAddress()); - + bool n = FTP.open("172.16.73.33", 21,"user1","user1"); + + /*****************************FTP CLIENT CONFIGURATION END HERE AND CONNECTION IS ALIVE *******************/ + + /*****************************MQTT CONFIGURATION START FROM HERE *****************************************/ + + printf("Wait a second...\r\n"); + char* topic = "openhab/parents/command"; + + + char* hostname = "172.16.73.1"; + int port = 1883; + + int rc = ipstack.connect(hostname, port); + if (rc != 0) + printf("rc from TCP connect is %d\n", rc); + + printf("Topic: %s\r\n",topic); + + MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + data.MQTTVersion = 3; + data.clientID.cstring = "parents"; + + if ((rc = client.connect(data)) == 0) + printf("rc from MQTT connect is %d\n", rc); + + + /********************MQTT CONFIGURATION END FROM HERE********************/ + + float count; + int nameOfimage; + int cnt = 0; + while(1) { - //char Msg_c = pc.getc(); - - //if(Msg_c==0x31) - //{ - - pc.printf("\nConnecting...FTPServer\r\nIP:%s, PORT:%d\r\n", ftpServer_control_ip_addr, FTP_SERVER_PORT); - //pc.printf("Test\n"); - - //FTP.open("demo.wftpserver.com"); - - bool n = FTP.open("192.168.0.100", 21,"FTP","user"); - - //pc.printf("%d\r\n",n); - wait(10); - printf("\nThe Files and folders available in the server are :\r\n"); - FTP.ls(); - printf("\r\n"); - wait(10); - printf("The Files in upload folder are :\r\n"); - FTP.cd("/upload"); - FTP.ls(); - wait(10); - printf("The Files in download folder are :\r\n"); - FTP.cd("/download"); - FTP.ls(); - printf("\r\n"); - wait(10); - - // } + + t.start(); + /******************CAPTURE THE IMAGAE FROM CAMERA*******************/ + + + 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."); + } + + + /******************CAPTURE END*******************/ - - } - + pc.printf("\nConnecting...FTPServer\r\nIP:%s, PORT:%d\r\n", ftpServer_control_ip_addr, FTP_SERVER_PORT); + printf("%d\r\n",n); + wait(2); + count = t.read(); + nameOfimage = count *1000000; + snprintf(fname_FTP, sizeof(fname_FTP), FILENAME_FTP, nameOfimage ); + + t.stop(); + printf("The file name is : %s \n and our filename is :%s\n", fname, fname_FTP); + FTP.putfile(fname,fname_FTP); + pc.printf("\r\n"); + wait(2); + + message.qos = MQTT::QOS0; + message.retained = false; + message.dup = false; + + printf("filename over MQTT is : %s\n", fname_FTP); + message.payload =(void *)fname_FTP; + message.payloadlen = strlen(fname_FTP); + + rc = client.publish("cdi/laxmi", message); + pc.printf("send via MQTT\n"); + + cnt++; + + + } + } \ No newline at end of file
diff -r a185787cdddf -r cd4afee8b093 mbed-src.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-src.lib Mon Jul 16 09:25:11 2018 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-src/#2d5fc5624619
diff -r a185787cdddf -r cd4afee8b093 mbed.bld --- a/mbed.bld Thu Dec 14 05:35:51 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/e7ca05fa8600 \ No newline at end of file