Connections to Xively working; has 5 channels on Xively (axl_x, axl_y, axl_z, heater_status, temperature)

Dependencies:   C12832_lcd EthernetInterface LM75B MMA7660 NTPClient libxively mbed-rtos mbed

Revision:
1:0d467ac74808
Parent:
0:785d351db1ad
Child:
2:cf992d90396e
--- a/main.cpp	Tue Apr 29 06:50:11 2014 +0000
+++ b/main.cpp	Sun May 18 02:24:22 2014 +0000
@@ -4,7 +4,19 @@
 #include "C12832_lcd.h"
 #include "LM75B.h"
 #include "MMA7660.h"
+#include "SimpleSMTPClient.h"
 
+#define DOMAIN "comcast.net" //gmail.com
+#define SERVER "smtp.comcast.net" //smtp.gmail.com
+#define PORT "587" //25 or 587,465(OutBound Port25 Blocking ) 465
+#define USER "patkionkar" //bhakti.kulkarni08
+#define PWD "book#jungle" //bhakti0887
+#define FROM_ADDRESS "patkionkar@comcast.net" //bhakti.kulkarni08
+// TO_ADDRESS (Of some address is possible.)
+// to-user1@domain, to-user2@domain, to-user3@domain ....
+// The TO_ADDRESS are less than 128 characters.
+#define TO_ADDRESS "wren301@gmail.com" 
+#define SUBJECT "Test Mail"
 #define PST_OFFSET  7*60*60
 
 Ticker timer;
@@ -22,6 +34,18 @@
 #define TEMP_HYSTERESIS 2
 //update time every 10 minutes
 #define UPDATE_TIME 60*1 
+//temperature to send an sms to users at
+#define ALERT_TEMP 95
+//The alert will go off when it first crosses 90, and only reset in two
+//cases:
+//1. The temperature drops again to below ALERT_TEMP - ALERT_HYSTERESIS_TEMP
+//2. ALERT_HYSTERESIS_TIME minutes pass after the first alert and the
+//temperature is still over ALERT_TEMP
+#define ALERT_HYSTERESIS_TEMP 2
+#define ALERT_HYSTERESIS_TIME 2
+int timeSinceLastSMS = ALERT_HYSTERESIS_TIME;
+bool alertActive = true;
+
 int pressed = 0;
 int debounceCount = 0;
 float avgTemp = 0;
@@ -42,7 +66,8 @@
             lcd.printf("\n\rTemp: %.0f F Set: %.0f\n\r", avgTemp, setTemp);
         }
     }
-}    
+}  
+  
 void connectToTheInternet() {
     eth.init(); //Init and use DHCP
     wait(2);
@@ -60,10 +85,40 @@
     lcd.cls();
 }
 
+void updateTimeRoutine() {
+    if (ntp.setTime("0.pool.ntp.org") == 0) {
+        printf("Time updated!");
+    } else {
+        lcd.locate(0,0);
+        printf("Time update failed \n\r");
+        lcd.printf("Time update failed");
+    }
+    updateTimeFromServer = 0;
+}
+
 void updateTime() {
     updateTimeFromServer = 1;
 }
 
+void sendSMS() {
+    printf("SENDING SMS\n\r");
+    SimpleSMTPClient smtp;
+    int ret;
+    char msg[]="Hello SimpleSMTPClient ";
+    smtp.setFromAddress(FROM_ADDRESS);
+    smtp.setToAddress("6502700054@txt.att.net");     
+    smtp.setMessage(SUBJECT,msg);
+    smtp.addMessage("TEST TEST TEST\r\n");
+    ret = smtp.sendmail(SERVER, USER, PWD, DOMAIN,PORT,SMTP_AUTH_PLAIN); 
+    if (ret) {
+        printf("SMS Transmission Error\r\n");
+    } else {
+        printf("SMS Transmission OK\r\n");
+    }
+    timeSinceLastSMS = 0;
+    alertActive = 0;
+}
+
 int main()
 {   
     connectToTheInternet();    
@@ -77,27 +132,19 @@
     lcd.printf("Updating time...\r\n");   
     printf("Updating time...\r\n");   
     if (ntp.setTime("0.pool.ntp.org") == 0) {
-        time_t ctTime;
-        lcd.printf("Set time successfully\r\n");
         printf("Set time successfully\r\n");       
         lcd.cls();
         timer.attach(&updateTime, UPDATE_TIME);
         lcd.printf("\n\r\n\rHEATER OFF");
-        while(1) {      
         
+        while(1) {              
             if(updateTimeFromServer) {
-                if (ntp.setTime("0.pool.ntp.org") == 0) {
-                    printf("Time updated!");
-                } else {
-                    lcd.locate(0,0);
-                    printf("Time update failed \n\r");
-                    lcd.printf("Time update failed");
-                }
-                updateTimeFromServer = 0;    
+                updateTimeRoutine();    
             }      
             //Checks button and sets setTemp accordingly
             debouncedButtonCheck();            
             //Fetch the time 
+            time_t ctTime;
             ctTime = time(NULL)- PST_OFFSET;
             char timeBuffer[32];  
                      
@@ -110,7 +157,9 @@
                 lcd.locate(0,0);
                 lcd.printf("%s\r", timeBuffer);
                 currentMinute[1] = minute[1];
+                timeSinceLastSMS++;
             }          
+            
             //Update the temperature display if the temperature, set temp has changed
             temp = tmp.read()*9.0/5.0 + 32.0;  
             //checks if the temperature (rounded to the nearest whole number) has changed 
@@ -120,7 +169,21 @@
                 avgTemp = (temp+currentTemp)/2.0;
                 lcd.locate(0,0);
                 lcd.printf("\n\rTemp: %.0f F Set: %.0f\n\r", avgTemp, setTemp);
-            }    
+            }   
+            
+            //If the temp has gone over a threshold, sends an SMS to the user
+            if ( temp > ALERT_TEMP ) {
+                if ((timeSinceLastSMS >= ALERT_HYSTERESIS_TIME) || alertActive ) {
+                    sendSMS();
+                }     
+            }
+            //reactivates SMS alert if temp goes a certain amount
+            //below the alert temperature
+            if ( !alertActive && (temp < (ALERT_TEMP - ALERT_HYSTERESIS_TEMP) ) ) {
+                alertActive = 1;
+                printf("Alert reactivated from temp drop");
+            }
+             
             lcd.locate(0,0);             
             //Heater logic: turns off if it has gone 2 degrees over the set temp, or on if it's 2 degrees under
             if (heaterOn && (temp > (setTemp + TEMP_HYSTERESIS)) ) {