4180 Final Project

Dependencies:   4DGL-uLCD-SE mbed Servo

Revision:
27:2bd737c0a8af
Parent:
26:f08205fa22d3
--- a/main.cpp	Sun May 01 15:19:50 2016 +0000
+++ b/main.cpp	Sun May 01 19:54:20 2016 +0000
@@ -36,7 +36,7 @@
 
 // log for web page
 vector<string> accessLog;
-unsigned logLength = 10;
+unsigned logLength = 5;
 volatile bool updateAccessLog = false;
 
 // save to file flag
@@ -185,7 +185,6 @@
             }
             //if the password is long enough, check passwords, and clear vector.
             if (password.size() >= minLength) {
-                //CALL checkResult() FUNCTION, OR SET A FLAG INDICATING THAT PASSWORD IS COMPLETE AND AVAILABLE TO BE CHECKED!
                 //ledb = 1;
                 //ans = password;
                 //pc.printf("ENTER KEY PRESSED\n\r");
@@ -194,10 +193,13 @@
                 //    ans.push_back(password[k]);
                 checkKeyboard();
                 rtc = time(NULL);
+                char time_s[128];
+                struct tm * p = localtime(&rtc);
+                strftime(time_s, 128, "%T %F", p);
                 if(foundResult){
                     foundResult=false;
                     char buf[128];
-                    sprintf(buf,"%s %s SUCCESS", print_vector(name).c_str(), ctime(&rtc));
+                    sprintf(buf,"SUCCESS %s %s", print_vector(name).c_str(), time_s);
                     if(accessLog.size()<logLength){
                         accessLog.push_back(buf);
                     } else{
@@ -209,7 +211,7 @@
                 } else{
                     ledr = 1;
                     char buf[128];
-                    sprintf(buf,"%s %s FAILURE", print_vector(password).c_str(), ctime(&rtc));
+                    sprintf(buf,"FAILURE %s %s", print_vector(password).c_str(), time_s);
                     if(accessLog.size()<logLength){
                         accessLog.push_back(buf);
                     } else{
@@ -221,7 +223,7 @@
                 }
                 //update_log(accessLog);
                 updateAccessLog = true;
-                //wait(0.005);
+                wait(0.005);
                 ledg=0;
                 ledr=0;
                 vector<char> empty;
@@ -276,9 +278,6 @@
 
 }
 
-
-
-
 void save_to_file()
 {
     FILE * fp = fopen("/local/users.txt", "w");
@@ -310,17 +309,27 @@
 void Rx_interrupt()
 {
     if(!newData){
+        // at the start of getting new data we need to indicate we are receiving data
+        // the first character will clear the temporary IM and save the char
+        ledb = 1;
         tempim.clear();
-        tempim.push_back(pc.getc());
-        newData = true;
+        char c = pc.getc();
+        if(c=='!')
+            print_to_serial();
+        else{
+            tempim.push_back(c);
+            newData = true;
+        }
     }
     else{
+        // get the next character
         char c = pc.getc();
+        // unless we see a #, then we expect more characters
         if (c!='#') {
             tempim.push_back(c); //writing all the characters into vector
-            
-            //checkKeyboard(im);
+        // if we see a #, the string is complete, reset all of the flags and update im
         } else {
+            ledb=0;
             tempim.push_back(c);
             saveToFileFlag = true;
             im = tempim;
@@ -333,45 +342,67 @@
 
 int main()
 {
-    
+    // notify over PC serial that we have reset the mbed
     pc.printf("\r\n\r\nMBED RESET\r\n");
+    // ensure the lock starts in the locked position
+    // lock = 0 is unlocked
     lock = 1.0;
+    
+    // to show it's setting up, we keep the LED blue, when a function completes it flashes
+    // when the LED flashes green 3 times the setup is complete
+    ledb=1;     
+    // setup and connect to the WiFi
     pc.printf("Setting up WIFI\r\n");
-    //setup_wifi();
+    setup_wifi();
+    ledb = 0;
+    wait(0.3);
+    ledb = 1;
+    // send the HTTP socket functions to the ESP8266 module
     pc.printf("Setting Up HTTP\r\n");
-    //setup_http();
-  //  pc.printf("updating access log\r\n");
-  //  accessLog.push_back("test 1");
-  //  accessLog.push_back("Hey Edison");
-  //  accessLog.push_back("Great job!");
-    //update_log(accessLog);
+    setup_http();
+    ledb = 0;
+    wait(0.3);
+    ledb = 1;
+    // send the NTP connection functions to the WiFi module
     pc.printf("Setting up NTP\r\n");
-    //setup_ntp();
+    setup_ntp();
+    ledb = 0;
+    wait(0.3);
+    ledb = 1;
+    // use NTP and update the mbed RTC
     pc.printf("Updating NTP\r\n");
-    //update_ntp();
+    update_ntp();
+    ledb = 0;
+    wait(0.3);
+    ledb = 1;
+    // verify to the user over the PC serial port the time obtained
     rtc = time(NULL);
     pc.printf("%s\r\n", ctime(&rtc));
 
-    
+    // load the user/password information saved on the device
     load_from_file();
     print_to_serial();
- 
+    ledb = 1;
+    wait(0.3);
+    ledb = 0;
+    // register the iterrupts for handling the keypad 
     interrupt.fall(&fallInterrupt);
     interrupt.mode(PullUp);
-    //load_from_file();
-
-    //interupt driven read from serial port
-    // calls save_to_file();
+    ledb = 1;
+    wait(0.3);
+    ledb = 0;
+    //interupt driven read from serial port C# program
+    // sets saveToFileFlag;
     pc.attach(&Rx_interrupt, Serial::RxIrq);
+    ledb = 1;
+    wait(0.3);
+    ledb = 0;
+    // blink green three times to indicate full boot
+    ledg = 1; wait(0.3);ledg = 0;
+    wait(0.3); ledg = 1; wait(0.3);
+    ledg = 0; wait(0.3);ledg = 1;
+    wait(0.3); ledg = 0;
     
-    ledg = 1;
-    wait(0.3);
-    ledg = 0;
-    wait(0.3);
-    ledg = 1;
-    wait(0.3);
-    ledg = 0;
-   
     while(1){
         if(updateAccessLog){
             update_log(accessLog);
@@ -379,35 +410,12 @@
         }
         
         if(saveToFileFlag){
+            pc.printf("\r\nSAVING TO FILE\r\n");
             save_to_file();
             saveToFileFlag = false;
         }
-        /*
-        if(newData){
-            pc.printf("read in from serial\r\n");
-            
-            ledb = 1;
-            vector<char> empty;
-            swap(empty, im);
-            while (1) {
-                char c = pc.getc();
-                if (c!='#') {
-                    im.push_back(c); //writing all the characters into vector
-                    
-                    //checkKeyboard(im);
-                } else {
-                    im.push_back(c);
-                    saveToFileFlag = true;
-                    newData = false;
-                    break;
-                }
         
-            }
-            ledb = 0;
-            
-        }
-        */
-        wait(0.1);
+        wait(1);
         //sleep();
     
     }