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@2:cf992d90396e, 2014-05-18 (annotated)
- Committer:
- wren301
- Date:
- Sun May 18 03:23:37 2014 +0000
- Revision:
- 2:cf992d90396e
- Parent:
- 1:0d467ac74808
- Child:
- 3:3a48ed10d6fe
HW5 everything tested and working except JSON;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wren301 | 0:785d351db1ad | 1 | #include "mbed.h" |
wren301 | 0:785d351db1ad | 2 | #include "EthernetInterface.h" |
wren301 | 0:785d351db1ad | 3 | #include "NTPClient.h" |
wren301 | 0:785d351db1ad | 4 | #include "C12832_lcd.h" |
wren301 | 0:785d351db1ad | 5 | #include "LM75B.h" |
wren301 | 0:785d351db1ad | 6 | #include "MMA7660.h" |
wren301 | 0:785d351db1ad | 7 | |
wren301 | 0:785d351db1ad | 8 | #define PST_OFFSET 7*60*60 |
wren301 | 0:785d351db1ad | 9 | |
wren301 | 0:785d351db1ad | 10 | Ticker timer; |
wren301 | 0:785d351db1ad | 11 | C12832_LCD lcd; //Graphics LCD |
wren301 | 0:785d351db1ad | 12 | EthernetInterface eth; //Networking functions |
wren301 | 0:785d351db1ad | 13 | NTPClient ntp; //NTP client |
wren301 | 0:785d351db1ad | 14 | LM75B tmp(p28,p27); |
wren301 | 2:cf992d90396e | 15 | float lowTemp = 74; |
wren301 | 2:cf992d90396e | 16 | float highTemp = 86; |
wren301 | 0:785d351db1ad | 17 | float temp; |
wren301 | 0:785d351db1ad | 18 | int heaterOn = 0; |
wren301 | 2:cf992d90396e | 19 | AnalogIn pot1(p20); |
wren301 | 2:cf992d90396e | 20 | AnalogIn pot2(p19); |
wren301 | 0:785d351db1ad | 21 | #define LOOP_DELAY_MS 100 |
wren301 | 0:785d351db1ad | 22 | //update time every 10 minutes |
wren301 | 0:785d351db1ad | 23 | #define UPDATE_TIME 60*1 |
wren301 | 1:0d467ac74808 | 24 | |
wren301 | 0:785d351db1ad | 25 | MMA7660 MMA(p28, p27); |
wren301 | 0:785d351db1ad | 26 | int updateTimeFromServer = 1; |
wren301 | 2:cf992d90396e | 27 | |
wren301 | 0:785d351db1ad | 28 | void connectToTheInternet() { |
wren301 | 0:785d351db1ad | 29 | eth.init(); //Init and use DHCP |
wren301 | 0:785d351db1ad | 30 | wait(2); |
wren301 | 0:785d351db1ad | 31 | lcd.cls(); |
wren301 | 0:785d351db1ad | 32 | lcd.printf("Getting IP Address\r\n"); |
wren301 | 2:cf992d90396e | 33 | printf("\n\rGetting IP Address\r\n"); |
wren301 | 0:785d351db1ad | 34 | if(eth.connect(60000)!=0) { |
wren301 | 0:785d351db1ad | 35 | lcd.printf("DHCP error - No IP"); |
wren301 | 0:785d351db1ad | 36 | wait(10); |
wren301 | 0:785d351db1ad | 37 | } else { |
wren301 | 0:785d351db1ad | 38 | lcd.printf("IP is %s\n", eth.getIPAddress()); |
wren301 | 0:785d351db1ad | 39 | printf("IP is %s\n", eth.getIPAddress()); |
wren301 | 0:785d351db1ad | 40 | wait(2); |
wren301 | 0:785d351db1ad | 41 | } |
wren301 | 0:785d351db1ad | 42 | lcd.cls(); |
wren301 | 0:785d351db1ad | 43 | } |
wren301 | 0:785d351db1ad | 44 | |
wren301 | 1:0d467ac74808 | 45 | void updateTimeRoutine() { |
wren301 | 1:0d467ac74808 | 46 | if (ntp.setTime("0.pool.ntp.org") == 0) { |
wren301 | 1:0d467ac74808 | 47 | printf("Time updated!"); |
wren301 | 1:0d467ac74808 | 48 | } else { |
wren301 | 1:0d467ac74808 | 49 | lcd.locate(0,0); |
wren301 | 1:0d467ac74808 | 50 | printf("Time update failed \n\r"); |
wren301 | 1:0d467ac74808 | 51 | lcd.printf("Time update failed"); |
wren301 | 1:0d467ac74808 | 52 | } |
wren301 | 1:0d467ac74808 | 53 | updateTimeFromServer = 0; |
wren301 | 1:0d467ac74808 | 54 | } |
wren301 | 1:0d467ac74808 | 55 | |
wren301 | 0:785d351db1ad | 56 | void updateTime() { |
wren301 | 0:785d351db1ad | 57 | updateTimeFromServer = 1; |
wren301 | 0:785d351db1ad | 58 | } |
wren301 | 0:785d351db1ad | 59 | |
wren301 | 2:cf992d90396e | 60 | //POT values are 0:1, will allow high and low temperatures |
wren301 | 2:cf992d90396e | 61 | //lets you set temp between 50 and 100 degrees |
wren301 | 2:cf992d90396e | 62 | void updateTempsFromPots() { |
wren301 | 2:cf992d90396e | 63 | float lowTempPot = pot2*50.0+50; |
wren301 | 2:cf992d90396e | 64 | float highTempPot = pot1*50.0+50; |
wren301 | 2:cf992d90396e | 65 | //round to the nearest whole number |
wren301 | 2:cf992d90396e | 66 | lowTempPot = floor(lowTempPot+.5); |
wren301 | 2:cf992d90396e | 67 | highTempPot = floor(highTempPot+.5); |
wren301 | 2:cf992d90396e | 68 | //high temp must be at least 1 degree above low temp |
wren301 | 2:cf992d90396e | 69 | if (highTempPot <= lowTempPot) {highTempPot = lowTempPot +1;} |
wren301 | 2:cf992d90396e | 70 | //Refresh the display if the temps have changed by more than a degree |
wren301 | 2:cf992d90396e | 71 | if ((lowTemp != lowTempPot) || (highTemp != highTempPot)) { |
wren301 | 2:cf992d90396e | 72 | lowTemp = lowTempPot; |
wren301 | 2:cf992d90396e | 73 | highTemp = highTempPot; |
wren301 | 2:cf992d90396e | 74 | lcd.locate(0,0); |
wren301 | 2:cf992d90396e | 75 | lcd.printf("\n\r%.1f LOW: %.0f HIGH: %.0f", temp, lowTemp, highTemp); |
wren301 | 2:cf992d90396e | 76 | printf("LOW: %.2f HIGH: %.2f\n\r", lowTemp, highTemp); |
wren301 | 1:0d467ac74808 | 77 | } |
wren301 | 1:0d467ac74808 | 78 | } |
wren301 | 1:0d467ac74808 | 79 | |
wren301 | 0:785d351db1ad | 80 | int main() |
wren301 | 0:785d351db1ad | 81 | { |
wren301 | 0:785d351db1ad | 82 | connectToTheInternet(); |
wren301 | 0:785d351db1ad | 83 | //Variable to hold the current minute so we only update the display when the minute changes |
wren301 | 0:785d351db1ad | 84 | char currentMinute[2]; |
wren301 | 0:785d351db1ad | 85 | currentMinute[1] = 'a'; |
wren301 | 0:785d351db1ad | 86 | char minute[2]; |
wren301 | 2:cf992d90396e | 87 | |
wren301 | 0:785d351db1ad | 88 | float currentTemp = -200; |
wren301 | 0:785d351db1ad | 89 | lcd.printf("Updating time...\r\n"); |
wren301 | 0:785d351db1ad | 90 | printf("Updating time...\r\n"); |
wren301 | 0:785d351db1ad | 91 | if (ntp.setTime("0.pool.ntp.org") == 0) { |
wren301 | 0:785d351db1ad | 92 | printf("Set time successfully\r\n"); |
wren301 | 0:785d351db1ad | 93 | lcd.cls(); |
wren301 | 0:785d351db1ad | 94 | timer.attach(&updateTime, UPDATE_TIME); |
wren301 | 0:785d351db1ad | 95 | lcd.printf("\n\r\n\rHEATER OFF"); |
wren301 | 0:785d351db1ad | 96 | |
wren301 | 1:0d467ac74808 | 97 | while(1) { |
wren301 | 0:785d351db1ad | 98 | if(updateTimeFromServer) { |
wren301 | 1:0d467ac74808 | 99 | updateTimeRoutine(); |
wren301 | 0:785d351db1ad | 100 | } |
wren301 | 2:cf992d90396e | 101 | //Sets temp from POTs |
wren301 | 2:cf992d90396e | 102 | updateTempsFromPots(); |
wren301 | 0:785d351db1ad | 103 | //Fetch the time |
wren301 | 1:0d467ac74808 | 104 | time_t ctTime; |
wren301 | 0:785d351db1ad | 105 | ctTime = time(NULL)- PST_OFFSET; |
wren301 | 0:785d351db1ad | 106 | char timeBuffer[32]; |
wren301 | 0:785d351db1ad | 107 | |
wren301 | 0:785d351db1ad | 108 | //See if the minute has changed; set an update display flag if it has |
wren301 | 0:785d351db1ad | 109 | strftime(minute, 8, "%M", localtime(&ctTime)); |
wren301 | 0:785d351db1ad | 110 | if ( (minute[1] != currentMinute[1]) ) { |
wren301 | 0:785d351db1ad | 111 | //Formats the time for display |
wren301 | 0:785d351db1ad | 112 | strftime(timeBuffer, 32, "%a %b %d %I:%M%p\n\r", localtime(&ctTime)); |
wren301 | 0:785d351db1ad | 113 | printf("%s\r", timeBuffer); |
wren301 | 0:785d351db1ad | 114 | lcd.locate(0,0); |
wren301 | 0:785d351db1ad | 115 | lcd.printf("%s\r", timeBuffer); |
wren301 | 0:785d351db1ad | 116 | currentMinute[1] = minute[1]; |
wren301 | 0:785d351db1ad | 117 | } |
wren301 | 1:0d467ac74808 | 118 | |
wren301 | 0:785d351db1ad | 119 | //Update the temperature display if the temperature, set temp has changed |
wren301 | 0:785d351db1ad | 120 | temp = tmp.read()*9.0/5.0 + 32.0; |
wren301 | 0:785d351db1ad | 121 | //checks if the temperature (rounded to the nearest whole number) has changed |
wren301 | 0:785d351db1ad | 122 | if (floor(temp+.5) != floor(currentTemp+.5)) { |
wren301 | 2:cf992d90396e | 123 | printf("Temp change: %.1f F\n\r", temp); |
wren301 | 0:785d351db1ad | 124 | currentTemp = temp; |
wren301 | 0:785d351db1ad | 125 | lcd.locate(0,0); |
wren301 | 2:cf992d90396e | 126 | lcd.printf("\n\r%.1f LOW: %.0f HIGH: %.0f", temp, lowTemp, highTemp); |
wren301 | 1:0d467ac74808 | 127 | } |
wren301 | 2:cf992d90396e | 128 | |
wren301 | 0:785d351db1ad | 129 | lcd.locate(0,0); |
wren301 | 2:cf992d90396e | 130 | //Heater logic: turns off if it has gone over the high temp, on if under the low temp |
wren301 | 2:cf992d90396e | 131 | if (heaterOn && (temp > highTemp) ) { |
wren301 | 0:785d351db1ad | 132 | printf("Heater turned OFF\n\r"); |
wren301 | 0:785d351db1ad | 133 | heaterOn = 0; |
wren301 | 0:785d351db1ad | 134 | lcd.locate(0,0); |
wren301 | 0:785d351db1ad | 135 | lcd.printf("\n\r\n\rHEATER OFF"); |
wren301 | 2:cf992d90396e | 136 | } else if (!heaterOn && (temp < lowTemp) ) { |
wren301 | 0:785d351db1ad | 137 | printf("Heater turned ON\n\r"); |
wren301 | 0:785d351db1ad | 138 | heaterOn = 1; |
wren301 | 0:785d351db1ad | 139 | lcd.locate(0,0); |
wren301 | 0:785d351db1ad | 140 | lcd.printf("\n\r\n\rHEATER ON"); |
wren301 | 0:785d351db1ad | 141 | } |
wren301 | 0:785d351db1ad | 142 | wait(LOOP_DELAY_MS*.001); |
wren301 | 0:785d351db1ad | 143 | } |
wren301 | 0:785d351db1ad | 144 | } else { |
wren301 | 0:785d351db1ad | 145 | lcd.printf("NTP Error\r\n"); |
wren301 | 0:785d351db1ad | 146 | } |
wren301 | 0:785d351db1ad | 147 | eth.disconnect(); |
wren301 | 0:785d351db1ad | 148 | } |