Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #include "Camera_LS_Y201.h" 00003 //#include <string.h> 00004 //#include "GPS.h" 00005 #define DEBMSG printf 00006 #define NEWLINE() printf("\r\n") 00007 00008 #define BUFF_SIZE 32 00009 #define USE_SDCARD 0 00010 00011 00012 #if USE_SDCARD 00013 #define FILENAME "/sd/IMG_%04d.jpg" 00014 //SDFileSystem fs(p5, p6, p7, p8, "sd"); 00015 #else 00016 #define FILENAME "/local/ALT%d_%02d.jpg" 00017 LocalFileSystem fs("local"); 00018 #endif 00019 Camera_LS_Y201 cam1(p28, p27); 00020 #define DEPTH 5 00021 //GPS gps(p9,p10); 00022 Serial gsm(p13,p14); 00023 Serial pc(USBTX,USBRX); 00024 AnalogIn motion(p15); 00025 00026 float reading; 00027 int alert_num = 0; 00028 char phone_num[11]; 00029 00030 typedef struct work { 00031 FILE *fp; 00032 } work_t; 00033 00034 work_t work; 00035 00036 /** 00037 * Callback function for readJpegFileContent. 00038 * 00039 * @param buf A pointer to a buffer. 00040 * @param siz A size of the buffer. 00041 */ 00042 void callback_func(int done, int total, uint8_t *buf, size_t siz) 00043 { 00044 fwrite(buf, siz, 1, work.fp); 00045 00046 } 00047 00048 00049 /** 00050 * Capture. 00051 * 00052 * @param cam A pointer to a camera object. 00053 * @param filename The file name. 00054 * 00055 * @return Return 0 if it succeed. 00056 */ 00057 int capture(Camera_LS_Y201 *cam, char *filename) 00058 { 00059 /* 00060 * Take a picture. 00061 */ 00062 if (cam->takePicture() != 0) { 00063 return -1; 00064 } 00065 //DEBMSG("Captured."); 00066 //NEWLINE(); 00067 00068 /* 00069 * Open file. 00070 */ 00071 work.fp = fopen(filename, "wb"); 00072 if (work.fp == NULL) { 00073 return -2; 00074 } 00075 00076 /* 00077 * Read the content. 00078 */ 00079 //DEBMSG("%s", filename); 00080 //NEWLINE(); 00081 if (cam->readJpegFileContent(callback_func) != 0) 00082 { 00083 fclose(work.fp); 00084 return -3; 00085 } 00086 fclose(work.fp); 00087 00088 /* 00089 * Stop taking pictures. 00090 */ 00091 cam->stopTakingPictures(); 00092 00093 return 0; 00094 } 00095 00096 void print_pics() 00097 { 00098 00099 int cnt = 0; 00100 int c = 0; 00101 while (cnt < 4) 00102 { 00103 00104 char fname[64]; 00105 snprintf(fname, sizeof(fname) - 1, FILENAME, alert_num, cnt); 00106 int r = capture(&cam1, fname); 00107 if (r == 0) 00108 { 00109 ++c; 00110 // DEBMSG("[%04d]:OK.", cnt); 00111 //NEWLINE(); 00112 } 00113 else 00114 { 00115 DEBMSG("[%04d]:NG. (code=%d)", cnt, r); 00116 NEWLINE(); 00117 error("Failure."); 00118 } 00119 cnt++; 00120 } 00121 if(c==4) 00122 { 00123 printf("All pictures taken \n"); 00124 } 00125 00126 return; 00127 } 00128 00129 void send_text() 00130 { 00131 char buf[40]; 00132 char buf2= 0x1A; 00133 00134 gsm.printf("AT\r\n"); //Should return OK 00135 // gsm.scanf("%s",buf); 00136 // pc.printf("%s\n",buf); 00137 00138 //gsm.scanf("%s",buf1); 00139 //pc.printf("%s\n",buf1); 00140 wait(.5); 00141 gsm.printf("AT+CSMP=17,167,0,0\r\n"); //Set text mode parameters =<fo>,<vp>,<pid>,<dcs> 00142 //gsm.scanf("%s",buf); // Should return OK 00143 //gsm.scanf("%s",buf1); 00144 //pc.printf("%s\n",buf); 00145 //pc.printf("%s\n",buf1); 00146 wait(.5); 00147 gsm.printf("AT+CMGF=1\r\n"); //1 - text mode, indicates the format of messages used with send, list, read and write commands 00148 //gsm.scanf("%s",buf); 00149 // gsm.scanf("%s",buf1); 00150 //pc.printf("%s\n",buf); 00151 //pc.printf("%s\n",buf1); 00152 wait(.5); 00153 while (1) 00154 { 00155 //pc.printf("message sent"); 00156 gsm.printf("AT+CMGS=\"%s\"\r\n", phone_num); //AT+CMGS=”<da>"...da - destination address 00157 // gsm.scanf("%s",buf); //Response OK 00158 // gsm.scanf("%s",buf1); 00159 //pc.printf("%s\n",buf); 00160 //pc.printf("%s\n",buf1); 00161 wait(.5); 00162 gsm.printf("Alert message #%d %c \r\n", alert_num, buf2); //Actual message following CMGS command followed by ctrl+z (buf2) 00163 //gsm.scanf("%s",buf); //Response OK 00164 // gsm.scanf("%s",buf1); 00165 //pc.printf("%s\n",buf); 00166 //pc.printf("%s\n",buf1); 00167 pc.printf("Message sent \n"); 00168 00169 break; 00170 } 00171 } 00172 00173 void sound_alarm() 00174 { 00175 ++alert_num; 00176 pc.printf("\nSound alarm...alert alert\n"); 00177 pc.printf("The value was: %f \n", reading); 00178 pc.printf("4 Pictures being taken... \n"); 00179 print_pics(); 00180 pc.printf("Pictures have been taken!\n"); 00181 pc.printf("Sending text alert...\n"); 00182 send_text(); 00183 00184 00185 00186 pc.printf("Re-engaging in 10 seconds... \n"); 00187 wait(10); 00188 return; 00189 } 00190 00191 void init_cam() 00192 { 00193 if (cam1.reset() == 0) 00194 { 00195 DEBMSG("Reset OK."); 00196 NEWLINE(); 00197 } 00198 else 00199 { 00200 DEBMSG("Reset fail."); 00201 NEWLINE(); 00202 error("Reset fail."); 00203 } 00204 return; 00205 } 00206 00207 00208 int main() 00209 { 00210 gsm.baud(115200); 00211 pc.baud(115200); 00212 //pc.printf("Testing..."); 00213 //send_text(); 00214 00215 char user_input[5] = ""; 00216 // user_input = ""; 00217 00218 float moving_avg[DEPTH]; 00219 double avg; 00220 00221 // 00222 while(strcmp(user_input, "y") != 0) 00223 { 00224 pc.printf("Initialize Security System (y/n) \n"); 00225 pc.scanf("%s", user_input); 00226 pc.printf("Input phone number to send alerts\n"); 00227 pc.scanf("%s", phone_num); 00228 } 00229 pc.printf("Initializing...\n"); 00230 init_cam(); 00231 00232 int i = 0; 00233 while(1) 00234 { 00235 //Moving average for motion detection 00236 //moving_avg[i] = motion.read(); 00237 reading = motion.read(); 00238 if(i>=DEPTH) //Check for difference versus moving avg 00239 { 00240 if(i == DEPTH) 00241 pc.printf("Security System Engaged! \n"); 00242 avg = 0; 00243 00244 for(int j=0; j < DEPTH; ++j) 00245 avg = avg + moving_avg[j]; 00246 avg = avg/DEPTH; 00247 pc.printf("Average = %f", avg); 00248 if((reading > 1.2 * avg)||(reading < .8 *avg)) 00249 { 00250 sound_alarm(); 00251 continue; 00252 } 00253 else 00254 moving_avg[i%DEPTH] = reading; 00255 } 00256 else 00257 moving_avg[i%DEPTH] = reading; 00258 00259 ++i; 00260 pc.printf("Motion value: %f\n", reading); 00261 wait(.4); 00262 } 00263 00264 return 0; 00265 }
Generated on Fri Jul 15 2022 22:13:09 by
1.7.2