Dreamforce 2013 MiniHack Thermostat Challenge - completed code

Dependencies:   C12832_lcd EthernetInterface-ansond-patched HTTPClient-thermostat-remotes LM75B MMA7660 SocketIO WebSocketClient-ThermostatDemo mbed-rtos mbed picojson

Committer:
ansond
Date:
Mon Nov 11 20:35:49 2013 +0000
Revision:
5:3ab657317bfa
Parent:
3:9f70d34d3660
updates

Who changed what in which revision?

UserRevisionLine numberNew 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 0:26c48388f725 47 //
ansond 0:26c48388f725 48 // Our default Latitude and Longitude
ansond 0:26c48388f725 49 //
ansond 0:26c48388f725 50 #define DEFAULT_LATITUDE 37.7842
ansond 0:26c48388f725 51 #define DEFAULT_LONGITUDE -122.4016
ansond 0:26c48388f725 52
ansond 0:26c48388f725 53 //
ansond 0:26c48388f725 54 // Name our endpoint something unique
ansond 0:26c48388f725 55 //
ansond 0:26c48388f725 56 #define DEFAULT_ENDPOINT_NAME "DreamForce Thermostat"
ansond 0:26c48388f725 57
ansond 0:26c48388f725 58 // DreamForce 2013 Tunables END
ansond 0:26c48388f725 59
ansond 0:26c48388f725 60 // Wait Loop default sleep per iteration
ansond 0:26c48388f725 61 #define DEFAULT_MAIN_LOOP_WAIT 2.0 // 2 seconds
ansond 0:26c48388f725 62
ansond 0:26c48388f725 63 // JSON parsing support
ansond 0:26c48388f725 64 #include "picojson.h"
ansond 0:26c48388f725 65
ansond 0:26c48388f725 66 //
ansond 0:26c48388f725 67 // Accelerometer Support
ansond 0:26c48388f725 68 //
ansond 0:26c48388f725 69 #include "MMA7660.h"
ansond 0:26c48388f725 70 MMA7660 acc(p28, p27);
ansond 0:26c48388f725 71
ansond 0:26c48388f725 72 //
ansond 0:26c48388f725 73 // Temperature Sensor Support
ansond 0:26c48388f725 74 //
ansond 0:26c48388f725 75 #include "LM75B.h"
ansond 0:26c48388f725 76 LM75B temp_sensor(p28,p27);
ansond 0:26c48388f725 77
ansond 0:26c48388f725 78 //
ansond 0:26c48388f725 79 // Ethernet support
ansond 0:26c48388f725 80 //
ansond 0:26c48388f725 81 #include "EthernetInterface.h"
ansond 0:26c48388f725 82 EthernetInterface ethernet;
ansond 0:26c48388f725 83
ansond 0:26c48388f725 84 //
ansond 0:26c48388f725 85 // Thermostat SocketIO Support
ansond 0:26c48388f725 86 //
ansond 0:26c48388f725 87 #include "ThermostatSocketIO.h"
ansond 0:26c48388f725 88 ThermostatSocketIO socketio(DEFAULT_ENDPOINT_NAME);
ansond 0:26c48388f725 89
ansond 0:26c48388f725 90 // Include LED Utils
ansond 0:26c48388f725 91 #include "Thermostat-LEDUtils.h"
ansond 0:26c48388f725 92
ansond 0:26c48388f725 93 // Include Base Utils
ansond 0:26c48388f725 94 #include "Thermostat-BaseUtils.h"
ansond 0:26c48388f725 95
ansond 0:26c48388f725 96 // Include Location Stubs
ansond 0:26c48388f725 97 #include "Thermostat-Location.h"
ansond 0:26c48388f725 98
ansond 0:26c48388f725 99 // Default constructor
ansond 0:26c48388f725 100 Thermostat::Thermostat() {
ansond 0:26c48388f725 101 this->m_temperature = 0.0;
ansond 0:26c48388f725 102 this->m_battery = 50.0;
ansond 0:26c48388f725 103 this->m_status = "OK";
ansond 0:26c48388f725 104 }
ansond 0:26c48388f725 105
ansond 0:26c48388f725 106 // Destructor
ansond 0:26c48388f725 107 Thermostat::~Thermostat() {
ansond 0:26c48388f725 108 // close down connections
ansond 0:26c48388f725 109 socketio.close();
ansond 0:26c48388f725 110 ethernet.disconnect();
ansond 0:26c48388f725 111 this->turnRGBLEDBlue();
ansond 0:26c48388f725 112 }
ansond 0:26c48388f725 113
ansond 0:26c48388f725 114 // get the temp
ansond 0:26c48388f725 115 float Thermostat::getTemperature() {
ansond 0:26c48388f725 116 // get Pot 2 an additive/subtractive value to the ambient temp
ansond 0:26c48388f725 117 float scale = Pot2.read();
ansond 0:26c48388f725 118 scale = scale * 20.0; // scale to 0 - 20
ansond 0:26c48388f725 119 scale = scale - 10.0; // scale -10 to +10
ansond 0:26c48388f725 120
ansond 0:26c48388f725 121 // Get the Temperature (in C)
ansond 0:26c48388f725 122 float c = temp_sensor.read();
ansond 0:26c48388f725 123 float f = ((9/5) * c ) + 32;
ansond 0:26c48388f725 124
ansond 0:26c48388f725 125 // now get the ambient temp and scale it
ansond 0:26c48388f725 126 this->m_temperature = c + scale;
ansond 0:26c48388f725 127 this->display("Temp %.1f C (%.1f F)",this->m_temperature,f);
ansond 0:26c48388f725 128 this->display_lcd("Temp: %.1f C (%.1f F)",this->m_temperature,f);
ansond 0:26c48388f725 129
ansond 0:26c48388f725 130 // return the temperature
ansond 0:26c48388f725 131 return this->m_temperature;
ansond 0:26c48388f725 132 }
ansond 0:26c48388f725 133
ansond 0:26c48388f725 134 // get the current battery level
ansond 0:26c48388f725 135 float Thermostat::getBatteryLevel() {
ansond 0:26c48388f725 136 // get Pot 1 an additive/subtractive value to simulate battery level
ansond 0:26c48388f725 137 this->m_battery = Pot1.read();
ansond 0:26c48388f725 138 this->m_battery = this->m_battery * 100.0; // scale to 0 - 100;
ansond 0:26c48388f725 139
ansond 0:26c48388f725 140 // return the battery level
ansond 0:26c48388f725 141 return this->m_battery;
ansond 0:26c48388f725 142 }
ansond 0:26c48388f725 143
ansond 0:26c48388f725 144 // receive from the heroku SocketIO web service
ansond 0:26c48388f725 145 char *Thermostat::receiveFromWSService(char *buffer) {
ansond 0:26c48388f725 146 bool success = socketio.read(buffer);
ansond 0:26c48388f725 147 if (success == true)
ansond 0:26c48388f725 148 this->display("SocketIO: Read success");
ansond 0:26c48388f725 149 else
ansond 0:26c48388f725 150 this->display("SocketIO: Read failure");
ansond 0:26c48388f725 151
ansond 0:26c48388f725 152 return buffer;
ansond 0:26c48388f725 153 }
ansond 0:26c48388f725 154
ansond 0:26c48388f725 155 // translate the LED status
ansond 0:26c48388f725 156 int Thermostat::translateLEDStatus(const char *status, int current) {
ansond 0:26c48388f725 157 int i_status = current;
ansond 0:26c48388f725 158
ansond 0:26c48388f725 159 if (status != NULL && strlen(status) > 0) {
ansond 0:26c48388f725 160 if (strcmp(status,"on") == 0 || strcmp(status,"ON") == 0 || strcmp(status,"On") == 0 || strcmp(status,"1") == 0 || strcmp(status,"oN") == 0)
ansond 0:26c48388f725 161 i_status = 1;
ansond 0:26c48388f725 162 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 163 i_status = 0;
ansond 0:26c48388f725 164 }
ansond 0:26c48388f725 165
ansond 0:26c48388f725 166 // return the status
ansond 0:26c48388f725 167 return i_status;
ansond 0:26c48388f725 168 }
ansond 0:26c48388f725 169
ansond 0:26c48388f725 170 // reset the device status to OK
ansond 0:26c48388f725 171 void Thermostat::resetDeviceStatus() {
ansond 0:26c48388f725 172 this->turnRGBLEDGreen();
ansond 0:26c48388f725 173 this->m_status = "OK";
ansond 0:26c48388f725 174 socketio.resetMessageCounter();
ansond 0:26c48388f725 175 this->resetAllLEDs();
ansond 0:26c48388f725 176 }
ansond 0:26c48388f725 177
ansond 0:26c48388f725 178 // basic parsing and processing of the control message
ansond 0:26c48388f725 179 //
ansond 0:26c48388f725 180 // Control Message Format: 5:::{"name":"control-device","args":[{"hello":"world"}]}
ansond 0:26c48388f725 181 //
ansond 0:26c48388f725 182 void Thermostat::parseAndActOnControlMessage(char *json) {
ansond 0:26c48388f725 183 picojson::value v;
ansond 0:26c48388f725 184
ansond 0:26c48388f725 185 if (json != NULL && strlen(json) > 0 && strstr(json,"{") != NULL) {
ansond 0:26c48388f725 186 // move past the socket.io header
ansond 0:26c48388f725 187 char *json_proper = strstr(json,"{");
ansond 0:26c48388f725 188
ansond 0:26c48388f725 189 // parse the packet
ansond 0:26c48388f725 190 string err = picojson::parse(v, json_proper, json_proper + strlen(json_proper));
ansond 0:26c48388f725 191
ansond 0:26c48388f725 192 // the args value is an array of json values
ansond 0:26c48388f725 193 picojson::array list = v.get("args").get<picojson::array>();
ansond 0:26c48388f725 194
ansond 0:26c48388f725 195 // loop through the array and parse/process each element
ansond 0:26c48388f725 196 for (picojson::array::iterator iter = list.begin(); iter != list.end(); ++iter) {
ansond 0:26c48388f725 197 // Toggle LEDs
ansond 0:26c48388f725 198 if ((*iter).get("led1") != NULL) led1.write(this->translateLEDStatus((*iter).get("led1").get<string>().c_str(),(int)led1));
ansond 0:26c48388f725 199 if ((*iter).get("led2") != NULL) led2.write(this->translateLEDStatus((*iter).get("led2").get<string>().c_str(),(int)led2));
ansond 0:26c48388f725 200 if ((*iter).get("led3") != NULL) led3.write(this->translateLEDStatus((*iter).get("led3").get<string>().c_str(),(int)led3));
ansond 0:26c48388f725 201 if ((*iter).get("led4") != NULL) led4.write(this->translateLEDStatus((*iter).get("led4").get<string>().c_str(),(int)led4));
ansond 0:26c48388f725 202 if ((*iter).get("reset") != NULL) this->resetDeviceStatus();
ansond 0:26c48388f725 203 if ((*iter).get("blink") != NULL) this->blinkAllLEDs();
ansond 0:26c48388f725 204
ansond 0:26c48388f725 205 //
ansond 0:26c48388f725 206 // 2013 DreamForce MiniHack - add code to look for a "text" message - send its contents to the LCD panel
ansond 1:3faa003ad6e6 207 // using the this->displayTextMessage(char *) method
ansond 0:26c48388f725 208 //
ansond 0:26c48388f725 209 // Answer:
ansond 0:26c48388f725 210 //
ansond 1:3faa003ad6e6 211 if ((*iter).get("text") != NULL) this->displayTextMessage((*iter).get("text").get<string>().c_str());
ansond 0:26c48388f725 212 }
ansond 0:26c48388f725 213 }
ansond 0:26c48388f725 214 }
ansond 0:26c48388f725 215
ansond 0:26c48388f725 216 // recv and process a control message
ansond 0:26c48388f725 217 void Thermostat::processControlMessage() {
ansond 0:26c48388f725 218
ansond 0:26c48388f725 219 if (socketio.is_connected()) {
ansond 0:26c48388f725 220 char message[SOCKETIO_MESSAGE_LENGTH];
ansond 0:26c48388f725 221 if (socketio.read(message)) {
ansond 0:26c48388f725 222 // log the message
ansond 0:26c48388f725 223 this->display("Received control message: %s",message);
ansond 0:26c48388f725 224
ansond 0:26c48388f725 225 // process the message
ansond 0:26c48388f725 226 this->parseAndActOnControlMessage(message);
ansond 0:26c48388f725 227
ansond 0:26c48388f725 228 // blink the RX led
ansond 0:26c48388f725 229 this->blinkTransportRxLED();
ansond 0:26c48388f725 230 }
ansond 0:26c48388f725 231 else {
ansond 0:26c48388f725 232 // no messages received - log
ansond 0:26c48388f725 233 this->display("No control message received. OK");
ansond 0:26c48388f725 234 }
ansond 0:26c48388f725 235 }
ansond 0:26c48388f725 236 }
ansond 0:26c48388f725 237
ansond 0:26c48388f725 238 // send status (no params) to the service
ansond 0:26c48388f725 239 void Thermostat::sendStatus() {
ansond 0:26c48388f725 240 // send the status
ansond 0:26c48388f725 241 this->sendStatus(this->getTemperature(),this->getBatteryLevel());
ansond 0:26c48388f725 242 }
ansond 0:26c48388f725 243
ansond 0:26c48388f725 244 // send status (temp & battery) to the service
ansond 0:26c48388f725 245 void Thermostat::sendStatus(float temp, int bat) {
ansond 0:26c48388f725 246 // incorporate location coordinates
ansond 0:26c48388f725 247 this->sendStatus(temp,this->m_latitude,this->m_longitude,bat);
ansond 0:26c48388f725 248 }
ansond 0:26c48388f725 249
ansond 0:26c48388f725 250 // send status (temp, lat/long, battery) to the service
ansond 0:26c48388f725 251 void Thermostat::sendStatus(float temp, float latitude, float longitude, float bat) {
ansond 0:26c48388f725 252 // Announce
ansond 0:26c48388f725 253 this->display("Send: status...");
ansond 0:26c48388f725 254 this->display_lcd("Sending status...");
ansond 0:26c48388f725 255
ansond 0:26c48388f725 256 // now send...
ansond 0:26c48388f725 257 int sent = socketio.emit(temp,latitude,longitude,bat,this->getErrorState(),this->m_status);
ansond 0:26c48388f725 258
ansond 0:26c48388f725 259 // Log
ansond 0:26c48388f725 260 if (sent > 0) {
ansond 0:26c48388f725 261 this->display("Send success. Ready...");
ansond 0:26c48388f725 262 this->display_lcd("Send status: OK.\r\nSleeping...");
ansond 0:26c48388f725 263 }
ansond 0:26c48388f725 264 else {
ansond 0:26c48388f725 265 this->display("Send Failed: sent=%d",sent);
ansond 0:26c48388f725 266 this->display_lcd("Send status: FAILED.\r\nSleeping...");
ansond 0:26c48388f725 267 }
ansond 0:26c48388f725 268
ansond 0:26c48388f725 269 // blink the TX led
ansond 0:26c48388f725 270 this->blinkTransportTxLED();
ansond 0:26c48388f725 271 }
ansond 0:26c48388f725 272
ansond 0:26c48388f725 273 // connect to the heroku WebService
ansond 0:26c48388f725 274 bool Thermostat::connectWebSocketService() {
ansond 0:26c48388f725 275 // Log
ansond 0:26c48388f725 276 this->display("Connecting to SocketIO...");
ansond 0:26c48388f725 277 this->display_lcd("SocketIO connecting...");
ansond 0:26c48388f725 278
ansond 0:26c48388f725 279 // only connect if we are not already so
ansond 0:26c48388f725 280 if (!socketio.is_connected()) socketio.connect();
ansond 0:26c48388f725 281
ansond 0:26c48388f725 282 // check connection status
ansond 0:26c48388f725 283 bool connected = socketio.is_connected();
ansond 0:26c48388f725 284
ansond 0:26c48388f725 285 // log
ansond 0:26c48388f725 286 if (connected == true) {
ansond 0:26c48388f725 287 this->display("SocketIO: Connected!");
ansond 0:26c48388f725 288 this->display_lcd("SocketIO: Connected.");
ansond 0:26c48388f725 289 }
ansond 0:26c48388f725 290 else {
ansond 0:26c48388f725 291 this->display("SocketIO: Not connected!");
ansond 0:26c48388f725 292 this->display_lcd("SocketIO: Connect FAILED.");
ansond 0:26c48388f725 293 }
ansond 0:26c48388f725 294
ansond 0:26c48388f725 295 // return the status
ansond 0:26c48388f725 296 return connected;
ansond 0:26c48388f725 297 }
ansond 0:26c48388f725 298
ansond 0:26c48388f725 299 // Connect up Ethernet
ansond 0:26c48388f725 300 bool Thermostat::connectEthernet() {
ansond 0:26c48388f725 301 bool connected = false;
ansond 0:26c48388f725 302 char *ipAddr = NULL;
ansond 0:26c48388f725 303
ansond 0:26c48388f725 304 // Use DHCP
ansond 0:26c48388f725 305 this->display("Initializing Ethernet...");
ansond 0:26c48388f725 306 this->display_lcd("Ethernet: Initializing...");
ansond 0:26c48388f725 307 ethernet.init();
ansond 0:26c48388f725 308
ansond 0:26c48388f725 309 // attempt connection
ansond 0:26c48388f725 310 this->display("Connecting Ethernet...");
ansond 0:26c48388f725 311 this->display_lcd("Ethernet: Connecting...");
ansond 0:26c48388f725 312 if (ethernet.connect() == 0) connected = true;
ansond 0:26c48388f725 313
ansond 0:26c48388f725 314 // check connection status
ansond 0:26c48388f725 315 if (connected) {
ansond 0:26c48388f725 316 ipAddr = ethernet.getIPAddress();
ansond 0:26c48388f725 317 if (ipAddr != NULL && strlen(ipAddr) > 0)
ansond 0:26c48388f725 318 connected = true;
ansond 0:26c48388f725 319 }
ansond 0:26c48388f725 320
ansond 0:26c48388f725 321 // log
ansond 0:26c48388f725 322 if (connected == true) {
ansond 0:26c48388f725 323 this->display("Ethernet: Connected.\r\nIP: %s", ipAddr);
ansond 0:26c48388f725 324 this->display_lcd("Ethernet: Connected\r\nIP: %s",ipAddr);
ansond 0:26c48388f725 325 }
ansond 0:26c48388f725 326 else {
ansond 0:26c48388f725 327 this->display("Ethernet: Not connected");
ansond 0:26c48388f725 328 this->display_lcd("Ethernet: Connect FAILED.");
ansond 0:26c48388f725 329 }
ansond 0:26c48388f725 330
ansond 0:26c48388f725 331 // return the status
ansond 0:26c48388f725 332 return connected;
ansond 0:26c48388f725 333 }
ansond 0:26c48388f725 334
ansond 0:26c48388f725 335 // gracefully de-register and close down the WS connection
ansond 0:26c48388f725 336 void Thermostat::gracefullyDisconnect() {
ansond 0:26c48388f725 337 // disconnect
ansond 0:26c48388f725 338 if (socketio.is_connected()) socketio.close();
ansond 0:26c48388f725 339
ansond 0:26c48388f725 340 // announce
ansond 0:26c48388f725 341 this->display("Disconnected.");
ansond 0:26c48388f725 342 this->display_lcd("Disconnected.");
ansond 0:26c48388f725 343 this->turnRGBLEDBlue();
ansond 0:26c48388f725 344
ansond 0:26c48388f725 345 // reset any status
ansond 0:26c48388f725 346 this->m_status = "OK";
ansond 0:26c48388f725 347 socketio.resetMessageCounter();
ansond 0:26c48388f725 348 }
ansond 0:26c48388f725 349
ansond 0:26c48388f725 350 // external function in main() to check for the CTRL-C key press to exit the application gracefully
ansond 0:26c48388f725 351 extern void checkForExit();
ansond 0:26c48388f725 352
ansond 0:26c48388f725 353 // main loop
ansond 0:26c48388f725 354 void Thermostat::mainLoop() {
ansond 0:26c48388f725 355 // initialize our location
ansond 0:26c48388f725 356 this->initLocation();
ansond 0:26c48388f725 357
ansond 0:26c48388f725 358 // begin the main loop
ansond 0:26c48388f725 359 while(true) {
ansond 0:26c48388f725 360 // sleep for a bit
ansond 0:26c48388f725 361 checkForExit();
ansond 0:26c48388f725 362 wait(DEFAULT_MAIN_LOOP_WAIT);
ansond 0:26c48388f725 363
ansond 0:26c48388f725 364 // announce our position
ansond 0:26c48388f725 365 this->updateCoordinates();
ansond 3:9f70d34d3660 366
ansond 3:9f70d34d3660 367 // if not connected... reconnect
ansond 3:9f70d34d3660 368 if (!socketio.is_connected()){
ansond 3:9f70d34d3660 369 // announce
ansond 3:9f70d34d3660 370 this->display("Re-connecting...");
ansond 3:9f70d34d3660 371 this->display_lcd("Re-connecting...");
ansond 3:9f70d34d3660 372
ansond 3:9f70d34d3660 373 // re-connect
ansond 3:9f70d34d3660 374 if (this->connectWebSocketService()) {
ansond 3:9f70d34d3660 375 // announce
ansond 3:9f70d34d3660 376 this->display("Reconnect success");
ansond 3:9f70d34d3660 377 this->display_lcd("Reconnect: SUCCESS");
ansond 3:9f70d34d3660 378 this->turnRGBLEDGreen();
ansond 3:9f70d34d3660 379 this->resetAllLEDs();
ansond 3:9f70d34d3660 380 }
ansond 3:9f70d34d3660 381 else {
ansond 3:9f70d34d3660 382 // announce
ansond 3:9f70d34d3660 383 this->display("Reconnect failure");
ansond 3:9f70d34d3660 384 this->display_lcd("Reconnect: FAILED");
ansond 3:9f70d34d3660 385 this->gracefullyDisconnect();
ansond 3:9f70d34d3660 386 this->turnRGBLEDRed();
ansond 3:9f70d34d3660 387 }
ansond 3:9f70d34d3660 388 }
ansond 0:26c48388f725 389
ansond 0:26c48388f725 390 // check and react to the joystick button press
ansond 0:26c48388f725 391 if (joystick_pressed) {
ansond 0:26c48388f725 392 if (this->m_rgbLEDColor > 1) {
ansond 0:26c48388f725 393 this->turnRGBLEDRed();
ansond 0:26c48388f725 394 this->m_status = "FAIL";
ansond 0:26c48388f725 395 }
ansond 0:26c48388f725 396 else {
ansond 0:26c48388f725 397 this->turnRGBLEDGreen();
ansond 0:26c48388f725 398 this->m_status = "OK";
ansond 0:26c48388f725 399 }
ansond 0:26c48388f725 400 }
ansond 0:26c48388f725 401 else if (socketio.is_connected() && this->m_rgbLEDColor > 121) {
ansond 0:26c48388f725 402 this->turnRGBLEDGreen();
ansond 0:26c48388f725 403 }
ansond 0:26c48388f725 404
ansond 0:26c48388f725 405 // check the status of the joystick
ansond 0:26c48388f725 406 if (joystick) {
ansond 0:26c48388f725 407 if (socketio.is_connected()) {
ansond 0:26c48388f725 408 // announce
ansond 0:26c48388f725 409 this->display("Disconnecting...");
ansond 0:26c48388f725 410 this->display_lcd("Disconnecting...");
ansond 0:26c48388f725 411
ansond 0:26c48388f725 412 // disconnect
ansond 0:26c48388f725 413 this->gracefullyDisconnect();
ansond 0:26c48388f725 414 }
ansond 0:26c48388f725 415 else if (!socketio.is_connected()){
ansond 0:26c48388f725 416 // announce
ansond 0:26c48388f725 417 this->display("Re-connecting...");
ansond 0:26c48388f725 418 this->display_lcd("Re-connecting...");
ansond 0:26c48388f725 419
ansond 0:26c48388f725 420 // re-connect
ansond 0:26c48388f725 421 if (this->connectWebSocketService()) {
ansond 0:26c48388f725 422 // announce
ansond 0:26c48388f725 423 this->display("Reconnect success");
ansond 0:26c48388f725 424 this->display_lcd("Reconnect: SUCCESS");
ansond 0:26c48388f725 425 this->turnRGBLEDGreen();
ansond 0:26c48388f725 426 this->resetAllLEDs();
ansond 0:26c48388f725 427 }
ansond 0:26c48388f725 428 else {
ansond 0:26c48388f725 429 // announce
ansond 0:26c48388f725 430 this->display("Reconnect failure");
ansond 0:26c48388f725 431 this->display_lcd("Reconnect: FAILED");
ansond 0:26c48388f725 432 this->gracefullyDisconnect();
ansond 0:26c48388f725 433 this->turnRGBLEDRed();
ansond 0:26c48388f725 434 }
ansond 0:26c48388f725 435 }
ansond 0:26c48388f725 436 }
ansond 0:26c48388f725 437
ansond 0:26c48388f725 438 // if we are connected, send our status
ansond 0:26c48388f725 439 if (socketio.is_connected()) {
ansond 0:26c48388f725 440 // send status
ansond 0:26c48388f725 441 this->sendStatus();
ansond 0:26c48388f725 442 checkForExit();
ansond 0:26c48388f725 443 }
ansond 0:26c48388f725 444
ansond 0:26c48388f725 445 // if we are connected, read any control we may receive
ansond 0:26c48388f725 446 if (socketio.is_connected()) {
ansond 0:26c48388f725 447 // process control messages
ansond 0:26c48388f725 448 this->processControlMessage();
ansond 0:26c48388f725 449 checkForExit();
ansond 0:26c48388f725 450 }
ansond 0:26c48388f725 451 }
ansond 0:26c48388f725 452 }
ansond 0:26c48388f725 453
ansond 0:26c48388f725 454 // Run the Demo
ansond 0:26c48388f725 455 void Thermostat::runDemo() {
ansond 0:26c48388f725 456 // Announce
ansond 0:26c48388f725 457 this->display("Thermostat Hands-On Demo v1.0");
ansond 0:26c48388f725 458 this->display_lcd("Thermostat Hands-On\r\nDemo v1.0");
ansond 0:26c48388f725 459
ansond 0:26c48388f725 460 // init the RGB LED
ansond 0:26c48388f725 461 this->display("Initializing LEDs...");
ansond 0:26c48388f725 462 this->turnRGBLEDBlue();
ansond 0:26c48388f725 463
ansond 0:26c48388f725 464 // Log
ansond 0:26c48388f725 465 this->display("Connecting...");
ansond 0:26c48388f725 466 this->display_lcd("Connecting...");
ansond 0:26c48388f725 467
ansond 0:26c48388f725 468 // connect and send the initial status
ansond 0:26c48388f725 469 if (this->connectEthernet() == true && this->connectWebSocketService() == true) {
ansond 0:26c48388f725 470 this->sendStatus();
ansond 0:26c48388f725 471 this->mainLoop();
ansond 0:26c48388f725 472 }
ansond 0:26c48388f725 473 else {
ansond 0:26c48388f725 474 this->display("Connection failure. Application exiting...");
ansond 0:26c48388f725 475 this->display_lcd("Connect: FAILURE.\r\nApplication Exiting");
ansond 0:26c48388f725 476 }
ansond 0:26c48388f725 477
ansond 0:26c48388f725 478 // exit the application if we get here
ansond 0:26c48388f725 479 exit(1);
ansond 0:26c48388f725 480 }