Dreamforce Heroku Sample mbed application for the FRDM-K64F. This application uses SocketIO to connect and communicate with Heroku.

Dependencies:   BufferedSerial C12832 EthernetInterface HTTPClient-SSL LM75B MMA7660 SocketIO-k64f WebSocketClient-ThermostatDemo mbed-rtos mbed picojson

Fork of df-2013-minihack-thermostat-complete by MBED_DEMOS

Committer:
ansond
Date:
Tue Nov 05 21:31:01 2013 +0000
Revision:
0:26c48388f725
Child:
1:3faa003ad6e6
initial checkin

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("text") != NULL) this->display((*iter).get("text").get<string>().c_str());
ansond 0:26c48388f725 204 if ((*iter).get("blink") != NULL) this->blinkAllLEDs();
ansond 0:26c48388f725 205
ansond 0:26c48388f725 206 //
ansond 0:26c48388f725 207 // 2013 DreamForce MiniHack - add code to look for a "text" message - send its contents to the LCD panel
ansond 0:26c48388f725 208 // using the this->display(char *) method
ansond 0:26c48388f725 209 //
ansond 0:26c48388f725 210 // Answer:
ansond 0:26c48388f725 211 //
ansond 0:26c48388f725 212 if ((*iter).get("text") != NULL) this->display((*iter).get("text").get<string>().c_str());
ansond 0:26c48388f725 213 }
ansond 0:26c48388f725 214 }
ansond 0:26c48388f725 215 }
ansond 0:26c48388f725 216
ansond 0:26c48388f725 217 // recv and process a control message
ansond 0:26c48388f725 218 void Thermostat::processControlMessage() {
ansond 0:26c48388f725 219
ansond 0:26c48388f725 220 if (socketio.is_connected()) {
ansond 0:26c48388f725 221 char message[SOCKETIO_MESSAGE_LENGTH];
ansond 0:26c48388f725 222 if (socketio.read(message)) {
ansond 0:26c48388f725 223 // log the message
ansond 0:26c48388f725 224 this->display("Received control message: %s",message);
ansond 0:26c48388f725 225
ansond 0:26c48388f725 226 // process the message
ansond 0:26c48388f725 227 this->parseAndActOnControlMessage(message);
ansond 0:26c48388f725 228
ansond 0:26c48388f725 229 // blink the RX led
ansond 0:26c48388f725 230 this->blinkTransportRxLED();
ansond 0:26c48388f725 231 }
ansond 0:26c48388f725 232 else {
ansond 0:26c48388f725 233 // no messages received - log
ansond 0:26c48388f725 234 this->display("No control message received. OK");
ansond 0:26c48388f725 235 }
ansond 0:26c48388f725 236 }
ansond 0:26c48388f725 237 }
ansond 0:26c48388f725 238
ansond 0:26c48388f725 239 // send status (no params) to the service
ansond 0:26c48388f725 240 void Thermostat::sendStatus() {
ansond 0:26c48388f725 241 // send the status
ansond 0:26c48388f725 242 this->sendStatus(this->getTemperature(),this->getBatteryLevel());
ansond 0:26c48388f725 243 }
ansond 0:26c48388f725 244
ansond 0:26c48388f725 245 // send status (temp & battery) to the service
ansond 0:26c48388f725 246 void Thermostat::sendStatus(float temp, int bat) {
ansond 0:26c48388f725 247 // incorporate location coordinates
ansond 0:26c48388f725 248 this->sendStatus(temp,this->m_latitude,this->m_longitude,bat);
ansond 0:26c48388f725 249 }
ansond 0:26c48388f725 250
ansond 0:26c48388f725 251 // send status (temp, lat/long, battery) to the service
ansond 0:26c48388f725 252 void Thermostat::sendStatus(float temp, float latitude, float longitude, float bat) {
ansond 0:26c48388f725 253 // Announce
ansond 0:26c48388f725 254 this->display("Send: status...");
ansond 0:26c48388f725 255 this->display_lcd("Sending status...");
ansond 0:26c48388f725 256
ansond 0:26c48388f725 257 // now send...
ansond 0:26c48388f725 258 int sent = socketio.emit(temp,latitude,longitude,bat,this->getErrorState(),this->m_status);
ansond 0:26c48388f725 259
ansond 0:26c48388f725 260 // Log
ansond 0:26c48388f725 261 if (sent > 0) {
ansond 0:26c48388f725 262 this->display("Send success. Ready...");
ansond 0:26c48388f725 263 this->display_lcd("Send status: OK.\r\nSleeping...");
ansond 0:26c48388f725 264 }
ansond 0:26c48388f725 265 else {
ansond 0:26c48388f725 266 this->display("Send Failed: sent=%d",sent);
ansond 0:26c48388f725 267 this->display_lcd("Send status: FAILED.\r\nSleeping...");
ansond 0:26c48388f725 268 }
ansond 0:26c48388f725 269
ansond 0:26c48388f725 270 // blink the TX led
ansond 0:26c48388f725 271 this->blinkTransportTxLED();
ansond 0:26c48388f725 272 }
ansond 0:26c48388f725 273
ansond 0:26c48388f725 274 // connect to the heroku WebService
ansond 0:26c48388f725 275 bool Thermostat::connectWebSocketService() {
ansond 0:26c48388f725 276 // Log
ansond 0:26c48388f725 277 this->display("Connecting to SocketIO...");
ansond 0:26c48388f725 278 this->display_lcd("SocketIO connecting...");
ansond 0:26c48388f725 279
ansond 0:26c48388f725 280 // only connect if we are not already so
ansond 0:26c48388f725 281 if (!socketio.is_connected()) socketio.connect();
ansond 0:26c48388f725 282
ansond 0:26c48388f725 283 // check connection status
ansond 0:26c48388f725 284 bool connected = socketio.is_connected();
ansond 0:26c48388f725 285
ansond 0:26c48388f725 286 // log
ansond 0:26c48388f725 287 if (connected == true) {
ansond 0:26c48388f725 288 this->display("SocketIO: Connected!");
ansond 0:26c48388f725 289 this->display_lcd("SocketIO: Connected.");
ansond 0:26c48388f725 290 }
ansond 0:26c48388f725 291 else {
ansond 0:26c48388f725 292 this->display("SocketIO: Not connected!");
ansond 0:26c48388f725 293 this->display_lcd("SocketIO: Connect FAILED.");
ansond 0:26c48388f725 294 }
ansond 0:26c48388f725 295
ansond 0:26c48388f725 296 // return the status
ansond 0:26c48388f725 297 return connected;
ansond 0:26c48388f725 298 }
ansond 0:26c48388f725 299
ansond 0:26c48388f725 300 // Connect up Ethernet
ansond 0:26c48388f725 301 bool Thermostat::connectEthernet() {
ansond 0:26c48388f725 302 bool connected = false;
ansond 0:26c48388f725 303 char *ipAddr = NULL;
ansond 0:26c48388f725 304
ansond 0:26c48388f725 305 // Use DHCP
ansond 0:26c48388f725 306 this->display("Initializing Ethernet...");
ansond 0:26c48388f725 307 this->display_lcd("Ethernet: Initializing...");
ansond 0:26c48388f725 308 ethernet.init();
ansond 0:26c48388f725 309
ansond 0:26c48388f725 310 // attempt connection
ansond 0:26c48388f725 311 this->display("Connecting Ethernet...");
ansond 0:26c48388f725 312 this->display_lcd("Ethernet: Connecting...");
ansond 0:26c48388f725 313 if (ethernet.connect() == 0) connected = true;
ansond 0:26c48388f725 314
ansond 0:26c48388f725 315 // check connection status
ansond 0:26c48388f725 316 if (connected) {
ansond 0:26c48388f725 317 ipAddr = ethernet.getIPAddress();
ansond 0:26c48388f725 318 if (ipAddr != NULL && strlen(ipAddr) > 0)
ansond 0:26c48388f725 319 connected = true;
ansond 0:26c48388f725 320 }
ansond 0:26c48388f725 321
ansond 0:26c48388f725 322 // log
ansond 0:26c48388f725 323 if (connected == true) {
ansond 0:26c48388f725 324 this->display("Ethernet: Connected.\r\nIP: %s", ipAddr);
ansond 0:26c48388f725 325 this->display_lcd("Ethernet: Connected\r\nIP: %s",ipAddr);
ansond 0:26c48388f725 326 }
ansond 0:26c48388f725 327 else {
ansond 0:26c48388f725 328 this->display("Ethernet: Not connected");
ansond 0:26c48388f725 329 this->display_lcd("Ethernet: Connect FAILED.");
ansond 0:26c48388f725 330 }
ansond 0:26c48388f725 331
ansond 0:26c48388f725 332 // return the status
ansond 0:26c48388f725 333 return connected;
ansond 0:26c48388f725 334 }
ansond 0:26c48388f725 335
ansond 0:26c48388f725 336 // gracefully de-register and close down the WS connection
ansond 0:26c48388f725 337 void Thermostat::gracefullyDisconnect() {
ansond 0:26c48388f725 338 // disconnect
ansond 0:26c48388f725 339 if (socketio.is_connected()) socketio.close();
ansond 0:26c48388f725 340
ansond 0:26c48388f725 341 // announce
ansond 0:26c48388f725 342 this->display("Disconnected.");
ansond 0:26c48388f725 343 this->display_lcd("Disconnected.");
ansond 0:26c48388f725 344 this->turnRGBLEDBlue();
ansond 0:26c48388f725 345
ansond 0:26c48388f725 346 // reset any status
ansond 0:26c48388f725 347 this->m_status = "OK";
ansond 0:26c48388f725 348 socketio.resetMessageCounter();
ansond 0:26c48388f725 349 }
ansond 0:26c48388f725 350
ansond 0:26c48388f725 351 // external function in main() to check for the CTRL-C key press to exit the application gracefully
ansond 0:26c48388f725 352 extern void checkForExit();
ansond 0:26c48388f725 353
ansond 0:26c48388f725 354 // main loop
ansond 0:26c48388f725 355 void Thermostat::mainLoop() {
ansond 0:26c48388f725 356 // initialize our location
ansond 0:26c48388f725 357 this->initLocation();
ansond 0:26c48388f725 358
ansond 0:26c48388f725 359 // begin the main loop
ansond 0:26c48388f725 360 while(true) {
ansond 0:26c48388f725 361 // sleep for a bit
ansond 0:26c48388f725 362 checkForExit();
ansond 0:26c48388f725 363 wait(DEFAULT_MAIN_LOOP_WAIT);
ansond 0:26c48388f725 364
ansond 0:26c48388f725 365 // announce our position
ansond 0:26c48388f725 366 this->updateCoordinates();
ansond 0:26c48388f725 367
ansond 0:26c48388f725 368 // check and react to the joystick button press
ansond 0:26c48388f725 369 if (joystick_pressed) {
ansond 0:26c48388f725 370 if (this->m_rgbLEDColor > 1) {
ansond 0:26c48388f725 371 this->turnRGBLEDRed();
ansond 0:26c48388f725 372 this->m_status = "FAIL";
ansond 0:26c48388f725 373 }
ansond 0:26c48388f725 374 else {
ansond 0:26c48388f725 375 this->turnRGBLEDGreen();
ansond 0:26c48388f725 376 this->m_status = "OK";
ansond 0:26c48388f725 377 }
ansond 0:26c48388f725 378 }
ansond 0:26c48388f725 379 else if (socketio.is_connected() && this->m_rgbLEDColor > 121) {
ansond 0:26c48388f725 380 this->turnRGBLEDGreen();
ansond 0:26c48388f725 381 }
ansond 0:26c48388f725 382
ansond 0:26c48388f725 383 // check the status of the joystick
ansond 0:26c48388f725 384 if (joystick) {
ansond 0:26c48388f725 385 if (socketio.is_connected()) {
ansond 0:26c48388f725 386 // announce
ansond 0:26c48388f725 387 this->display("Disconnecting...");
ansond 0:26c48388f725 388 this->display_lcd("Disconnecting...");
ansond 0:26c48388f725 389
ansond 0:26c48388f725 390 // disconnect
ansond 0:26c48388f725 391 this->gracefullyDisconnect();
ansond 0:26c48388f725 392 }
ansond 0:26c48388f725 393 else if (!socketio.is_connected()){
ansond 0:26c48388f725 394 // announce
ansond 0:26c48388f725 395 this->display("Re-connecting...");
ansond 0:26c48388f725 396 this->display_lcd("Re-connecting...");
ansond 0:26c48388f725 397
ansond 0:26c48388f725 398 // re-connect
ansond 0:26c48388f725 399 if (this->connectWebSocketService()) {
ansond 0:26c48388f725 400 // announce
ansond 0:26c48388f725 401 this->display("Reconnect success");
ansond 0:26c48388f725 402 this->display_lcd("Reconnect: SUCCESS");
ansond 0:26c48388f725 403 this->turnRGBLEDGreen();
ansond 0:26c48388f725 404 this->resetAllLEDs();
ansond 0:26c48388f725 405 }
ansond 0:26c48388f725 406 else {
ansond 0:26c48388f725 407 // announce
ansond 0:26c48388f725 408 this->display("Reconnect failure");
ansond 0:26c48388f725 409 this->display_lcd("Reconnect: FAILED");
ansond 0:26c48388f725 410 this->gracefullyDisconnect();
ansond 0:26c48388f725 411 this->turnRGBLEDRed();
ansond 0:26c48388f725 412 }
ansond 0:26c48388f725 413 }
ansond 0:26c48388f725 414 }
ansond 0:26c48388f725 415
ansond 0:26c48388f725 416 // if we are connected, send our status
ansond 0:26c48388f725 417 if (socketio.is_connected()) {
ansond 0:26c48388f725 418 // send status
ansond 0:26c48388f725 419 this->sendStatus();
ansond 0:26c48388f725 420 checkForExit();
ansond 0:26c48388f725 421 }
ansond 0:26c48388f725 422
ansond 0:26c48388f725 423 // if we are connected, read any control we may receive
ansond 0:26c48388f725 424 if (socketio.is_connected()) {
ansond 0:26c48388f725 425 // process control messages
ansond 0:26c48388f725 426 this->processControlMessage();
ansond 0:26c48388f725 427 checkForExit();
ansond 0:26c48388f725 428 }
ansond 0:26c48388f725 429 }
ansond 0:26c48388f725 430 }
ansond 0:26c48388f725 431
ansond 0:26c48388f725 432 // Run the Demo
ansond 0:26c48388f725 433 void Thermostat::runDemo() {
ansond 0:26c48388f725 434 // Announce
ansond 0:26c48388f725 435 this->display("Thermostat Hands-On Demo v1.0");
ansond 0:26c48388f725 436 this->display_lcd("Thermostat Hands-On\r\nDemo v1.0");
ansond 0:26c48388f725 437
ansond 0:26c48388f725 438 // init the RGB LED
ansond 0:26c48388f725 439 this->display("Initializing LEDs...");
ansond 0:26c48388f725 440 this->turnRGBLEDBlue();
ansond 0:26c48388f725 441
ansond 0:26c48388f725 442 // Log
ansond 0:26c48388f725 443 this->display("Connecting...");
ansond 0:26c48388f725 444 this->display_lcd("Connecting...");
ansond 0:26c48388f725 445
ansond 0:26c48388f725 446 // connect and send the initial status
ansond 0:26c48388f725 447 if (this->connectEthernet() == true && this->connectWebSocketService() == true) {
ansond 0:26c48388f725 448 this->sendStatus();
ansond 0:26c48388f725 449 this->mainLoop();
ansond 0:26c48388f725 450 }
ansond 0:26c48388f725 451 else {
ansond 0:26c48388f725 452 this->display("Connection failure. Application exiting...");
ansond 0:26c48388f725 453 this->display_lcd("Connect: FAILURE.\r\nApplication Exiting");
ansond 0:26c48388f725 454 }
ansond 0:26c48388f725 455
ansond 0:26c48388f725 456 // exit the application if we get here
ansond 0:26c48388f725 457 exit(1);
ansond 0:26c48388f725 458 }