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