Dreamforce 2013 MiniHack Thermostat Challenge - remotes
Dependencies: C12832_lcd EthernetInterface-ansond-patched HTTPClient-thermostat-remotes LM75B MMA7660 SocketIO WebSocketClient-ThermostatDemo mbed-rtos mbed picojson
Fork of df-2013-minihack-thermostat-complete by
Thermostat.cpp@6:33f71cf9a04f, 2013-11-11 (annotated)
- Committer:
- ansond
- Date:
- Mon Nov 11 22:46:25 2013 +0000
- Revision:
- 6:33f71cf9a04f
- Parent:
- 5:9866322ed225
updates
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:26c48388f725 | 1 | /* Thermostat.cpp */ |
ansond | 0:26c48388f725 | 2 | /* Copyright (C) 2013 mbed.org, MIT License |
ansond | 0:26c48388f725 | 3 | * |
ansond | 0:26c48388f725 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
ansond | 0:26c48388f725 | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
ansond | 0:26c48388f725 | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
ansond | 0:26c48388f725 | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
ansond | 0:26c48388f725 | 8 | * furnished to do so, subject to the following conditions: |
ansond | 0:26c48388f725 | 9 | * |
ansond | 0:26c48388f725 | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
ansond | 0:26c48388f725 | 11 | * substantial portions of the Software. |
ansond | 0:26c48388f725 | 12 | * |
ansond | 0:26c48388f725 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
ansond | 0:26c48388f725 | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
ansond | 0:26c48388f725 | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
ansond | 0:26c48388f725 | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ansond | 0:26c48388f725 | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
ansond | 0:26c48388f725 | 18 | */ |
ansond | 0:26c48388f725 | 19 | |
ansond | 0:26c48388f725 | 20 | // |
ansond | 0:26c48388f725 | 21 | // 2013 DreamForce MiniHack - |
ansond | 0:26c48388f725 | 22 | // Look for the method: Thermostat::parseAndActOnControlMessage(char *json) |
ansond | 0:26c48388f725 | 23 | // From there, add code to look for a "text" message - send its contents to the LCD panel |
ansond | 0:26c48388f725 | 24 | // using the this->display(char *) method. You can use the other messages as a reference. |
ansond | 0:26c48388f725 | 25 | // |
ansond | 0:26c48388f725 | 26 | |
ansond | 0:26c48388f725 | 27 | /* |
ansond | 0:26c48388f725 | 28 | * Operation: with the MBED appboard - the following is available: |
ansond | 0:26c48388f725 | 29 | * 1. connect/disconnect: push and hold the joystick forward (towards potentiometer) until a connect/disconnect message is seen |
ansond | 0:26c48388f725 | 30 | * 2. control messages |
ansond | 0:26c48388f725 | 31 | * "led1" --> "on" or "off" (led1 is also used as the TX status...) |
ansond | 0:26c48388f725 | 32 | * "led2" --> "on" or "off" (led2 is also used as the RX status...) |
ansond | 0:26c48388f725 | 33 | * "led3" --> "on" or "off" |
ansond | 0:26c48388f725 | 34 | * "led4" --> "on" or "off" |
ansond | 0:26c48388f725 | 35 | * "blink" --> <dont care> should blink all 4 LEDs a few times |
ansond | 0:26c48388f725 | 36 | * "reset" --> <dont care> will reset the simulated error state for the device |
ansond | 0:26c48388f725 | 37 | * 3. simulated error state: depress and hold the joystick until the RGB LED turns red |
ansond | 0:26c48388f725 | 38 | * 4. rotate the potentiometer closest to the LCD panel to manipulate the battery level (0 - 100%) |
ansond | 0:26c48388f725 | 39 | * 5. rotate the potentiometer nearest the RGB LED to add/subtract 10 degrees from the current ambient temperature |
ansond | 0:26c48388f725 | 40 | */ |
ansond | 0:26c48388f725 | 41 | |
ansond | 0:26c48388f725 | 42 | // Primary include |
ansond | 0:26c48388f725 | 43 | #include "Thermostat.h" |
ansond | 0:26c48388f725 | 44 | |
ansond | 0:26c48388f725 | 45 | // DreamForce 2013 Tunables START |
ansond | 0:26c48388f725 | 46 | |
ansond | 4:379e9ad21486 | 47 | // set our location |
ansond | 6:33f71cf9a04f | 48 | int location_index = 0; |
ansond | 0:26c48388f725 | 49 | |
ansond | 4:379e9ad21486 | 50 | // Remoted Thermostats |
ansond | 6:33f71cf9a04f | 51 | // Thermostat 0: Austin TX 30.2500 –97.7500 |
ansond | 6:33f71cf9a04f | 52 | // Thermostat 1: Phoenix AZ 33.4500 -112.0667 |
ansond | 6:33f71cf9a04f | 53 | // Thermostat 2: Dallas TX 32.7758 -96.7967 |
ansond | 6:33f71cf9a04f | 54 | // Thermostat 3: Miami FL 25.7877 -80.2241 |
ansond | 6:33f71cf9a04f | 55 | // Thermostat 4: Chicago IL 41.8819 –87.6278 |
ansond | 6:33f71cf9a04f | 56 | // Thermostat 5: Memphis TN 35.1174 -89.9711 |
ansond | 6:33f71cf9a04f | 57 | // Thermostat 6: Seattle WA 47.6097 –122.3331 |
ansond | 6:33f71cf9a04f | 58 | // Thermostat 7: New York NY 40.6700 –73.9400 |
ansond | 6:33f71cf9a04f | 59 | // Thermostat 8: Raleigh NC 35.8189 –78.6447 |
ansond | 6:33f71cf9a04f | 60 | // Thermostat 9: Moscone 37.7842 –122.4016 |
ansond | 4:379e9ad21486 | 61 | float latitudes[10] = { 30.2500, 33.4500, 32.7758, 25.7877, 41.8819, 35.1174, 47.6097, 40.6700, 35.8189, 37.7842}; |
ansond | 4:379e9ad21486 | 62 | float longitudes[10] = { -97.7500, -112.0667, -96.7967, -80.2241, -87.6278, -89.9711, -122.3331, -73.9400, -78.6447, -122.4016}; |
ansond | 4:379e9ad21486 | 63 | char *locations[10] = { "DF Thermostat-Austin TX", "DF Thermostat-Phoenix AZ", "DF Thermostat-Dallas TX", "DF Thermostat-Miami FL", |
ansond | 4:379e9ad21486 | 64 | "DF Thermostat-Chicago IL", "DF Thermostat-Memphis TN", "DF Thermostat-Seattle WA", |
ansond | 4:379e9ad21486 | 65 | "DF Thermostat-New York NY", "DF Thermostat-Raleigh NC", "DF Thermostat-Moscone Center"}; |
ansond | 6:33f71cf9a04f | 66 | char *lcd_locations[10]= { "Austin TX", "Phoenix AZ", "Dallas TX", "Miami FL", |
ansond | 6:33f71cf9a04f | 67 | "Chicago IL", "Memphis TN", "Seattle WA", |
ansond | 6:33f71cf9a04f | 68 | "New York NY", "Raleigh NC", "Moscone Center"}; |
ansond | 4:379e9ad21486 | 69 | |
ansond | 0:26c48388f725 | 70 | // DreamForce 2013 Tunables END |
ansond | 0:26c48388f725 | 71 | |
ansond | 0:26c48388f725 | 72 | // Wait Loop default sleep per iteration |
ansond | 0:26c48388f725 | 73 | #define DEFAULT_MAIN_LOOP_WAIT 2.0 // 2 seconds |
ansond | 0:26c48388f725 | 74 | |
ansond | 0:26c48388f725 | 75 | // JSON parsing support |
ansond | 0:26c48388f725 | 76 | #include "picojson.h" |
ansond | 0:26c48388f725 | 77 | |
ansond | 0:26c48388f725 | 78 | // |
ansond | 0:26c48388f725 | 79 | // Accelerometer Support |
ansond | 0:26c48388f725 | 80 | // |
ansond | 0:26c48388f725 | 81 | #include "MMA7660.h" |
ansond | 0:26c48388f725 | 82 | MMA7660 acc(p28, p27); |
ansond | 0:26c48388f725 | 83 | |
ansond | 0:26c48388f725 | 84 | // |
ansond | 0:26c48388f725 | 85 | // Temperature Sensor Support |
ansond | 0:26c48388f725 | 86 | // |
ansond | 0:26c48388f725 | 87 | #include "LM75B.h" |
ansond | 0:26c48388f725 | 88 | LM75B temp_sensor(p28,p27); |
ansond | 0:26c48388f725 | 89 | |
ansond | 0:26c48388f725 | 90 | // |
ansond | 0:26c48388f725 | 91 | // Ethernet support |
ansond | 0:26c48388f725 | 92 | // |
ansond | 0:26c48388f725 | 93 | #include "EthernetInterface.h" |
ansond | 0:26c48388f725 | 94 | EthernetInterface ethernet; |
ansond | 0:26c48388f725 | 95 | |
ansond | 0:26c48388f725 | 96 | // |
ansond | 0:26c48388f725 | 97 | // Thermostat SocketIO Support |
ansond | 0:26c48388f725 | 98 | // |
ansond | 0:26c48388f725 | 99 | #include "ThermostatSocketIO.h" |
ansond | 4:379e9ad21486 | 100 | ThermostatSocketIO socketio(locations[location_index]); |
ansond | 0:26c48388f725 | 101 | |
ansond | 0:26c48388f725 | 102 | // Include LED Utils |
ansond | 0:26c48388f725 | 103 | #include "Thermostat-LEDUtils.h" |
ansond | 0:26c48388f725 | 104 | |
ansond | 0:26c48388f725 | 105 | // Include Base Utils |
ansond | 0:26c48388f725 | 106 | #include "Thermostat-BaseUtils.h" |
ansond | 0:26c48388f725 | 107 | |
ansond | 0:26c48388f725 | 108 | // Include Location Stubs |
ansond | 0:26c48388f725 | 109 | #include "Thermostat-Location.h" |
ansond | 0:26c48388f725 | 110 | |
ansond | 0:26c48388f725 | 111 | // Default constructor |
ansond | 0:26c48388f725 | 112 | Thermostat::Thermostat() { |
ansond | 0:26c48388f725 | 113 | this->m_temperature = 0.0; |
ansond | 0:26c48388f725 | 114 | this->m_battery = 50.0; |
ansond | 0:26c48388f725 | 115 | this->m_status = "OK"; |
ansond | 0:26c48388f725 | 116 | } |
ansond | 0:26c48388f725 | 117 | |
ansond | 0:26c48388f725 | 118 | // Destructor |
ansond | 0:26c48388f725 | 119 | Thermostat::~Thermostat() { |
ansond | 0:26c48388f725 | 120 | // close down connections |
ansond | 0:26c48388f725 | 121 | socketio.close(); |
ansond | 0:26c48388f725 | 122 | ethernet.disconnect(); |
ansond | 0:26c48388f725 | 123 | this->turnRGBLEDBlue(); |
ansond | 0:26c48388f725 | 124 | } |
ansond | 0:26c48388f725 | 125 | |
ansond | 0:26c48388f725 | 126 | // get the temp |
ansond | 0:26c48388f725 | 127 | float Thermostat::getTemperature() { |
ansond | 0:26c48388f725 | 128 | // get Pot 2 an additive/subtractive value to the ambient temp |
ansond | 0:26c48388f725 | 129 | float scale = Pot2.read(); |
ansond | 0:26c48388f725 | 130 | scale = scale * 20.0; // scale to 0 - 20 |
ansond | 0:26c48388f725 | 131 | scale = scale - 10.0; // scale -10 to +10 |
ansond | 0:26c48388f725 | 132 | |
ansond | 0:26c48388f725 | 133 | // Get the Temperature (in C) |
ansond | 0:26c48388f725 | 134 | float c = temp_sensor.read(); |
ansond | 0:26c48388f725 | 135 | float f = ((9/5) * c ) + 32; |
ansond | 0:26c48388f725 | 136 | |
ansond | 0:26c48388f725 | 137 | // now get the ambient temp and scale it |
ansond | 0:26c48388f725 | 138 | this->m_temperature = c + scale; |
ansond | 0:26c48388f725 | 139 | this->display("Temp %.1f C (%.1f F)",this->m_temperature,f); |
ansond | 0:26c48388f725 | 140 | this->display_lcd("Temp: %.1f C (%.1f F)",this->m_temperature,f); |
ansond | 0:26c48388f725 | 141 | |
ansond | 0:26c48388f725 | 142 | // return the temperature |
ansond | 0:26c48388f725 | 143 | return this->m_temperature; |
ansond | 0:26c48388f725 | 144 | } |
ansond | 0:26c48388f725 | 145 | |
ansond | 0:26c48388f725 | 146 | // get the current battery level |
ansond | 0:26c48388f725 | 147 | float Thermostat::getBatteryLevel() { |
ansond | 0:26c48388f725 | 148 | // get Pot 1 an additive/subtractive value to simulate battery level |
ansond | 0:26c48388f725 | 149 | this->m_battery = Pot1.read(); |
ansond | 0:26c48388f725 | 150 | this->m_battery = this->m_battery * 100.0; // scale to 0 - 100; |
ansond | 0:26c48388f725 | 151 | |
ansond | 0:26c48388f725 | 152 | // return the battery level |
ansond | 0:26c48388f725 | 153 | return this->m_battery; |
ansond | 0:26c48388f725 | 154 | } |
ansond | 0:26c48388f725 | 155 | |
ansond | 0:26c48388f725 | 156 | // receive from the heroku SocketIO web service |
ansond | 0:26c48388f725 | 157 | char *Thermostat::receiveFromWSService(char *buffer) { |
ansond | 0:26c48388f725 | 158 | bool success = socketio.read(buffer); |
ansond | 0:26c48388f725 | 159 | if (success == true) |
ansond | 0:26c48388f725 | 160 | this->display("SocketIO: Read success"); |
ansond | 0:26c48388f725 | 161 | else |
ansond | 0:26c48388f725 | 162 | this->display("SocketIO: Read failure"); |
ansond | 0:26c48388f725 | 163 | |
ansond | 0:26c48388f725 | 164 | return buffer; |
ansond | 0:26c48388f725 | 165 | } |
ansond | 0:26c48388f725 | 166 | |
ansond | 0:26c48388f725 | 167 | // translate the LED status |
ansond | 0:26c48388f725 | 168 | int Thermostat::translateLEDStatus(const char *status, int current) { |
ansond | 0:26c48388f725 | 169 | int i_status = current; |
ansond | 0:26c48388f725 | 170 | |
ansond | 0:26c48388f725 | 171 | if (status != NULL && strlen(status) > 0) { |
ansond | 0:26c48388f725 | 172 | if (strcmp(status,"on") == 0 || strcmp(status,"ON") == 0 || strcmp(status,"On") == 0 || strcmp(status,"1") == 0 || strcmp(status,"oN") == 0) |
ansond | 0:26c48388f725 | 173 | i_status = 1; |
ansond | 0:26c48388f725 | 174 | if (strcmp(status,"off") == 0 || strcmp(status,"OFF") == 0 || strcmp(status,"Off") == 0 || strcmp(status,"0") == 0 || strcmp(status,"oFF") == 0 || strcmp(status,"oF") == 0 || strcmp(status,"ofF") == 0) |
ansond | 0:26c48388f725 | 175 | i_status = 0; |
ansond | 0:26c48388f725 | 176 | } |
ansond | 0:26c48388f725 | 177 | |
ansond | 0:26c48388f725 | 178 | // return the status |
ansond | 0:26c48388f725 | 179 | return i_status; |
ansond | 0:26c48388f725 | 180 | } |
ansond | 0:26c48388f725 | 181 | |
ansond | 0:26c48388f725 | 182 | // reset the device status to OK |
ansond | 0:26c48388f725 | 183 | void Thermostat::resetDeviceStatus() { |
ansond | 0:26c48388f725 | 184 | this->turnRGBLEDGreen(); |
ansond | 0:26c48388f725 | 185 | this->m_status = "OK"; |
ansond | 0:26c48388f725 | 186 | socketio.resetMessageCounter(); |
ansond | 0:26c48388f725 | 187 | this->resetAllLEDs(); |
ansond | 0:26c48388f725 | 188 | } |
ansond | 0:26c48388f725 | 189 | |
ansond | 0:26c48388f725 | 190 | // basic parsing and processing of the control message |
ansond | 0:26c48388f725 | 191 | // |
ansond | 0:26c48388f725 | 192 | // Control Message Format: 5:::{"name":"control-device","args":[{"hello":"world"}]} |
ansond | 0:26c48388f725 | 193 | // |
ansond | 0:26c48388f725 | 194 | void Thermostat::parseAndActOnControlMessage(char *json) { |
ansond | 0:26c48388f725 | 195 | picojson::value v; |
ansond | 0:26c48388f725 | 196 | |
ansond | 0:26c48388f725 | 197 | if (json != NULL && strlen(json) > 0 && strstr(json,"{") != NULL) { |
ansond | 0:26c48388f725 | 198 | // move past the socket.io header |
ansond | 0:26c48388f725 | 199 | char *json_proper = strstr(json,"{"); |
ansond | 0:26c48388f725 | 200 | |
ansond | 0:26c48388f725 | 201 | // parse the packet |
ansond | 0:26c48388f725 | 202 | string err = picojson::parse(v, json_proper, json_proper + strlen(json_proper)); |
ansond | 0:26c48388f725 | 203 | |
ansond | 0:26c48388f725 | 204 | // the args value is an array of json values |
ansond | 0:26c48388f725 | 205 | picojson::array list = v.get("args").get<picojson::array>(); |
ansond | 0:26c48388f725 | 206 | |
ansond | 0:26c48388f725 | 207 | // loop through the array and parse/process each element |
ansond | 0:26c48388f725 | 208 | for (picojson::array::iterator iter = list.begin(); iter != list.end(); ++iter) { |
ansond | 0:26c48388f725 | 209 | // Toggle LEDs |
ansond | 0:26c48388f725 | 210 | if ((*iter).get("led1") != NULL) led1.write(this->translateLEDStatus((*iter).get("led1").get<string>().c_str(),(int)led1)); |
ansond | 0:26c48388f725 | 211 | if ((*iter).get("led2") != NULL) led2.write(this->translateLEDStatus((*iter).get("led2").get<string>().c_str(),(int)led2)); |
ansond | 0:26c48388f725 | 212 | if ((*iter).get("led3") != NULL) led3.write(this->translateLEDStatus((*iter).get("led3").get<string>().c_str(),(int)led3)); |
ansond | 0:26c48388f725 | 213 | if ((*iter).get("led4") != NULL) led4.write(this->translateLEDStatus((*iter).get("led4").get<string>().c_str(),(int)led4)); |
ansond | 0:26c48388f725 | 214 | if ((*iter).get("reset") != NULL) this->resetDeviceStatus(); |
ansond | 0:26c48388f725 | 215 | if ((*iter).get("blink") != NULL) this->blinkAllLEDs(); |
ansond | 0:26c48388f725 | 216 | |
ansond | 0:26c48388f725 | 217 | // |
ansond | 0:26c48388f725 | 218 | // 2013 DreamForce MiniHack - add code to look for a "text" message - send its contents to the LCD panel |
ansond | 1:3faa003ad6e6 | 219 | // using the this->displayTextMessage(char *) method |
ansond | 0:26c48388f725 | 220 | // |
ansond | 0:26c48388f725 | 221 | // Answer: |
ansond | 0:26c48388f725 | 222 | // |
ansond | 1:3faa003ad6e6 | 223 | if ((*iter).get("text") != NULL) this->displayTextMessage((*iter).get("text").get<string>().c_str()); |
ansond | 0:26c48388f725 | 224 | } |
ansond | 0:26c48388f725 | 225 | } |
ansond | 0:26c48388f725 | 226 | } |
ansond | 0:26c48388f725 | 227 | |
ansond | 0:26c48388f725 | 228 | // recv and process a control message |
ansond | 0:26c48388f725 | 229 | void Thermostat::processControlMessage() { |
ansond | 0:26c48388f725 | 230 | |
ansond | 0:26c48388f725 | 231 | if (socketio.is_connected()) { |
ansond | 0:26c48388f725 | 232 | char message[SOCKETIO_MESSAGE_LENGTH]; |
ansond | 0:26c48388f725 | 233 | if (socketio.read(message)) { |
ansond | 0:26c48388f725 | 234 | // log the message |
ansond | 0:26c48388f725 | 235 | this->display("Received control message: %s",message); |
ansond | 0:26c48388f725 | 236 | |
ansond | 0:26c48388f725 | 237 | // process the message |
ansond | 0:26c48388f725 | 238 | this->parseAndActOnControlMessage(message); |
ansond | 0:26c48388f725 | 239 | |
ansond | 0:26c48388f725 | 240 | // blink the RX led |
ansond | 0:26c48388f725 | 241 | this->blinkTransportRxLED(); |
ansond | 0:26c48388f725 | 242 | } |
ansond | 0:26c48388f725 | 243 | else { |
ansond | 0:26c48388f725 | 244 | // no messages received - log |
ansond | 0:26c48388f725 | 245 | this->display("No control message received. OK"); |
ansond | 0:26c48388f725 | 246 | } |
ansond | 0:26c48388f725 | 247 | } |
ansond | 0:26c48388f725 | 248 | } |
ansond | 0:26c48388f725 | 249 | |
ansond | 0:26c48388f725 | 250 | // send status (no params) to the service |
ansond | 0:26c48388f725 | 251 | void Thermostat::sendStatus() { |
ansond | 0:26c48388f725 | 252 | // send the status |
ansond | 0:26c48388f725 | 253 | this->sendStatus(this->getTemperature(),this->getBatteryLevel()); |
ansond | 0:26c48388f725 | 254 | } |
ansond | 0:26c48388f725 | 255 | |
ansond | 0:26c48388f725 | 256 | // send status (temp & battery) to the service |
ansond | 0:26c48388f725 | 257 | void Thermostat::sendStatus(float temp, int bat) { |
ansond | 0:26c48388f725 | 258 | // incorporate location coordinates |
ansond | 0:26c48388f725 | 259 | this->sendStatus(temp,this->m_latitude,this->m_longitude,bat); |
ansond | 0:26c48388f725 | 260 | } |
ansond | 0:26c48388f725 | 261 | |
ansond | 0:26c48388f725 | 262 | // send status (temp, lat/long, battery) to the service |
ansond | 0:26c48388f725 | 263 | void Thermostat::sendStatus(float temp, float latitude, float longitude, float bat) { |
ansond | 0:26c48388f725 | 264 | // Announce |
ansond | 0:26c48388f725 | 265 | this->display("Send: status..."); |
ansond | 0:26c48388f725 | 266 | this->display_lcd("Sending status..."); |
ansond | 0:26c48388f725 | 267 | |
ansond | 0:26c48388f725 | 268 | // now send... |
ansond | 0:26c48388f725 | 269 | int sent = socketio.emit(temp,latitude,longitude,bat,this->getErrorState(),this->m_status); |
ansond | 0:26c48388f725 | 270 | |
ansond | 0:26c48388f725 | 271 | // Log |
ansond | 0:26c48388f725 | 272 | if (sent > 0) { |
ansond | 6:33f71cf9a04f | 273 | this->display("Send success. Ready...Location: %s",lcd_locations[location_index]); |
ansond | 6:33f71cf9a04f | 274 | this->display_lcd("Send status: OK.\r\nSleeping...\r\nLocation: %s",lcd_locations[location_index]); |
ansond | 0:26c48388f725 | 275 | } |
ansond | 0:26c48388f725 | 276 | else { |
ansond | 0:26c48388f725 | 277 | this->display("Send Failed: sent=%d",sent); |
ansond | 5:9866322ed225 | 278 | this->display_lcd("Send status: FAILED.\r\nResetting..."); |
ansond | 5:9866322ed225 | 279 | NVIC_SystemReset(); |
ansond | 0:26c48388f725 | 280 | } |
ansond | 0:26c48388f725 | 281 | |
ansond | 0:26c48388f725 | 282 | // blink the TX led |
ansond | 0:26c48388f725 | 283 | this->blinkTransportTxLED(); |
ansond | 0:26c48388f725 | 284 | } |
ansond | 0:26c48388f725 | 285 | |
ansond | 0:26c48388f725 | 286 | // connect to the heroku WebService |
ansond | 0:26c48388f725 | 287 | bool Thermostat::connectWebSocketService() { |
ansond | 0:26c48388f725 | 288 | // Log |
ansond | 0:26c48388f725 | 289 | this->display("Connecting to SocketIO..."); |
ansond | 0:26c48388f725 | 290 | this->display_lcd("SocketIO connecting..."); |
ansond | 0:26c48388f725 | 291 | |
ansond | 0:26c48388f725 | 292 | // only connect if we are not already so |
ansond | 0:26c48388f725 | 293 | if (!socketio.is_connected()) socketio.connect(); |
ansond | 0:26c48388f725 | 294 | |
ansond | 0:26c48388f725 | 295 | // check connection status |
ansond | 0:26c48388f725 | 296 | bool connected = socketio.is_connected(); |
ansond | 0:26c48388f725 | 297 | |
ansond | 0:26c48388f725 | 298 | // log |
ansond | 0:26c48388f725 | 299 | if (connected == true) { |
ansond | 0:26c48388f725 | 300 | this->display("SocketIO: Connected!"); |
ansond | 0:26c48388f725 | 301 | this->display_lcd("SocketIO: Connected."); |
ansond | 0:26c48388f725 | 302 | } |
ansond | 0:26c48388f725 | 303 | else { |
ansond | 0:26c48388f725 | 304 | this->display("SocketIO: Not connected!"); |
ansond | 0:26c48388f725 | 305 | this->display_lcd("SocketIO: Connect FAILED."); |
ansond | 0:26c48388f725 | 306 | } |
ansond | 0:26c48388f725 | 307 | |
ansond | 0:26c48388f725 | 308 | // return the status |
ansond | 0:26c48388f725 | 309 | return connected; |
ansond | 0:26c48388f725 | 310 | } |
ansond | 0:26c48388f725 | 311 | |
ansond | 0:26c48388f725 | 312 | // Connect up Ethernet |
ansond | 0:26c48388f725 | 313 | bool Thermostat::connectEthernet() { |
ansond | 0:26c48388f725 | 314 | bool connected = false; |
ansond | 0:26c48388f725 | 315 | char *ipAddr = NULL; |
ansond | 0:26c48388f725 | 316 | |
ansond | 0:26c48388f725 | 317 | // Use DHCP |
ansond | 0:26c48388f725 | 318 | this->display("Initializing Ethernet..."); |
ansond | 0:26c48388f725 | 319 | this->display_lcd("Ethernet: Initializing..."); |
ansond | 0:26c48388f725 | 320 | ethernet.init(); |
ansond | 0:26c48388f725 | 321 | |
ansond | 0:26c48388f725 | 322 | // attempt connection |
ansond | 0:26c48388f725 | 323 | this->display("Connecting Ethernet..."); |
ansond | 0:26c48388f725 | 324 | this->display_lcd("Ethernet: Connecting..."); |
ansond | 0:26c48388f725 | 325 | if (ethernet.connect() == 0) connected = true; |
ansond | 0:26c48388f725 | 326 | |
ansond | 0:26c48388f725 | 327 | // check connection status |
ansond | 0:26c48388f725 | 328 | if (connected) { |
ansond | 0:26c48388f725 | 329 | ipAddr = ethernet.getIPAddress(); |
ansond | 0:26c48388f725 | 330 | if (ipAddr != NULL && strlen(ipAddr) > 0) |
ansond | 0:26c48388f725 | 331 | connected = true; |
ansond | 0:26c48388f725 | 332 | } |
ansond | 0:26c48388f725 | 333 | |
ansond | 0:26c48388f725 | 334 | // log |
ansond | 0:26c48388f725 | 335 | if (connected == true) { |
ansond | 0:26c48388f725 | 336 | this->display("Ethernet: Connected.\r\nIP: %s", ipAddr); |
ansond | 0:26c48388f725 | 337 | this->display_lcd("Ethernet: Connected\r\nIP: %s",ipAddr); |
ansond | 0:26c48388f725 | 338 | } |
ansond | 0:26c48388f725 | 339 | else { |
ansond | 0:26c48388f725 | 340 | this->display("Ethernet: Not connected"); |
ansond | 0:26c48388f725 | 341 | this->display_lcd("Ethernet: Connect FAILED."); |
ansond | 0:26c48388f725 | 342 | } |
ansond | 0:26c48388f725 | 343 | |
ansond | 0:26c48388f725 | 344 | // return the status |
ansond | 0:26c48388f725 | 345 | return connected; |
ansond | 0:26c48388f725 | 346 | } |
ansond | 0:26c48388f725 | 347 | |
ansond | 0:26c48388f725 | 348 | // gracefully de-register and close down the WS connection |
ansond | 0:26c48388f725 | 349 | void Thermostat::gracefullyDisconnect() { |
ansond | 0:26c48388f725 | 350 | // disconnect |
ansond | 0:26c48388f725 | 351 | if (socketio.is_connected()) socketio.close(); |
ansond | 0:26c48388f725 | 352 | |
ansond | 0:26c48388f725 | 353 | // announce |
ansond | 0:26c48388f725 | 354 | this->display("Disconnected."); |
ansond | 0:26c48388f725 | 355 | this->display_lcd("Disconnected."); |
ansond | 0:26c48388f725 | 356 | this->turnRGBLEDBlue(); |
ansond | 0:26c48388f725 | 357 | |
ansond | 0:26c48388f725 | 358 | // reset any status |
ansond | 0:26c48388f725 | 359 | this->m_status = "OK"; |
ansond | 0:26c48388f725 | 360 | socketio.resetMessageCounter(); |
ansond | 0:26c48388f725 | 361 | } |
ansond | 0:26c48388f725 | 362 | |
ansond | 0:26c48388f725 | 363 | // external function in main() to check for the CTRL-C key press to exit the application gracefully |
ansond | 0:26c48388f725 | 364 | extern void checkForExit(); |
ansond | 0:26c48388f725 | 365 | |
ansond | 0:26c48388f725 | 366 | // main loop |
ansond | 0:26c48388f725 | 367 | void Thermostat::mainLoop() { |
ansond | 0:26c48388f725 | 368 | // initialize our location |
ansond | 0:26c48388f725 | 369 | this->initLocation(); |
ansond | 0:26c48388f725 | 370 | |
ansond | 0:26c48388f725 | 371 | // begin the main loop |
ansond | 0:26c48388f725 | 372 | while(true) { |
ansond | 0:26c48388f725 | 373 | // sleep for a bit |
ansond | 0:26c48388f725 | 374 | checkForExit(); |
ansond | 0:26c48388f725 | 375 | wait(DEFAULT_MAIN_LOOP_WAIT); |
ansond | 0:26c48388f725 | 376 | |
ansond | 0:26c48388f725 | 377 | // announce our position |
ansond | 0:26c48388f725 | 378 | this->updateCoordinates(); |
ansond | 4:379e9ad21486 | 379 | |
ansond | 5:9866322ed225 | 380 | // if not connected... reset |
ansond | 4:379e9ad21486 | 381 | if (!socketio.is_connected()){ |
ansond | 5:9866322ed225 | 382 | // reset |
ansond | 5:9866322ed225 | 383 | NVIC_SystemReset(); |
ansond | 4:379e9ad21486 | 384 | } |
ansond | 0:26c48388f725 | 385 | |
ansond | 0:26c48388f725 | 386 | // check and react to the joystick button press |
ansond | 0:26c48388f725 | 387 | if (joystick_pressed) { |
ansond | 0:26c48388f725 | 388 | if (this->m_rgbLEDColor > 1) { |
ansond | 0:26c48388f725 | 389 | this->turnRGBLEDRed(); |
ansond | 0:26c48388f725 | 390 | this->m_status = "FAIL"; |
ansond | 0:26c48388f725 | 391 | } |
ansond | 0:26c48388f725 | 392 | else { |
ansond | 0:26c48388f725 | 393 | this->turnRGBLEDGreen(); |
ansond | 0:26c48388f725 | 394 | this->m_status = "OK"; |
ansond | 0:26c48388f725 | 395 | } |
ansond | 0:26c48388f725 | 396 | } |
ansond | 0:26c48388f725 | 397 | else if (socketio.is_connected() && this->m_rgbLEDColor > 121) { |
ansond | 0:26c48388f725 | 398 | this->turnRGBLEDGreen(); |
ansond | 0:26c48388f725 | 399 | } |
ansond | 0:26c48388f725 | 400 | |
ansond | 0:26c48388f725 | 401 | // check the status of the joystick |
ansond | 0:26c48388f725 | 402 | if (joystick) { |
ansond | 0:26c48388f725 | 403 | if (socketio.is_connected()) { |
ansond | 0:26c48388f725 | 404 | // announce |
ansond | 0:26c48388f725 | 405 | this->display("Disconnecting..."); |
ansond | 0:26c48388f725 | 406 | this->display_lcd("Disconnecting..."); |
ansond | 0:26c48388f725 | 407 | |
ansond | 0:26c48388f725 | 408 | // disconnect |
ansond | 0:26c48388f725 | 409 | this->gracefullyDisconnect(); |
ansond | 0:26c48388f725 | 410 | } |
ansond | 0:26c48388f725 | 411 | else if (!socketio.is_connected()){ |
ansond | 0:26c48388f725 | 412 | // announce |
ansond | 0:26c48388f725 | 413 | this->display("Re-connecting..."); |
ansond | 0:26c48388f725 | 414 | this->display_lcd("Re-connecting..."); |
ansond | 0:26c48388f725 | 415 | |
ansond | 0:26c48388f725 | 416 | // re-connect |
ansond | 0:26c48388f725 | 417 | if (this->connectWebSocketService()) { |
ansond | 0:26c48388f725 | 418 | // announce |
ansond | 0:26c48388f725 | 419 | this->display("Reconnect success"); |
ansond | 0:26c48388f725 | 420 | this->display_lcd("Reconnect: SUCCESS"); |
ansond | 0:26c48388f725 | 421 | this->turnRGBLEDGreen(); |
ansond | 0:26c48388f725 | 422 | this->resetAllLEDs(); |
ansond | 0:26c48388f725 | 423 | } |
ansond | 0:26c48388f725 | 424 | else { |
ansond | 0:26c48388f725 | 425 | // announce |
ansond | 0:26c48388f725 | 426 | this->display("Reconnect failure"); |
ansond | 0:26c48388f725 | 427 | this->display_lcd("Reconnect: FAILED"); |
ansond | 0:26c48388f725 | 428 | this->gracefullyDisconnect(); |
ansond | 0:26c48388f725 | 429 | this->turnRGBLEDRed(); |
ansond | 0:26c48388f725 | 430 | } |
ansond | 0:26c48388f725 | 431 | } |
ansond | 0:26c48388f725 | 432 | } |
ansond | 0:26c48388f725 | 433 | |
ansond | 0:26c48388f725 | 434 | // if we are connected, send our status |
ansond | 0:26c48388f725 | 435 | if (socketio.is_connected()) { |
ansond | 0:26c48388f725 | 436 | // send status |
ansond | 0:26c48388f725 | 437 | this->sendStatus(); |
ansond | 0:26c48388f725 | 438 | checkForExit(); |
ansond | 0:26c48388f725 | 439 | } |
ansond | 0:26c48388f725 | 440 | |
ansond | 0:26c48388f725 | 441 | // if we are connected, read any control we may receive |
ansond | 0:26c48388f725 | 442 | if (socketio.is_connected()) { |
ansond | 0:26c48388f725 | 443 | // process control messages |
ansond | 0:26c48388f725 | 444 | this->processControlMessage(); |
ansond | 0:26c48388f725 | 445 | checkForExit(); |
ansond | 0:26c48388f725 | 446 | } |
ansond | 0:26c48388f725 | 447 | } |
ansond | 0:26c48388f725 | 448 | } |
ansond | 0:26c48388f725 | 449 | |
ansond | 0:26c48388f725 | 450 | // Run the Demo |
ansond | 0:26c48388f725 | 451 | void Thermostat::runDemo() { |
ansond | 0:26c48388f725 | 452 | // Announce |
ansond | 6:33f71cf9a04f | 453 | this->display("Thermostat Remote v1.0"); |
ansond | 6:33f71cf9a04f | 454 | this->display_lcd("Thermostat Remote\r\nDemo v1.0"); |
ansond | 0:26c48388f725 | 455 | |
ansond | 0:26c48388f725 | 456 | // init the RGB LED |
ansond | 0:26c48388f725 | 457 | this->display("Initializing LEDs..."); |
ansond | 0:26c48388f725 | 458 | this->turnRGBLEDBlue(); |
ansond | 0:26c48388f725 | 459 | |
ansond | 0:26c48388f725 | 460 | // Log |
ansond | 0:26c48388f725 | 461 | this->display("Connecting..."); |
ansond | 0:26c48388f725 | 462 | this->display_lcd("Connecting..."); |
ansond | 0:26c48388f725 | 463 | |
ansond | 0:26c48388f725 | 464 | // connect and send the initial status |
ansond | 0:26c48388f725 | 465 | if (this->connectEthernet() == true && this->connectWebSocketService() == true) { |
ansond | 0:26c48388f725 | 466 | this->sendStatus(); |
ansond | 0:26c48388f725 | 467 | this->mainLoop(); |
ansond | 0:26c48388f725 | 468 | } |
ansond | 0:26c48388f725 | 469 | else { |
ansond | 0:26c48388f725 | 470 | this->display("Connection failure. Application exiting..."); |
ansond | 0:26c48388f725 | 471 | this->display_lcd("Connect: FAILURE.\r\nApplication Exiting"); |
ansond | 0:26c48388f725 | 472 | } |
ansond | 0:26c48388f725 | 473 | |
ansond | 0:26c48388f725 | 474 | // exit the application if we get here |
ansond | 6:33f71cf9a04f | 475 | NVIC_SystemReset(); |
ansond | 0:26c48388f725 | 476 | } |