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
ThermostatSocketIO.cpp
00001 #include "ThermostatSocketIO.h" 00002 00003 // Default SocketIO URL 00004 #define DEFAULT_URL "mc-control-1.herokuapp.com" 00005 00006 // message counter 00007 int counter = 1; 00008 00009 // constructor 00010 ThermostatSocketIO::ThermostatSocketIO(char *devname) : SocketIO(DEFAULT_URL) { 00011 this->device_name = devname; 00012 } 00013 00014 // constructor 00015 ThermostatSocketIO::ThermostatSocketIO(char * myurl, char *devname) : SocketIO(myurl) { 00016 this->device_name = devname; 00017 } 00018 00019 // reset the message counter 00020 void ThermostatSocketIO::resetMessageCounter() { 00021 counter = 1; 00022 } 00023 00024 // emit a message (broadcast) to the SocketIO server 00025 int ThermostatSocketIO::emit(float temp, float latitude, float longitude, float bat, int errorState, char *t_status) { 00026 // create json string with acc/tmp data 00027 char send[256]; 00028 char *json = this->createJSON(send,temp,latitude,longitude,bat,errorState,t_status); 00029 00030 // handle the message name 00031 char message_name[32]; 00032 if (counter == 1) 00033 // we need to register the device 00034 sprintf(message_name,"register-device"); 00035 else 00036 // we are simply issuing readings... 00037 sprintf(message_name,"readings"); 00038 00039 // increment our counter 00040 ++counter; 00041 00042 // emit this json 00043 return ((SocketIO *)this)->emit(message_name,json); 00044 } 00045 00046 // Close the thermostat socket.io 00047 bool ThermostatSocketIO::close() { 00048 char buf[256]; 00049 char message_name[32]; 00050 00051 // we need to register the device 00052 sprintf(message_name,"disconnect"); 00053 00054 // create the deregister JSON message 00055 char *json = this->createDeregisterJSON(buf); 00056 00057 // now send... 00058 int sent = ((SocketIO *)this)->emit(message_name,json); 00059 00060 // now close 00061 return ((SocketIO *)this)->close(); 00062 } 00063 00064 // create the JSON to deregister the device 00065 char *ThermostatSocketIO::createDeregisterJSON(char *buffer) { 00066 // create the JSON to just register the device 00067 sprintf(buffer,"[{ \"device_id\": \"%s\" } }]",this->device_name); 00068 00069 // return the JSON 00070 return buffer; 00071 } 00072 00073 // Create the JSON that is consumable by the demo webservice 00074 char *ThermostatSocketIO::createJSON(char *buffer, float temp, float latitude, float longitude, float bat, int errorState, char *t_status) { 00075 // create the message name 00076 if (counter == 1) { 00077 // create the JSON to just register the device 00078 sprintf(buffer,"[\"%s\",{\"battery\": %d, \"city\": \"Austin\", \"country\": \"US\", \"device_id\": \"%s\", \"lat\": %4.6f, \"long\": %4.6f, \"status\": \"%s\", \"temp\": %4.1f, \"switch\": %d}]", 00079 device_name, (int)bat, this->device_name, latitude, longitude, t_status, temp, errorState); 00080 } 00081 else { 00082 // create the JSON 00083 sprintf(buffer,"[{\"battery\": %d, \"city\": \"Austin\", \"country\": \"US\", \"device_id\": \"%s\", \"lat\": %4.6f, \"long\": %4.6f, \"status\": \"%s\", \"temp\": %4.1f, \"switch\": %d}]", 00084 (int)bat, this->device_name, latitude, longitude, t_status, temp, errorState); 00085 } 00086 00087 // return the JSON 00088 return buffer; 00089 }
Generated on Wed Jul 13 2022 05:16:04 by 1.7.2