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

main.cpp

Committer:
wren301
Date:
2014-05-18
Revision:
1:0d467ac74808
Parent:
0:785d351db1ad
Child:
2:cf992d90396e

File content as of revision 1:0d467ac74808:

#include "mbed.h"
#include "EthernetInterface.h"
#include "NTPClient.h"
#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;
C12832_LCD lcd; //Graphics LCD
EthernetInterface eth; //Networking functions
NTPClient ntp; //NTP client
LM75B tmp(p28,p27);
float setTemp = 74;
float temp;
int heaterOn = 0;
DigitalIn leftButton(p13);
DigitalIn rightButton(p16);
#define DEBOUNCE_DELAY 3
#define LOOP_DELAY_MS 100
#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;
MMA7660 MMA(p28, p27);
int updateTimeFromServer = 1;

void debouncedButtonCheck() {
    if (leftButton || rightButton || pressed) {
        if (leftButton && !pressed) {setTemp -= 2;}
        if (rightButton && !pressed) {setTemp += 2;}
        pressed = 1;
        debounceCount++;
        if (debounceCount >= DEBOUNCE_DELAY) {
            debounceCount = 0;
            pressed = 0;
            printf("New set temp: %.0f\n\r", setTemp);
            lcd.locate(0,0);
            lcd.printf("\n\rTemp: %.0f F Set: %.0f\n\r", avgTemp, setTemp);
        }
    }
}  
  
void connectToTheInternet() {
    eth.init(); //Init and use DHCP
    wait(2);
    lcd.cls();
    lcd.printf("Getting IP Address\r\n");
    printf("Getting IP Address\r\n");
    if(eth.connect(60000)!=0) {
        lcd.printf("DHCP error - No IP");
        wait(10);
    } else {
        lcd.printf("IP is %s\n", eth.getIPAddress());
        printf("IP is %s\n", eth.getIPAddress());
        wait(2);
    }
    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();    
    //Variable to hold the current minute so we only update the display when the minute changes
    char currentMinute[2];
    currentMinute[1] = 'a';
    char minute[2];
    //float zaxis = MMA.z();
    
    float currentTemp = -200;
    lcd.printf("Updating time...\r\n");   
    printf("Updating time...\r\n");   
    if (ntp.setTime("0.pool.ntp.org") == 0) {
        printf("Set time successfully\r\n");       
        lcd.cls();
        timer.attach(&updateTime, UPDATE_TIME);
        lcd.printf("\n\r\n\rHEATER OFF");
        
        while(1) {              
            if(updateTimeFromServer) {
                updateTimeRoutine();    
            }      
            //Checks button and sets setTemp accordingly
            debouncedButtonCheck();            
            //Fetch the time 
            time_t ctTime;
            ctTime = time(NULL)- PST_OFFSET;
            char timeBuffer[32];  
                     
            //See if the minute has changed; set an update display flag if it has
            strftime(minute, 8, "%M", localtime(&ctTime));           
            if ( (minute[1] != currentMinute[1]) ) {         
                //Formats the time for display
                strftime(timeBuffer, 32, "%a %b %d %I:%M%p\n\r", localtime(&ctTime));           
                printf("%s\r", timeBuffer);
                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 
            if (floor(temp+.5) != floor(currentTemp+.5)) {
                printf("Temp: %.1f F Set: %.0f\n\r", temp, setTemp);
                currentTemp = temp;
                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)) ) {
                printf("Heater turned OFF\n\r");
                heaterOn = 0; 
                lcd.locate(0,0);
                lcd.printf("\n\r\n\rHEATER OFF");
            } else if (!heaterOn && (temp < (setTemp - TEMP_HYSTERESIS)) ) {
                printf("Heater turned ON\n\r");
                heaterOn = 1;
                lcd.locate(0,0);
                lcd.printf("\n\r\n\rHEATER ON");
            }      
            wait(LOOP_DELAY_MS*.001);                                
        }
    } else {
        lcd.printf("NTP Error\r\n");
    }
    eth.disconnect();
}