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
Diff: main.cpp
- Revision:
- 2:cf992d90396e
- Parent:
- 1:0d467ac74808
- Child:
- 3:3a48ed10d6fe
--- a/main.cpp Sun May 18 02:24:22 2014 +0000 +++ b/main.cpp Sun May 18 03:23:37 2014 +0000 @@ -4,19 +4,7 @@ #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; @@ -24,56 +12,25 @@ EthernetInterface eth; //Networking functions NTPClient ntp; //NTP client LM75B tmp(p28,p27); -float setTemp = 74; +float lowTemp = 74; +float highTemp = 86; float temp; int heaterOn = 0; -DigitalIn leftButton(p13); -DigitalIn rightButton(p16); -#define DEBOUNCE_DELAY 3 +AnalogIn pot1(p20); +AnalogIn pot2(p19); #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"); + printf("\n\rGetting IP Address\r\n"); if(eth.connect(60000)!=0) { lcd.printf("DHCP error - No IP"); wait(10); @@ -100,23 +57,24 @@ 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"); +//POT values are 0:1, will allow high and low temperatures +//lets you set temp between 50 and 100 degrees +void updateTempsFromPots() { + float lowTempPot = pot2*50.0+50; + float highTempPot = pot1*50.0+50; + //round to the nearest whole number + lowTempPot = floor(lowTempPot+.5); + highTempPot = floor(highTempPot+.5); + //high temp must be at least 1 degree above low temp + if (highTempPot <= lowTempPot) {highTempPot = lowTempPot +1;} + //Refresh the display if the temps have changed by more than a degree + if ((lowTemp != lowTempPot) || (highTemp != highTempPot)) { + lowTemp = lowTempPot; + highTemp = highTempPot; + lcd.locate(0,0); + lcd.printf("\n\r%.1f LOW: %.0f HIGH: %.0f", temp, lowTemp, highTemp); + printf("LOW: %.2f HIGH: %.2f\n\r", lowTemp, highTemp); } - timeSinceLastSMS = 0; - alertActive = 0; } int main() @@ -126,8 +84,7 @@ 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"); @@ -141,8 +98,8 @@ if(updateTimeFromServer) { updateTimeRoutine(); } - //Checks button and sets setTemp accordingly - debouncedButtonCheck(); + //Sets temp from POTs + updateTempsFromPots(); //Fetch the time time_t ctTime; ctTime = time(NULL)- PST_OFFSET; @@ -157,41 +114,26 @@ 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); + printf("Temp change: %.1f F\n\r", temp); currentTemp = temp; - avgTemp = (temp+currentTemp)/2.0; lcd.locate(0,0); - lcd.printf("\n\rTemp: %.0f F Set: %.0f\n\r", avgTemp, setTemp); + lcd.printf("\n\r%.1f LOW: %.0f HIGH: %.0f", temp, lowTemp, highTemp); } - - //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)) ) { + //Heater logic: turns off if it has gone over the high temp, on if under the low temp + if (heaterOn && (temp > highTemp) ) { 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)) ) { + } else if (!heaterOn && (temp < lowTemp) ) { printf("Heater turned ON\n\r"); heaterOn = 1; lcd.locate(0,0);