DreamForce 2013 Hands-On Demo

Dependencies:   C12832_lcd EthernetInterface HTTPClient LM75B MMA7660 SocketIO WebSocketClient-ThermostatDemo mbed-rtos mbed picojson

Committer:
ansond
Date:
Fri Nov 01 04:14:55 2013 +0000
Revision:
3:6afb87443041
Parent:
2:be42baea4c81
updates for latlong and name

Who changed what in which revision?

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