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

Files at this revision

API Documentation at this revision

Comitter:
wren301
Date:
Mon Jun 02 19:13:07 2014 +0000
Parent:
4:05986b9ea330
Commit message:
Actually HW7 :) All connections to Xively operational as of last test

Changed in this revision

SimpleSMTPClient.lib Show diff for this revision Revisions of this file
WebSocketClient.lib Show diff for this revision Revisions of this file
app_board_io.cpp Show annotated file Show diff for this revision Revisions of this file
app_board_io.h Show annotated file Show diff for this revision Revisions of this file
libxively.lib Show annotated file Show diff for this revision Revisions of this file
logo.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
xi_user_config.h Show annotated file Show diff for this revision Revisions of this file
diff -r 05986b9ea330 -r d5023e875887 SimpleSMTPClient.lib
--- a/SimpleSMTPClient.lib	Mon May 19 15:40:00 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/sunifu/code/SimpleSMTPClient/#0847fa2294a0
diff -r 05986b9ea330 -r d5023e875887 WebSocketClient.lib
--- a/WebSocketClient.lib	Mon May 19 15:40:00 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/samux/code/WebSocketClient/#4567996414a5
diff -r 05986b9ea330 -r d5023e875887 app_board_io.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app_board_io.cpp	Mon Jun 02 19:13:07 2014 +0000
@@ -0,0 +1,19 @@
+#include "app_board_io.h"
+#include "C12832_lcd.h"
+
+extern C12832_LCD lcd;
+
+extern "C" void lcd_printf( const char* fmt, ...  )
+{
+    char buffer[ 64 ];
+    
+    va_list ap;
+    va_start( ap, fmt );
+    vsnprintf( buffer, 64, fmt, ap );
+    va_end( ap );
+    
+    lcd.cls();
+    lcd.locate( 0, 3 );
+    lcd.printf( buffer );
+    //wait( 1.0 );
+}
\ No newline at end of file
diff -r 05986b9ea330 -r d5023e875887 app_board_io.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app_board_io.h	Mon Jun 02 19:13:07 2014 +0000
@@ -0,0 +1,7 @@
+#include <stdarg.h>
+#include <stdio.h>
+
+#ifdef __cplusplus
+extern "C"
+#endif
+void lcd_printf( const char* fmt, ...  );
\ No newline at end of file
diff -r 05986b9ea330 -r d5023e875887 libxively.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libxively.lib	Mon Jun 02 19:13:07 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/xively/code/libxively/#1208875310d3
diff -r 05986b9ea330 -r d5023e875887 logo.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/logo.h	Mon Jun 02 19:13:07 2014 +0000
@@ -0,0 +1,55 @@
+static char xively_logo_data[] = {
+  0x0,   0x0,   0x0,   0x0,   0x0,   0x0,   0x0,  0x0,
+  0x0,   0x0,   0x0,   0x0,   0x0,   0x0,   0x0,  0x0,
+  0x0,   0x0,   0x0,   0x0,   0x0,   0x0,   0x1c, 0x0,
+  0x0,   0x0,   0x0,   0xe0,  0x0,   0x0,   0x0,  0x0,
+  0x3e,  0x0,   0x0,   0x0,   0x0,   0xf0,  0x0,  0x0,
+  0x0,   0x0,   0x3e,  0x0,   0x0,   0x0,   0x0,  0xf0,
+  0x0,   0x0,   0x0,   0x0,   0x1c,  0x0,   0x0,  0x0,
+  0x0,   0xf0,  0x0,   0x0,   0x0,   0x0,   0x0,  0x0,
+  0x0,   0x0,   0x0,   0xf0,  0x0,   0x0,   0x0,  0x0,
+  0x0,   0x0,   0x0,   0x0,   0x0,   0xf0,  0x0,  0x0,
+  0x10,  0x0,   0x0,   0x0,   0x0,   0xf,   0xc0, 0xf0,
+  0x0,   0x0,   0x1e,  0x7,   0x9c,  0xf0,  0x1e, 0x3f,
+  0xf0,  0xf7,  0x80,  0x78,  0xf,   0xf,   0x1c, 0xf0,
+  0x1e,  0x7f,  0xf8,  0xf7,  0x80,  0xf0,  0x7,  0x9e,
+  0x1c,  0xf0,  0x1e,  0xf8,  0x78,  0xf3,  0xc0, 0xf0,
+  0x7,   0x9e,  0x1c,  0x78,  0x3c,  0xf0,  0x3c, 0xf3,
+  0xc0,  0xe0,  0x3,   0xfc,  0x1c,  0x78,  0x3d, 0xe0,
+  0x1c,  0xf3,  0xc1,  0xe0,  0x1,   0xf8,  0x1c, 0x38,
+  0x39,  0xe0,  0x1c,  0xf1,  0xe1,  0xe0,  0x1,  0xf8,
+  0x1c,  0x3c,  0x79,  0xff,  0xfe,  0xf1,  0xe3, 0xc0,
+  0x1,   0xf8,  0x1c,  0x3c,  0x79,  0xff,  0xfe, 0xf0,
+  0xe3,  0xc0,  0x1,   0xf8,  0x1c,  0x1e,  0xf1, 0xe0,
+  0x0,   0xf0,  0xf3,  0x80,  0x3,   0xfc,  0x1c, 0x1e,
+  0xf1,  0xe0,  0x0,   0xf0,  0x77,  0x80,  0x7,  0x9e,
+  0x1c,  0xf,   0xe0,  0xf0,  0x0,   0xf0,  0x7f, 0x80,
+  0x7,   0x8f,  0x1c,  0xf,   0xe0,  0xf8,  0x10, 0xf0,
+  0x7f,  0x0,   0xf,   0xf,   0x1c,  0xf,   0xe0, 0x7f,
+  0xf8,  0xf0,  0x3f,  0x0,   0x1e,  0x7,   0x9c, 0x7,
+  0xc0,  0x3f,  0xf8,  0xf0,  0x3e,  0x0,   0x3e, 0x3,
+  0xdc,  0x7,   0xc0,  0x1f,  0xe0,  0xf0,  0x1e, 0x0,
+  0x0,   0x0,   0x0,   0x0,   0x0,   0x0,   0x0,  0x0,
+  0x1e,  0x0,   0x0,   0x0,   0x0,   0x0,   0x0,  0x0,
+  0x0,   0x0,   0x3c,  0x0,   0x0,   0x0,   0x0,  0x0,
+  0x0,   0x0,   0x0,   0x0,   0x3c,  0x0,   0x0,  0x0,
+  0x0,   0x0,   0x0,   0x0,   0x0,   0x1,   0xf8, 0x0,
+  0x0,   0x0,   0x0,   0x0,   0x0,   0x0,   0x0,  0x1,
+  0xf8,  0x0,   0x0,   0x0,   0x0,   0x0,   0x0,  0x0,
+  0x0,   0x1,   0xf0,  0x0,   0x0,   0x0,   0x0,  0x0,
+  0x0,   0x0,   0x0,   0x0,   0x0,   0x0,   0x0,  0x0,
+  0x0,   0x0,   0x0,   0x0,   0x0,   0x0,   0x0,  0x0
+};
+
+inline void lcd_print_xively_logo() {
+  static Bitmap xively_logo = { 80, 32, 10, xively_logo_data };
+  lcd.cls();
+  lcd.print_bm(xively_logo, 24, 0);
+  lcd.copy_to_lcd();  
+  for(int i = -32; i < 32; i++){ 
+    lcd.cls();
+    lcd.print_bm(xively_logo, 24, i);
+    lcd.copy_to_lcd(); 
+    wait(0.1);
+  }
+}
\ No newline at end of file
diff -r 05986b9ea330 -r d5023e875887 main.cpp
--- 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);
diff -r 05986b9ea330 -r d5023e875887 xi_user_config.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xi_user_config.h	Mon Jun 02 19:13:07 2014 +0000
@@ -0,0 +1,24 @@
+#ifndef __XI_USER_CONFIG_H__
+#define __XI_USER_CONFIG_H__
+
+// The following settings should lower memory footprint.
+// The library currently allows one to send batch datapoint
+// and feed updates, but it's not needed in most use cases
+#define XI_MAX_DATAPOINTS         1
+// The number of channels can be increased if needed
+#define XI_MAX_DATASTREAMS        5
+
+// Below are optimisations that reduce some minor functionality
+#define XI_OPT_NO_ERROR_STRINGS
+
+// If you wish to enable assertions, set this to 1
+#define XI_DEBUG_ASSERT 0
+// If you wish to disable debug output, set this to 0
+#define XI_DEBUG_OUTPUT 0
+
+// On the mbed app board we can use the LCD for debug output,
+// but one may wish to modify this and write to file instead
+#include "app_board_io.h"
+#define XI_DEBUG_PRINTF lcd_printf //TODO: use serial port...
+
+#endif /* __XI_USER_CONFIG_H__ */