Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: C12832_lcd EthernetInterface LM75B MMA7660 NTPClient libxively mbed-rtos mbed
main.cpp
- Committer:
- wren301
- Date:
- 2014-05-18
- Revision:
- 2:cf992d90396e
- Parent:
- 1:0d467ac74808
- Child:
- 3:3a48ed10d6fe
File content as of revision 2:cf992d90396e:
#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 lowTemp = 74;
float highTemp = 86;
float temp;
int heaterOn = 0;
AnalogIn pot1(p20);
AnalogIn pot2(p19);
#define LOOP_DELAY_MS 100
//update time every 10 minutes
#define UPDATE_TIME 60*1
MMA7660 MMA(p28, p27);
int updateTimeFromServer = 1;
void connectToTheInternet() {
eth.init(); //Init and use DHCP
wait(2);
lcd.cls();
lcd.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);
} 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;
}
//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);
}
}
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 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();
}
//Sets temp from POTs
updateTempsFromPots();
//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];
}
//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 change: %.1f F\n\r", temp);
currentTemp = temp;
lcd.locate(0,0);
lcd.printf("\n\r%.1f LOW: %.0f HIGH: %.0f", temp, lowTemp, highTemp);
}
lcd.locate(0,0);
//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 < lowTemp) ) {
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();
}