Connections to Xively working; has 5 channels on Xively (axl_x, axl_y, axl_z, heater_status, temperature)
Dependencies: C12832_lcd EthernetInterface LM75B MMA7660 NTPClient libxively mbed-rtos mbed
Diff: main.cpp
- Revision:
- 6:d5023e875887
- Parent:
- 4:05986b9ea330
--- a/main.cpp Mon May 19 15:40:00 2014 +0000 +++ b/main.cpp Mon Jun 02 19:13:07 2014 +0000 @@ -4,7 +4,13 @@ #include "C12832_lcd.h" #include "LM75B.h" #include "MMA7660.h" -#include "Websocket.h" +#define XI_FEED_ID 1000476735 // set Xively Feed ID (numerical, no quoutes) +#define XI_API_KEY "ROxvAOZ6RznZjFPGufMNNn1LiMbYseCgEwF0qn1WAmcZPPY2" // set Xively API key (double-quoted string) + +#include "app_board_io.h" + +#include "xively.h" +#include "xi_err.h" #define PST_OFFSET 7*60*60 @@ -22,9 +28,58 @@ #define LOOP_DELAY_MS 100 //update time every 10 minutes #define UPDATE_TIME 60*1 -MMA7660 acc(p28, p27);// accelerometer +#define UPDATE_XIVELY 60*1 +MMA7660 axl(p28, p27);// accelerometer int updateTimeFromServer = 1; +//Xively globals +int update_xively = 1; +xi_feed_t feed; +xi_datapoint_t* current_temperature; +xi_datapoint_t* current_x; +xi_datapoint_t* current_y; +xi_datapoint_t* current_z; +xi_datapoint_t* current_heaterstatus; +xi_datastream_t* heaterstatus_datastream; +xi_context_t* xi_context; + + +void setUpXively() { + memset( &feed, NULL, sizeof( xi_feed_t ) ); + + feed.feed_id = XI_FEED_ID; + feed.datastream_count = 5; + + feed.datastreams[0].datapoint_count = 1; + xi_datastream_t* temperature_datastream = &feed.datastreams[0]; + strcpy( temperature_datastream->datastream_id, "temperature" ); + current_temperature = &temperature_datastream->datapoints[0]; + + feed.datastreams[1].datapoint_count = 1; + xi_datastream_t* axl_x_datastream = &feed.datastreams[1]; + strcpy( axl_x_datastream->datastream_id, "axl_x" ); + current_x = &axl_x_datastream->datapoints[0]; + + feed.datastreams[2].datapoint_count = 1; + xi_datastream_t* axl_y_datastream = &feed.datastreams[2]; + strcpy( axl_y_datastream->datastream_id, "axl_y" ); + current_y = &axl_y_datastream->datapoints[0]; + + feed.datastreams[3].datapoint_count = 1; + xi_datastream_t* axl_z_datastream = &feed.datastreams[3]; + strcpy( axl_z_datastream->datastream_id, "axl_z" ); + current_z = &axl_z_datastream->datapoints[0]; + + feed.datastreams[4].datapoint_count = 1; + heaterstatus_datastream = &feed.datastreams[4]; + strcpy( heaterstatus_datastream->datastream_id, "heater_status" ); + current_heaterstatus = &heaterstatus_datastream->datapoints[0]; + + // create the cosm library context + xi_context = xi_create_context( XI_HTTP, XI_API_KEY, feed.feed_id ); + +} + void connectToTheInternet() { eth.init(); //Init and use DHCP @@ -60,17 +115,27 @@ updateTimeFromServer = 1; } -void sendTempJSON() -{ - char json_str[100]; - // See the output on http://sockets.mbed.org/app-board/viewer - Websocket ws("ws://sockets.mbed.org:443/ws/app-board/wo"); - ws.connect(); - // create json string with acc/tmp data - sprintf(json_str, "{\"id\":\"app_board_eth_EW2013\",\"ax\":%d,\"ay\":%d,\"az\":%d, \"tmp\":%d}", (int)(acc.x()*360), (int)(acc.y()*360), (int)(acc.z()*360), (int)tmp.read()); - // send str - ws.send(json_str); - ws.close(); +void updateXively() { + update_xively = 1; +} + +void updateXivelyRoutine() { + xi_set_value_f32( current_temperature, tmp.read() ); + xi_set_value_f32( current_x, axl.x() ); + xi_set_value_f32( current_y, axl.y() ); + xi_set_value_f32( current_z, axl.z() ); + update_xively = 0; + printf( "update...\n" ); + xi_feed_update( xi_context, &feed ); + + //xi_response_t myInData; + //myInData = xi_feed_get(xi_context, &feed); + xi_datastream_get(xi_context, feed.feed_id, heaterstatus_datastream->datastream_id, heaterstatus_datastream->datapoints); + printf( "\n\rHEATER STATUS: %d...\n",heaterstatus_datastream->datapoints[0]); + current_heaterstatus = &heaterstatus_datastream->datapoints[0]; + int heatervalue = current_heaterstatus->value.i32_value; + printf("HeaterStatus: %d", heatervalue); + printf( "done...\n" ); } //POT values are 0:1, will allow high and low temperatures @@ -99,6 +164,7 @@ int main() { connectToTheInternet(); + setUpXively(); //Variable to hold the current minute so we only update the display when the minute changes char currentMinute[2]; currentMinute[1] = 'a'; @@ -111,12 +177,16 @@ printf("Set time successfully\r\n"); lcd.cls(); timer.attach(&updateTime, UPDATE_TIME); + timer.attach(&updateXively, UPDATE_XIVELY); lcd.printf("\n\r\n\rHEATER OFF"); while(1) { if(updateTimeFromServer) { updateTimeRoutine(); } + if(update_xively){ + updateXivelyRoutine(); + } //Sets temp from POTs updateTempsFromPots(); //Fetch the time @@ -145,7 +215,6 @@ lcd.locate(0,0); //updates the temperature line of the display lcd.printf("\n\r%.1f LOW: %.0f HIGH: %.0f", temp, lowTemp, highTemp); - sendTempJSON(); } lcd.locate(0,0);