Edison Carrick / Mbed 2 deprecated Wifi_Alarm_Sunrise_Simulator

Dependencies:   mbed

Fork of huzzah_helloWorld by ECE 4180 Team Who

Revision:
3:072c63f44572
Parent:
2:f01835b2e4f7
Child:
4:80b814538fe6
--- a/main.cpp	Fri Mar 18 15:43:24 2016 +0000
+++ b/main.cpp	Fri Mar 18 15:53:44 2016 +0000
@@ -15,9 +15,9 @@
 
 Ticker alarmChecker;
 bool alarmFired = false;
-int alarmHour = 23;
-int alarmMin = 49;
-int alarmDur = 1;
+int alarmHour = 8; // hard configured start alarm time
+int alarmMin = 15;
+int alarmDur = 15; // alarm will execute at 8:00am and brighten for 15 min
 string data;
 Timer t;
 time_t rtc;
@@ -34,6 +34,9 @@
  
 void SendCMD(),getreply(),ESPhttp(),ESPsetbaudrate(), sendConfig(), ESPreset(), sendNTPFunction(), updateNTP(), getUtime(), startSunrise(float d);
 
+// check the time in seconds, compare to the alarm time
+// if the alarm has fired, then don't re-fire once done
+// wait until the next day (midnight) to reset the fire status
 void alarmCheck(){
         rtc = time(NULL);
         tp = localtime(&rtc);
@@ -42,14 +45,14 @@
             printf("hr: %d  min:%d\r\n", tp->tm_hour, tp->tm_min);
             int daystamp = tp->tm_hour * 3600 + tp->tm_min * 60;
             int alarmstamp = alarmHour * 3600 + alarmMin * 60 - (alarmDur * 60);
-            pc.printf("%d - %d = %d", alarmstamp, daystamp, alarmstamp - daystamp);
+            pc.printf("%d - %d = %d\r\n", alarmstamp, daystamp, alarmstamp - daystamp);
             if( (alarmstamp - daystamp) <= 0){
                 // time to start the wake up sequence
                 startSunrise((float)(alarmDur*60));
                 alarmFired = true;
             }
         } else{
-            if(tp->tm_hour == 0)
+            if(tp->tm_hour == 0) // wait until midnight
                 alarmFired = false;
         }
         wait(1);
@@ -132,31 +135,39 @@
     
     led3=1;
     
+    // test the light to make sure it works
     for(float i=0.0;i<1.0;i+=0.1){
         light=i;
         wait(0.5);
     }
     light = 0;
  
-    //ESPreset();
- 
+    // set up the HTTP server 
     ESPhttp();
         
+        // uncomment to be able to send commands to the wifi module directly through the PC COM port
     //pc.attach(&pc_recv, Serial::RxIrq);
+    // set up an interupt to listen for data sent by the wifi module
+    // allows the configuration to be updated.
     esp.attach(&dev_recv, Serial::RxIrq);
     
+    // print the time to console to verify updated
     rtc = time(NULL);
     pc.printf("%s\r\n", ctime(&rtc));
     led1=0;led2=0;led3=0;led4=0;
     tp = localtime(&rtc);
     printf("hr: %d  min:%d\r\n", tp->tm_hour, tp->tm_min);
+    
+    // set up an interupt checker to check the alarm status every minute.
     alarmChecker.attach(&alarmCheck, 60);
     while(1) {
-        sleep();
+        sleep(); // low power sleep to conserve power inbetween checks
     }
  
 }
 
+// brighten the light over the duration period length
+// keep the light on for 2 minutes, then shut it off
 void startSunrise(float d){
     for(float i=0.0; i < 1.0; i += (1.0/d)){
         light = i;   
@@ -171,6 +182,8 @@
 }
  
 // Sets new ESP8266 baurate, change the esp.baud(xxxxx) to match your new setting once this has been executed
+// This is an important configuration change. By setting the last argument to zero, it stops the module from echoing 
+// commands back, allowing us to send commands and read only the value
 void ESPsetbaudrate()
 {
     strcpy(snd, "uart.setup(0, 9600, 8, 0, 1, 0)\r\n");   // change the numeric value to the required baudrate
@@ -225,7 +238,6 @@
     
 }
  
-//  +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++
 void ESPhttp()
 {
      
@@ -428,6 +440,8 @@
     wait(15);
     //updateNTP();  
 }
+// this function executes the connection to the NTP server
+// saving the time stamp into a local variable on the wifi module to be read
 void updateNTP(){
     flush();
     strcpy(snd, "sk:connect(123,\"104.232.3.3\")\r\n");
@@ -441,7 +455,9 @@
     pc.printf(buf);
     //getUtime();
 }
-
+// This function calls the wifi module to output the time stamp and save it to the local clock
+// This should be executed right after updateNTP to reduce the delay in getting the clock value 
+// and setting the clock
 void getUtime(){
     flush();
     strcpy(snd, "print(time)\r\n");