v2 for lunchbox

Dependencies:   HTTPClient PWM_Tone_Library PinDetect_KL25Z Queue cc3000_hostdriver_mbedsocket mbed

Fork of kragl by The Kragl

Files at this revision

API Documentation at this revision

Comitter:
inezraharjo
Date:
Wed Dec 02 19:30:05 2015 +0000
Parent:
3:6641e741733c
Commit message:
Reduced delays! for turning on/off alarm by opening box and from app/entering code

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Dec 02 18:32:02 2015 +0000
+++ b/main.cpp	Wed Dec 02 19:30:05 2015 +0000
@@ -58,6 +58,8 @@
 DigitalOut warningLED(A4);
 DigitalOut correctLED(A5);
 
+int changedFromOFF = 0;
+int changedFromON = 0;
 int code[] = {0, 0, 0, 0};
 int passwordcounter = 0;
 bool codeEntered = false;
@@ -191,6 +193,10 @@
     pc.printf("Reed Open\r\n");
     pc.printf("boxOpenLegal: %d\r\n", boxOpenLegal);
     reedIsOpen = 1;
+    if (!boxOpenLegal){
+        Buzzer = 1;
+        changedFromOFF = 1;
+    }    
 }
 
 void reedClosed( void )
@@ -198,6 +204,7 @@
     pc.printf("Reed Closed\n");
     reedIsOpen = 0;
     if (boxOpenLegal==true){
+        changedFromON = 1;
         correctLED =0;
         for (int j=0; j<8; j++){
             warningLED =1;
@@ -231,7 +238,7 @@
     // print message to indicate the program has started
     pc.printf("CC3000 Lunch Theft\r\n");
     wifi.init();
-
+    
     p1.mode(PullUp);
     p2.mode(PullUp);
     p3.mode(PullUp);
@@ -265,6 +272,9 @@
     wait_ms(1000);
     Buzzer = 0;
     
+    changedFromOFF = 0;
+    changedFromON = 0;
+    
     clock_t start_t, current_t, total_t;
     //int counterforpasswordcheck = 0;
     //int i;
@@ -353,6 +363,9 @@
                             Buzzer = 0;
                         } else {
                             boxOpenLegal = false;
+                            if (reedIsOpen){
+                                Buzzer = 1;
+                            }
                             correctLED = 0;
                                 for (int j=0; j<8; j++){
                                 warningLED =1;
@@ -381,19 +394,38 @@
             codeEntered = false;
             Buzzer = 0;
             
+            if (changedFromON){
+            pc.printf("SENDING TO URL\n");
+            sprintf(url, "http://www.charlesding.net/kragl/lunchbox.php?isOpen=NO&alarmOn=OFF&boxLocked=YES");
+            
+            int ret = http.get(url, str, 128);
+            pc.printf("Trying to Request %s\r\n", url);
+            int tries = 20;
+            while (tries > 0) {
+                ret = http.get(url, str, 128);
+                if (!ret) {
+                    pc.printf("Requested %s\r\n", url);
+                    pc.printf("Page fetched successfully - read %d characters\r\n",
+                              strlen(str));
+                    pc.printf("Result: %s\r\n", str);
+                    break;
+                } else {
+                    pc.printf("Error - ret = %d - HTTP return code = %d\r\n",
+                              ret,
+                              http.getHTTPResponseCode());
+                    tries--;
+                }
+            }
+            changedFromON = 0;
+            }
+            
+            
         } else if (reedIsOpen && !boxOpenLegal) {
             // turn on alarm;
             pc.printf("boxOpenLegal: %d\r\n", boxOpenLegal);
             pc.printf("reedIsOpen: %d\r\n", reedIsOpen);
             pc.printf("ALARM TIME\r\n");
             Buzzer = 1;
-            
-            /*
-            for(i=0; i<tones_num; i++) {
-                Auto_tunes(Buzzer, tones[i], 4); // Auto performance
-                Stop_tunes(Buzzer);
-            }
-            */
 
             if(wifi.is_connected() == false) {
                 // try to connect
@@ -409,6 +441,7 @@
                 }
             } else {
                 // get input url and then return the value
+                if (changedFromOFF){
                 pc.printf("SENDING TO URL\n");
                 sprintf(url, "http://www.charlesding.net/kragl/lunchbox.php?isOpen=YES1&alarmOn=ON&boxLocked=NO");
 
@@ -430,73 +463,20 @@
                     }
                     ret = http.get(url, str, 128);
                 }
+                changedFromOFF = 0;
+                }
             }
             //wait(0.3);
         } else if (!reedIsOpen && boxOpenLegal) {
             Buzzer = 0;
-            boxOpenLegal = false;
-            pc.printf("SENDING TO URL\n");
-            sprintf(url, "http://www.charlesding.net/kragl/lunchbox.php?isOpen=NO&alarmOn=OFF&boxLocked=YES");
+            // boxOpenLegal = false;
             
-            int ret = http.get(url, str, 128);
-            pc.printf("Trying to Request %s\r\n", url);
-            int tries = 20;
-            while (tries > 0) {
-                ret = http.get(url, str, 128);
-                if (!ret) {
-                    pc.printf("Requested %s\r\n", url);
-                    pc.printf("Page fetched successfully - read %d characters\r\n",
-                              strlen(str));
-                    pc.printf("Result: %s\r\n", str);
-                    break;
-                } else {
-                    pc.printf("Error - ret = %d - HTTP return code = %d\r\n",
-                              ret,
-                              http.getHTTPResponseCode());
-                    tries--;
-                }
-            }
+            
             //wait(0.3);
         }
         if (!codeEntered && passwordcounter==sizeof(code)/4) {
             codeEntered = true;
-            /*
-            sprintf(url, "http://www.charlesding.net/kragl/pincode_read.php?");
-            int ret2 = http.get(url, str, 128);
-            
-            pc.printf("Trying to Request %s\r\n", url);
-            int tries = 20;
-            char* s;
-            while (tries > 0) {
-                pc.printf("try %d\n",tries);
-                if (!ret2) {
-                    pc.printf("Requested %s\r\n", url);
-                    pc.printf("Page fetched successfully - read %d characters\r\n",
-                            strlen(str));
-                    pc.printf("Result: %s\r\n", str);
-                    s = strtok(str, "\n");
-                    pc.printf("code string: %s\r\n",s);
-                    int num;
-                    
-                    if (true){
-                        for (int c=1; c<5; c++){
-                            char test[] = {s[c]};
-                            num = atoi(test);
-                            password[c-1] = num;    
-                        }
-                    }     
-                    pc.printf("Password in the system: %d%d%d%d", password[0], password[1], password[2], password[3]);           
-                    break;
-                } else {
-                    pc.printf("Error - ret = %d - HTTP return code = %d\r\n",
-                            ret2,
-                            http.getHTTPResponseCode());
-                }
-                tries--;
-                ret2 = http.get(url, str, 128);
-            }
-            //wait(0.3);   
-            */
+           
             for (int j=0; j<sizeof(code)/4; j++) {
                 if (code[j]!=password[j]) {
                     codeEntered = false;