Xively Jumpstart Demo with WiFly module

Dependencies:   C12832_lcd LM75B MMA7660 WiflyInterface libxively mbed-rtos mbed

Fork of xively-jumpstart-demo by Xively Official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "WiflyInterface.h"
00003 
00004 #define XI_FEED_ID 128488 // set Xively Feed ID (numerical, no quoutes)
00005 #define XI_API_KEY "T4KXAH_dasgw1PWBPc3fdsfsdgsdy-dUc4ND0g" // set Xively API key (double-quoted string)
00006 
00007 #include "app_board_io.h"
00008 
00009 #include "xively.h"
00010 #include "xi_err.h"
00011 
00012 #include "MMA7660.h"
00013 #include "LM75B.h"
00014 #include "C12832_lcd.h"
00015 
00016 MMA7660 axl(p28, p27);
00017 LM75B tmp(p28, p27);
00018 C12832_LCD lcd;
00019 WiflyInterface wifly(p9, p10, p30, p29, "ssid", "password", WPA);
00020 
00021 #include "logo.h"
00022 
00023 int main() {
00024     lcd_print_xively_logo();
00025     
00026     int s = wifly.init(); //Use DHCP
00027     
00028     if( s != NULL )
00029     {
00030         lcd_printf( "Could not initialise. Will halt!\n" );        
00031         exit( 0 );
00032     }    
00033         
00034     while(!wifly.connect())
00035         ;
00036     
00037     lcd_printf( "IP: %s\n", wifly.getIPAddress() );    
00038     
00039     xi_feed_t feed;
00040     memset( &feed, NULL, sizeof( xi_feed_t ) );
00041     
00042     feed.feed_id = XI_FEED_ID;
00043     feed.datastream_count = 3;
00044     
00045     feed.datastreams[0].datapoint_count = 1;
00046     xi_datastream_t* orientation_datastream = &feed.datastreams[0];
00047     strcpy( orientation_datastream->datastream_id, "orientation" );
00048     xi_datapoint_t* current_orientation = &orientation_datastream->datapoints[0];
00049 
00050     feed.datastreams[1].datapoint_count = 1;
00051     xi_datastream_t* side_rotation_datastream = &feed.datastreams[1];
00052     strcpy( side_rotation_datastream->datastream_id, "side_rotation" );
00053     xi_datapoint_t* current_side_rotation = &side_rotation_datastream->datapoints[0];
00054     
00055     feed.datastreams[2].datapoint_count = 1;
00056     xi_datastream_t* temperature_datastream = &feed.datastreams[2];
00057     strcpy( temperature_datastream->datastream_id, "temperature" );
00058     xi_datapoint_t* current_temperature = &temperature_datastream->datapoints[0];
00059     
00060     // create the cosm library context
00061     xi_context_t* xi_context
00062         = xi_create_context( XI_HTTP, XI_API_KEY, feed.feed_id );
00063 
00064     // check if everything works
00065     if( xi_context == NULL )
00066     {
00067         return -1;
00068     }
00069 
00070     while(1) {
00071       
00072       switch( axl.getSide() ) {
00073         case MMA7660::Front:
00074           xi_set_value_str( current_side_rotation, "front" );
00075           break;
00076         case MMA7660::Back:
00077           xi_set_value_str( current_side_rotation, "back" );
00078           break;
00079         default:
00080           xi_set_value_str( current_side_rotation, "unknown" );
00081           break;
00082       }
00083       
00084       switch( axl.getOrientation() ) {
00085         case MMA7660::Down:
00086           xi_set_value_str( current_orientation, "down" );
00087           break;
00088         case MMA7660::Up:
00089            xi_set_value_str( current_orientation, "up" );
00090            break;
00091         case MMA7660::Right:
00092           xi_set_value_str( current_orientation, "right" );
00093           break;
00094         case MMA7660::Left:
00095           xi_set_value_str( current_orientation, "left" );
00096           break;
00097         default: 
00098           xi_set_value_str( current_orientation, "unknown" );
00099           break;
00100       }
00101       
00102       xi_set_value_f32( current_temperature, tmp.read() );
00103         
00104       lcd_printf( "update...\n" );
00105       xi_feed_update( xi_context, &feed );
00106       lcd_printf( "done...\n" );
00107       
00108       wait( 1.0 );
00109     }
00110 }