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:
Mon Nov 30 01:39:27 2015 +0000
Parent:
1:90b792629d5e
Child:
3:6641e741733c
Commit message:
blinking LED

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Nov 26 04:17:27 2015 +0000
+++ b/main.cpp	Mon Nov 30 01:39:27 2015 +0000
@@ -54,7 +54,7 @@
 PinDetect p3(D7);
 PinDetect reed(A3);
 DigitalOut RGBindicator(D2);
-DigitalOut Buzzer(A2);
+DigitalOut Buzzer(D9);
 DigitalOut warningLED(A4);
 DigitalOut correctLED(A5);
 
@@ -197,9 +197,13 @@
     pc.printf("Reed Closed\n");
     reedIsOpen = 0;
     if (boxOpenLegal==true){
-        warningLED =1;
         correctLED =0;
-        boxOpenLegal = false;
+        for (int j=0; j<8; j++){
+            warningLED =1;
+            wait(0.15);
+            warningLED = 0;
+            wait(0.15);
+        }
     }
     Buzzer = 0;
 }
@@ -207,6 +211,7 @@
 void reedOpenHeld( void )
 {
     //pc.printf("Key Pressed Held\n");
+    reedIsOpen = 1;
 }
 
 int main()
@@ -214,7 +219,12 @@
     // by default, it's red
     led_red = 0;
     led_green = 1;
-    warningLED = 1;
+    for (int j=0; j<4; j++){
+        warningLED =1;
+        wait(0.5);
+        warningLED = 0;
+        wait(0.5);
+    }
     correctLED = 0;
     // print message to indicate the program has started
     pc.printf("CC3000 Lunch Theft\r\n");
@@ -245,7 +255,7 @@
 
     reed.attach_asserted( &reedClosed);
     reed.attach_deasserted( &reedOpen);
-    reed.attach_asserted_held( &reedOpenHeld);
+    reed.attach_deasserted_held( &reedOpenHeld);
     reed.setAssertValue( 0 );
     reed.setSampleFrequency( 10000 );
     
@@ -259,7 +269,12 @@
     int init = 0; // init wifi done
     start_t = clock();
 
+    reedIsOpen = (reed == 1);
     while (1) {
+        pc.printf("reed value:%d\r\n",reedIsOpen);
+        pc.printf("codeEntered:%d\r\n",codeEntered);
+        pc.printf("boxOpenLegal:%d\r\n",boxOpenLegal);        
+        
         if (init == 0) {
             if(wifi.is_connected() == false) {
                 // try to connect
@@ -299,7 +314,7 @@
         current_t = clock();
         total_t = (double)(current_t - start_t) / CLOCKS_PER_SEC;
         //pc.printf("total_t: %d\r\n", total_t);
-        if (!reedIsOpen && (total_t%2)==0){ ever t seconds
+        if ((total_t%1)==0){ //every t seconds
             pc.printf("SENDING TO URL\n");
                 sprintf(url, "http://www.charlesding.net/kragl/pincode_read.php?");
                 int ret = http.get(url, str, 128);
@@ -311,14 +326,30 @@
                         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);
+                        
                         char* s = strtok(str, "\n");
                         char test[] = {s[5]};
                         int num = atoi(test);
+                        pc.printf("Result last digit: %d\r\n", num);
                         if (num==1){
-                            codeEntered = true;
+                            boxOpenLegal = true;
+                            warningLED = 0;
+                            for (int j=0; j<8; j++){
+                                correctLED =1;
+                                wait(0.15);
+                                correctLED = 0;
+                                wait(0.15);
+                            }
+                            Buzzer = 0;
                         } else {
-                            codeEntered = false;
+                            boxOpenLegal = false;
+                            correctLED = 0;
+                                for (int j=0; j<8; j++){
+                                warningLED =1;
+                                wait(0.15);
+                                warningLED = 0;
+                                wait(0.15);
+                            }
                         }
                         break;
                     } else {
@@ -331,10 +362,7 @@
                     ret = http.get(url, str, 128);
                 }
             }
-            wait(0.3);
-            
-            
-          
+        pc.printf("waiting to check...\r\n");
         if (reedIsOpen && codeEntered) {
             pc.printf("LEGAL OPENING");
             boxOpenLegal = true;
@@ -466,11 +494,21 @@
                 codeEntered = true;
             }
             if (codeEntered==true){
-                correctLED = 1;
                 warningLED = 0;
+                for (int j=0; j<8; j++){
+                    correctLED =1;
+                    wait(0.15);
+                    correctLED = 0;
+                    wait(0.15);
+                }
             } else {
                 correctLED = 0;
-                warningLED = 1;
+                for (int j=0; j<8; j++){
+                    warningLED =1;
+                    wait(0.15);
+                    warningLED = 0;
+                    wait(0.15);
+                }
             }
 
 
@@ -479,14 +517,14 @@
 
             pc.printf("SENDING TO URL\n");
             if (codeEntered==false){
-            sprintf(url, "http://www.charlesding.net/kragl/lunchbox.php?isOpen=NO&alarmOn=OFF&authorized=NO");
+            sprintf(url, "http://www.charlesding.net/kragl/lunchbox.php?alarmOn=OFF&authorized=NO&openLock=2");
             } else {
-            sprintf(url, "http://www.charlesding.net/kragl/lunchbox.php?isOpen=NO&alarmOn=OFF&authorized=YES");
+            sprintf(url, "http://www.charlesding.net/kragl/lunchbox.php?alarmOn=OFF&authorized=YES&openLock=1");
             }
-
+            
             int ret = http.get(url, str, 128);
             pc.printf("Trying to Request %s\r\n", url);
-            tries = 20;
+            tries = 50;
             while (tries > 0) {
                 ret = http.get(url, str, 128);
                 if (!ret) {