Lab3_Surveillance

Dependencies:   GPS mbed

Committer:
gtg846r
Date:
Thu Oct 13 05:48:32 2011 +0000
Revision:
0:2af1e48a285d
Child:
1:65200fe545a5

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gtg846r 0:2af1e48a285d 1 #include "mbed.h"
gtg846r 0:2af1e48a285d 2 #include "Camera_LS_Y201.h"
gtg846r 0:2af1e48a285d 3 //#include <string.h>
gtg846r 0:2af1e48a285d 4 //#include "GPS.h"
gtg846r 0:2af1e48a285d 5 #define DEBMSG printf
gtg846r 0:2af1e48a285d 6 #define NEWLINE() printf("\r\n")
gtg846r 0:2af1e48a285d 7
gtg846r 0:2af1e48a285d 8 #define BUFF_SIZE 32
gtg846r 0:2af1e48a285d 9 #define USE_SDCARD 0
gtg846r 0:2af1e48a285d 10
gtg846r 0:2af1e48a285d 11
gtg846r 0:2af1e48a285d 12 #if USE_SDCARD
gtg846r 0:2af1e48a285d 13 #define FILENAME "/sd/IMG_%04d.jpg"
gtg846r 0:2af1e48a285d 14 //SDFileSystem fs(p5, p6, p7, p8, "sd");
gtg846r 0:2af1e48a285d 15 #else
gtg846r 0:2af1e48a285d 16 #define FILENAME "/local/ALT%d_%02d.jpg"
gtg846r 0:2af1e48a285d 17 LocalFileSystem fs("local");
gtg846r 0:2af1e48a285d 18 #endif
gtg846r 0:2af1e48a285d 19 Camera_LS_Y201 cam1(p28, p27);
gtg846r 0:2af1e48a285d 20 #define DEPTH 5
gtg846r 0:2af1e48a285d 21 //GPS gps(p9,p10);
gtg846r 0:2af1e48a285d 22 Serial gsm(p13,p14);
gtg846r 0:2af1e48a285d 23 Serial pc(USBTX,USBRX);
gtg846r 0:2af1e48a285d 24 AnalogIn motion(p15);
gtg846r 0:2af1e48a285d 25
gtg846r 0:2af1e48a285d 26 float reading;
gtg846r 0:2af1e48a285d 27 int alert_num = 0;
gtg846r 0:2af1e48a285d 28
gtg846r 0:2af1e48a285d 29 typedef struct work {
gtg846r 0:2af1e48a285d 30 FILE *fp;
gtg846r 0:2af1e48a285d 31 } work_t;
gtg846r 0:2af1e48a285d 32
gtg846r 0:2af1e48a285d 33 work_t work;
gtg846r 0:2af1e48a285d 34
gtg846r 0:2af1e48a285d 35 /**
gtg846r 0:2af1e48a285d 36 * Callback function for readJpegFileContent.
gtg846r 0:2af1e48a285d 37 *
gtg846r 0:2af1e48a285d 38 * @param buf A pointer to a buffer.
gtg846r 0:2af1e48a285d 39 * @param siz A size of the buffer.
gtg846r 0:2af1e48a285d 40 */
gtg846r 0:2af1e48a285d 41 void callback_func(int done, int total, uint8_t *buf, size_t siz)
gtg846r 0:2af1e48a285d 42 {
gtg846r 0:2af1e48a285d 43 fwrite(buf, siz, 1, work.fp);
gtg846r 0:2af1e48a285d 44
gtg846r 0:2af1e48a285d 45 }
gtg846r 0:2af1e48a285d 46
gtg846r 0:2af1e48a285d 47
gtg846r 0:2af1e48a285d 48 /**
gtg846r 0:2af1e48a285d 49 * Capture.
gtg846r 0:2af1e48a285d 50 *
gtg846r 0:2af1e48a285d 51 * @param cam A pointer to a camera object.
gtg846r 0:2af1e48a285d 52 * @param filename The file name.
gtg846r 0:2af1e48a285d 53 *
gtg846r 0:2af1e48a285d 54 * @return Return 0 if it succeed.
gtg846r 0:2af1e48a285d 55 */
gtg846r 0:2af1e48a285d 56 int capture(Camera_LS_Y201 *cam, char *filename)
gtg846r 0:2af1e48a285d 57 {
gtg846r 0:2af1e48a285d 58 /*
gtg846r 0:2af1e48a285d 59 * Take a picture.
gtg846r 0:2af1e48a285d 60 */
gtg846r 0:2af1e48a285d 61 if (cam->takePicture() != 0) {
gtg846r 0:2af1e48a285d 62 return -1;
gtg846r 0:2af1e48a285d 63 }
gtg846r 0:2af1e48a285d 64 DEBMSG("Captured.");
gtg846r 0:2af1e48a285d 65 NEWLINE();
gtg846r 0:2af1e48a285d 66
gtg846r 0:2af1e48a285d 67 /*
gtg846r 0:2af1e48a285d 68 * Open file.
gtg846r 0:2af1e48a285d 69 */
gtg846r 0:2af1e48a285d 70 work.fp = fopen(filename, "wb");
gtg846r 0:2af1e48a285d 71 if (work.fp == NULL) {
gtg846r 0:2af1e48a285d 72 return -2;
gtg846r 0:2af1e48a285d 73 }
gtg846r 0:2af1e48a285d 74
gtg846r 0:2af1e48a285d 75 /*
gtg846r 0:2af1e48a285d 76 * Read the content.
gtg846r 0:2af1e48a285d 77 */
gtg846r 0:2af1e48a285d 78 DEBMSG("%s", filename);
gtg846r 0:2af1e48a285d 79 NEWLINE();
gtg846r 0:2af1e48a285d 80 if (cam->readJpegFileContent(callback_func) != 0)
gtg846r 0:2af1e48a285d 81 {
gtg846r 0:2af1e48a285d 82 fclose(work.fp);
gtg846r 0:2af1e48a285d 83 return -3;
gtg846r 0:2af1e48a285d 84 }
gtg846r 0:2af1e48a285d 85 fclose(work.fp);
gtg846r 0:2af1e48a285d 86
gtg846r 0:2af1e48a285d 87 /*
gtg846r 0:2af1e48a285d 88 * Stop taking pictures.
gtg846r 0:2af1e48a285d 89 */
gtg846r 0:2af1e48a285d 90 cam->stopTakingPictures();
gtg846r 0:2af1e48a285d 91
gtg846r 0:2af1e48a285d 92 return 0;
gtg846r 0:2af1e48a285d 93 }
gtg846r 0:2af1e48a285d 94
gtg846r 0:2af1e48a285d 95 void print_pics()
gtg846r 0:2af1e48a285d 96 {
gtg846r 0:2af1e48a285d 97 if (cam1.reset() == 0)
gtg846r 0:2af1e48a285d 98 {
gtg846r 0:2af1e48a285d 99 DEBMSG("Reset OK.");
gtg846r 0:2af1e48a285d 100 NEWLINE();
gtg846r 0:2af1e48a285d 101 }
gtg846r 0:2af1e48a285d 102 else
gtg846r 0:2af1e48a285d 103 {
gtg846r 0:2af1e48a285d 104 DEBMSG("Reset fail.");
gtg846r 0:2af1e48a285d 105 NEWLINE();
gtg846r 0:2af1e48a285d 106 error("Reset fail.");
gtg846r 0:2af1e48a285d 107 }
gtg846r 0:2af1e48a285d 108
gtg846r 0:2af1e48a285d 109 int cnt = 0;
gtg846r 0:2af1e48a285d 110 while (cnt < 4)
gtg846r 0:2af1e48a285d 111 {
gtg846r 0:2af1e48a285d 112 char fname[64];
gtg846r 0:2af1e48a285d 113 snprintf(fname, sizeof(fname) - 1, FILENAME, alert_num, cnt);
gtg846r 0:2af1e48a285d 114 int r = capture(&cam1, fname);
gtg846r 0:2af1e48a285d 115 if (r == 0)
gtg846r 0:2af1e48a285d 116 {
gtg846r 0:2af1e48a285d 117 DEBMSG("[%04d]:OK.", cnt);
gtg846r 0:2af1e48a285d 118 NEWLINE();
gtg846r 0:2af1e48a285d 119 }
gtg846r 0:2af1e48a285d 120 else
gtg846r 0:2af1e48a285d 121 {
gtg846r 0:2af1e48a285d 122 DEBMSG("[%04d]:NG. (code=%d)", cnt, r);
gtg846r 0:2af1e48a285d 123 NEWLINE();
gtg846r 0:2af1e48a285d 124 error("Failure.");
gtg846r 0:2af1e48a285d 125 }
gtg846r 0:2af1e48a285d 126 cnt++;
gtg846r 0:2af1e48a285d 127 }
gtg846r 0:2af1e48a285d 128
gtg846r 0:2af1e48a285d 129 return;
gtg846r 0:2af1e48a285d 130 }
gtg846r 0:2af1e48a285d 131
gtg846r 0:2af1e48a285d 132 void send_text()
gtg846r 0:2af1e48a285d 133 {
gtg846r 0:2af1e48a285d 134 char buf[40];
gtg846r 0:2af1e48a285d 135 char buf2= 0x1A;
gtg846r 0:2af1e48a285d 136
gtg846r 0:2af1e48a285d 137 gsm.printf("AT\r\n"); //Should return OK
gtg846r 0:2af1e48a285d 138 gsm.scanf("%s",buf);
gtg846r 0:2af1e48a285d 139 pc.printf("%s\n",buf);
gtg846r 0:2af1e48a285d 140 //gsm.scanf("%s",buf1);
gtg846r 0:2af1e48a285d 141 //pc.printf("%s\n",buf1);
gtg846r 0:2af1e48a285d 142
gtg846r 0:2af1e48a285d 143 gsm.printf("AT+CSMP=17,167,0,0\r\n"); //Set text mode parameters =<fo>,<vp>,<pid>,<dcs>
gtg846r 0:2af1e48a285d 144 gsm.scanf("%s",buf); // Should return OK
gtg846r 0:2af1e48a285d 145 //gsm.scanf("%s",buf1);
gtg846r 0:2af1e48a285d 146 pc.printf("%s\n",buf);
gtg846r 0:2af1e48a285d 147 //pc.printf("%s\n",buf1);
gtg846r 0:2af1e48a285d 148
gtg846r 0:2af1e48a285d 149 gsm.printf("AT+CMGF=1\r\n"); //1 - text mode, indicates the format of messages used with send, list, read and write commands
gtg846r 0:2af1e48a285d 150 gsm.scanf("%s",buf);
gtg846r 0:2af1e48a285d 151 // gsm.scanf("%s",buf1);
gtg846r 0:2af1e48a285d 152 pc.printf("%s\n",buf);
gtg846r 0:2af1e48a285d 153 //pc.printf("%s\n",buf1);
gtg846r 0:2af1e48a285d 154
gtg846r 0:2af1e48a285d 155 while (1)
gtg846r 0:2af1e48a285d 156 {
gtg846r 0:2af1e48a285d 157 //pc.printf("message sent");
gtg846r 0:2af1e48a285d 158 gsm.printf("AT+CMGS=\"16784370074\"\r\n"); //AT+CMGS=”<da>"...da - destination address
gtg846r 0:2af1e48a285d 159 gsm.scanf("%s",buf); //Response OK
gtg846r 0:2af1e48a285d 160 // gsm.scanf("%s",buf1);
gtg846r 0:2af1e48a285d 161 pc.printf("%s\n",buf);
gtg846r 0:2af1e48a285d 162 //pc.printf("%s\n",buf1);
gtg846r 0:2af1e48a285d 163
gtg846r 0:2af1e48a285d 164 gsm.printf("Alert message %c \r\n", buf2); //Actual message following CMGS command followed by ctrl+z (buf2)
gtg846r 0:2af1e48a285d 165 gsm.scanf("%s",buf); //Response OK
gtg846r 0:2af1e48a285d 166 // gsm.scanf("%s",buf1);
gtg846r 0:2af1e48a285d 167 pc.printf("%s\n",buf);
gtg846r 0:2af1e48a285d 168 //pc.printf("%s\n",buf1);
gtg846r 0:2af1e48a285d 169 pc.printf("Message sent \n");
gtg846r 0:2af1e48a285d 170
gtg846r 0:2af1e48a285d 171 break;
gtg846r 0:2af1e48a285d 172 }
gtg846r 0:2af1e48a285d 173 }
gtg846r 0:2af1e48a285d 174
gtg846r 0:2af1e48a285d 175 void sound_alarm()
gtg846r 0:2af1e48a285d 176 {
gtg846r 0:2af1e48a285d 177 ++alert_num;
gtg846r 0:2af1e48a285d 178 pc.printf("The value was: %f", reading);
gtg846r 0:2af1e48a285d 179 pc.printf("Sound alarm...alert alert\n");
gtg846r 0:2af1e48a285d 180 pc.printf("4 Pictures being taken \n");
gtg846r 0:2af1e48a285d 181 print_pics();
gtg846r 0:2af1e48a285d 182 send_text();
gtg846r 0:2af1e48a285d 183 pc.printf("Done \n");
gtg846r 0:2af1e48a285d 184 return;
gtg846r 0:2af1e48a285d 185 }
gtg846r 0:2af1e48a285d 186
gtg846r 0:2af1e48a285d 187
gtg846r 0:2af1e48a285d 188
gtg846r 0:2af1e48a285d 189
gtg846r 0:2af1e48a285d 190 int main()
gtg846r 0:2af1e48a285d 191 {
gtg846r 0:2af1e48a285d 192 gsm.baud(115200);
gtg846r 0:2af1e48a285d 193 pc.baud(115200);
gtg846r 0:2af1e48a285d 194 char user_input[5] = "";
gtg846r 0:2af1e48a285d 195 // user_input = "";
gtg846r 0:2af1e48a285d 196
gtg846r 0:2af1e48a285d 197 float moving_avg[DEPTH];
gtg846r 0:2af1e48a285d 198 double avg;
gtg846r 0:2af1e48a285d 199
gtg846r 0:2af1e48a285d 200 //
gtg846r 0:2af1e48a285d 201 while(strcmp(user_input, "y") != 0)
gtg846r 0:2af1e48a285d 202 {
gtg846r 0:2af1e48a285d 203 pc.printf("Initialize Security System (y/n) \n");
gtg846r 0:2af1e48a285d 204 pc.scanf("%s", user_input);
gtg846r 0:2af1e48a285d 205 }
gtg846r 0:2af1e48a285d 206 int i = 0;
gtg846r 0:2af1e48a285d 207 while(1)
gtg846r 0:2af1e48a285d 208 {
gtg846r 0:2af1e48a285d 209 //Moving average for motion detection
gtg846r 0:2af1e48a285d 210 //moving_avg[i] = motion.read();
gtg846r 0:2af1e48a285d 211 reading = motion.read();
gtg846r 0:2af1e48a285d 212 if(i>=DEPTH) //Check for difference versus moving avg
gtg846r 0:2af1e48a285d 213 {
gtg846r 0:2af1e48a285d 214 if(i == DEPTH)
gtg846r 0:2af1e48a285d 215 pc.printf("Security System Engaged! \n");
gtg846r 0:2af1e48a285d 216 avg = 0;
gtg846r 0:2af1e48a285d 217
gtg846r 0:2af1e48a285d 218 for(int j=0; j < DEPTH; ++j)
gtg846r 0:2af1e48a285d 219 avg = avg + moving_avg[j];
gtg846r 0:2af1e48a285d 220 avg = avg/DEPTH;
gtg846r 0:2af1e48a285d 221 pc.printf("Average = %f", avg);
gtg846r 0:2af1e48a285d 222 if((reading > 1.2 * avg)||(reading < .8 *avg))
gtg846r 0:2af1e48a285d 223 {
gtg846r 0:2af1e48a285d 224 sound_alarm();
gtg846r 0:2af1e48a285d 225 continue;
gtg846r 0:2af1e48a285d 226 }
gtg846r 0:2af1e48a285d 227 else
gtg846r 0:2af1e48a285d 228 moving_avg[i%DEPTH] = reading;
gtg846r 0:2af1e48a285d 229 }
gtg846r 0:2af1e48a285d 230 else
gtg846r 0:2af1e48a285d 231 moving_avg[i%DEPTH] = reading;
gtg846r 0:2af1e48a285d 232
gtg846r 0:2af1e48a285d 233 ++i;
gtg846r 0:2af1e48a285d 234 pc.printf("Motion value: %f\n", reading);
gtg846r 0:2af1e48a285d 235 wait(.5);
gtg846r 0:2af1e48a285d 236 }
gtg846r 0:2af1e48a285d 237
gtg846r 0:2af1e48a285d 238 return 0;
gtg846r 0:2af1e48a285d 239 }