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:
Thu Oct 09 16:14:18 2014 +0000
Revision:
6:74c1e9c8c90e
Parent:
0:26c48388f725
updates of the 2013 DF heroku app ported to K64F+appshield

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:26c48388f725 1 #include "ThermostatSocketIO.h"
ansond 0:26c48388f725 2
ansond 0:26c48388f725 3 // Default SocketIO URL
ansond 0:26c48388f725 4 #define DEFAULT_URL "mc-control-1.herokuapp.com"
ansond 0:26c48388f725 5
ansond 0:26c48388f725 6 // message counter
ansond 0:26c48388f725 7 int counter = 1;
ansond 0:26c48388f725 8
ansond 0:26c48388f725 9 // constructor
ansond 0:26c48388f725 10 ThermostatSocketIO::ThermostatSocketIO(char *devname) : SocketIO(DEFAULT_URL) {
ansond 0:26c48388f725 11 this->device_name = devname;
ansond 0:26c48388f725 12 }
ansond 0:26c48388f725 13
ansond 0:26c48388f725 14 // constructor
ansond 0:26c48388f725 15 ThermostatSocketIO::ThermostatSocketIO(char * myurl, char *devname) : SocketIO(myurl) {
ansond 0:26c48388f725 16 this->device_name = devname;
ansond 0:26c48388f725 17 }
ansond 0:26c48388f725 18
ansond 0:26c48388f725 19 // reset the message counter
ansond 0:26c48388f725 20 void ThermostatSocketIO::resetMessageCounter() {
ansond 0:26c48388f725 21 counter = 1;
ansond 0:26c48388f725 22 }
ansond 0:26c48388f725 23
ansond 0:26c48388f725 24 // emit a message (broadcast) to the SocketIO server
ansond 0:26c48388f725 25 int ThermostatSocketIO::emit(float temp, float latitude, float longitude, float bat, int errorState, char *t_status) {
ansond 0:26c48388f725 26 // create json string with acc/tmp data
ansond 0:26c48388f725 27 char send[256];
ansond 0:26c48388f725 28 char *json = this->createJSON(send,temp,latitude,longitude,bat,errorState,t_status);
ansond 0:26c48388f725 29
ansond 0:26c48388f725 30 // handle the message name
ansond 0:26c48388f725 31 char message_name[32];
ansond 0:26c48388f725 32 if (counter == 1)
ansond 0:26c48388f725 33 // we need to register the device
ansond 0:26c48388f725 34 sprintf(message_name,"register-device");
ansond 0:26c48388f725 35 else
ansond 0:26c48388f725 36 // we are simply issuing readings...
ansond 0:26c48388f725 37 sprintf(message_name,"readings");
ansond 0:26c48388f725 38
ansond 0:26c48388f725 39 // increment our counter
ansond 0:26c48388f725 40 ++counter;
ansond 0:26c48388f725 41
ansond 0:26c48388f725 42 // emit this json
ansond 0:26c48388f725 43 return ((SocketIO *)this)->emit(message_name,json);
ansond 0:26c48388f725 44 }
ansond 0:26c48388f725 45
ansond 0:26c48388f725 46 // Close the thermostat socket.io
ansond 0:26c48388f725 47 bool ThermostatSocketIO::close() {
ansond 0:26c48388f725 48 char buf[256];
ansond 0:26c48388f725 49 char message_name[32];
ansond 0:26c48388f725 50
ansond 0:26c48388f725 51 // we need to register the device
ansond 0:26c48388f725 52 sprintf(message_name,"disconnect");
ansond 0:26c48388f725 53
ansond 0:26c48388f725 54 // create the deregister JSON message
ansond 0:26c48388f725 55 char *json = this->createDeregisterJSON(buf);
ansond 0:26c48388f725 56
ansond 0:26c48388f725 57 // now send...
ansond 0:26c48388f725 58 int sent = ((SocketIO *)this)->emit(message_name,json);
ansond 0:26c48388f725 59
ansond 0:26c48388f725 60 // now close
ansond 0:26c48388f725 61 return ((SocketIO *)this)->close();
ansond 0:26c48388f725 62 }
ansond 0:26c48388f725 63
ansond 0:26c48388f725 64 // create the JSON to deregister the device
ansond 0:26c48388f725 65 char *ThermostatSocketIO::createDeregisterJSON(char *buffer) {
ansond 0:26c48388f725 66 // create the JSON to just register the device
ansond 0:26c48388f725 67 sprintf(buffer,"[{ \"device_id\": \"%s\" } }]",this->device_name);
ansond 0:26c48388f725 68
ansond 0:26c48388f725 69 // return the JSON
ansond 0:26c48388f725 70 return buffer;
ansond 0:26c48388f725 71 }
ansond 0:26c48388f725 72
ansond 0:26c48388f725 73 // Create the JSON that is consumable by the demo webservice
ansond 0:26c48388f725 74 char *ThermostatSocketIO::createJSON(char *buffer, float temp, float latitude, float longitude, float bat, int errorState, char *t_status) {
ansond 0:26c48388f725 75 // create the message name
ansond 0:26c48388f725 76 if (counter == 1) {
ansond 0:26c48388f725 77 // create the JSON to just register the device
ansond 0:26c48388f725 78 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}]",
ansond 0:26c48388f725 79 device_name, (int)bat, this->device_name, latitude, longitude, t_status, temp, errorState);
ansond 0:26c48388f725 80 }
ansond 0:26c48388f725 81 else {
ansond 0:26c48388f725 82 // create the JSON
ansond 0:26c48388f725 83 sprintf(buffer,"[{\"battery\": %d, \"city\": \"Austin\", \"country\": \"US\", \"device_id\": \"%s\", \"lat\": %4.6f, \"long\": %4.6f, \"status\": \"%s\", \"temp\": %4.1f, \"switch\": %d}]",
ansond 0:26c48388f725 84 (int)bat, this->device_name, latitude, longitude, t_status, temp, errorState);
ansond 0:26c48388f725 85 }
ansond 0:26c48388f725 86
ansond 0:26c48388f725 87 // return the JSON
ansond 0:26c48388f725 88 return buffer;
ansond 0:26c48388f725 89 }