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:
- 0:785d351db1ad
- Child:
- 1:0d467ac74808
diff -r 000000000000 -r 785d351db1ad main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Apr 29 06:50:11 2014 +0000 @@ -0,0 +1,143 @@ +#include "mbed.h" +#include "EthernetInterface.h" +#include "NTPClient.h" +#include "C12832_lcd.h" +#include "LM75B.h" +#include "MMA7660.h" + +#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 +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 updateTime() { + updateTimeFromServer = 1; +} + +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) { + 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) { + + 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; + } + //Checks button and sets setTemp accordingly + debouncedButtonCheck(); + //Fetch the time + 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]; + } + //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); + } + 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(); +} \ No newline at end of file