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