Demo application of SNICInterface library for Murata TypeYD, which reports sensor data periodically to Xively cloud server . Hardware platform: mbed application board (https://mbed.org/cookbook/mbed-application-board), mbed LPC1768 (https://mbed.org/platforms/mbed-LPC1768/) and TypeYD.
Dependencies: C12832 LM75B MMA7660 SNICInterface libxively mbed-rtos mbed
main.cpp@3:7ad3f6543b6e, 2013-06-26 (annotated)
- Committer:
- xively
- Date:
- Wed Jun 26 11:45:42 2013 +0000
- Revision:
- 3:7ad3f6543b6e
- Parent:
- 1:0a61d7ab702c
- Child:
- 4:e7ca62a11595
Added:; - pause to avoid Xively API rate limits; - reading of temperature sensor available on the application board;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xively | 0:efdea27c3b81 | 1 | #include "mbed.h" |
xively | 0:efdea27c3b81 | 2 | #include "EthernetInterface.h" |
xively | 0:efdea27c3b81 | 3 | #include "xively.h" |
xively | 0:efdea27c3b81 | 4 | #include "xi_err.h" |
xively | 0:efdea27c3b81 | 5 | #include "xi_printf.h" |
xively | 0:efdea27c3b81 | 6 | |
xively | 0:efdea27c3b81 | 7 | #include <stdarg.h> |
xively | 0:efdea27c3b81 | 8 | #include <stdio.h> |
xively | 0:efdea27c3b81 | 9 | |
xively | 0:efdea27c3b81 | 10 | #include "MMA7660.h" |
xively | 3:7ad3f6543b6e | 11 | #include "LM75B.h" |
xively | 0:efdea27c3b81 | 12 | |
xively | 3:7ad3f6543b6e | 13 | //#define XI_FEED_ID 123 // set Xively Feed ID (numerical, no quoutes |
xively | 3:7ad3f6543b6e | 14 | //#define XI_API_KEY "T4KXAH_dasgw1PWBPc3fdsfsdgsdy-dUc4ND0g" // set Xively API key (double-quoted string) |
xively | 3:7ad3f6543b6e | 15 | #define XI_FEED_ID 128488 |
xively | 3:7ad3f6543b6e | 16 | #define XI_API_KEY "9YL5ZOL8pV0gLeJMKJeQPy5TaQJkz06IpgccQD2fXvjezSll" |
xively | 0:efdea27c3b81 | 17 | |
xively | 0:efdea27c3b81 | 18 | #include "C12832_lcd.h" |
xively | 0:efdea27c3b81 | 19 | |
xively | 0:efdea27c3b81 | 20 | C12832_LCD lcd; |
xively | 0:efdea27c3b81 | 21 | |
xively | 0:efdea27c3b81 | 22 | MMA7660 axl(p28, p27); |
xively | 3:7ad3f6543b6e | 23 | LM75B tmp(p28, p27); |
xively | 0:efdea27c3b81 | 24 | |
xively | 0:efdea27c3b81 | 25 | extern "C" { |
xively | 0:efdea27c3b81 | 26 | |
xively | 0:efdea27c3b81 | 27 | void user_printf( const char* buffer ) |
xively | 0:efdea27c3b81 | 28 | { |
xively | 0:efdea27c3b81 | 29 | lcd.cls(); |
xively | 0:efdea27c3b81 | 30 | lcd.locate( 0, 3 ); |
xively | 0:efdea27c3b81 | 31 | lcd.printf( buffer ); |
xively | 0:efdea27c3b81 | 32 | //wait( 1.0 ); |
xively | 0:efdea27c3b81 | 33 | } |
xively | 0:efdea27c3b81 | 34 | |
xively | 0:efdea27c3b81 | 35 | void mbed_printf( const char* fmt, ... ) |
xively | 0:efdea27c3b81 | 36 | { |
xively | 0:efdea27c3b81 | 37 | char buffer[ 64 ]; |
xively | 0:efdea27c3b81 | 38 | |
xively | 0:efdea27c3b81 | 39 | va_list ap; |
xively | 0:efdea27c3b81 | 40 | va_start( ap, fmt ); |
xively | 0:efdea27c3b81 | 41 | vsnprintf( buffer, 64, fmt, ap ); |
xively | 0:efdea27c3b81 | 42 | va_end( ap ); |
xively | 0:efdea27c3b81 | 43 | |
xively | 0:efdea27c3b81 | 44 | user_printf( buffer ); |
xively | 0:efdea27c3b81 | 45 | } |
xively | 0:efdea27c3b81 | 46 | |
xively | 0:efdea27c3b81 | 47 | } |
xively | 0:efdea27c3b81 | 48 | |
xively | 0:efdea27c3b81 | 49 | int main() { |
xively | 0:efdea27c3b81 | 50 | // set our device specific print function |
xively | 0:efdea27c3b81 | 51 | USER_PRINT = user_printf; |
xively | 0:efdea27c3b81 | 52 | |
xively | 0:efdea27c3b81 | 53 | EthernetInterface eth; |
xively | 0:efdea27c3b81 | 54 | |
xively | 0:efdea27c3b81 | 55 | int s = eth.init(); //Use DHCP |
xively | 0:efdea27c3b81 | 56 | |
xively | 0:efdea27c3b81 | 57 | if( s != NULL ) |
xively | 0:efdea27c3b81 | 58 | { |
xively | 0:efdea27c3b81 | 59 | mbed_printf( "Could not initialise. Will halt!\n" ); |
xively | 0:efdea27c3b81 | 60 | exit( 0 ); |
xively | 0:efdea27c3b81 | 61 | } |
xively | 0:efdea27c3b81 | 62 | |
xively | 0:efdea27c3b81 | 63 | s = eth.connect(); |
xively | 0:efdea27c3b81 | 64 | |
xively | 0:efdea27c3b81 | 65 | if( s != NULL ) |
xively | 0:efdea27c3b81 | 66 | { |
xively | 0:efdea27c3b81 | 67 | mbed_printf( "Could not connect. Will halt!\n" ); |
xively | 0:efdea27c3b81 | 68 | exit( 0 ); |
xively | 0:efdea27c3b81 | 69 | } |
xively | 0:efdea27c3b81 | 70 | else |
xively | 0:efdea27c3b81 | 71 | { |
xively | 0:efdea27c3b81 | 72 | mbed_printf( "IP: %s\n", eth.getIPAddress() ); |
xively | 0:efdea27c3b81 | 73 | } |
xively | 0:efdea27c3b81 | 74 | |
xively | 0:efdea27c3b81 | 75 | xi_feed_t feed; |
xively | 0:efdea27c3b81 | 76 | memset( &feed, NULL, sizeof( xi_feed_t ) ); |
xively | 0:efdea27c3b81 | 77 | |
xively | 0:efdea27c3b81 | 78 | feed.feed_id = XI_FEED_ID; |
xively | 3:7ad3f6543b6e | 79 | feed.datastream_count = 3; |
xively | 0:efdea27c3b81 | 80 | |
xively | 0:efdea27c3b81 | 81 | feed.datastreams[0].datapoint_count = 1; |
xively | 0:efdea27c3b81 | 82 | xi_datastream_t* orientation_datastream = &feed.datastreams[0]; |
xively | 0:efdea27c3b81 | 83 | strcpy( orientation_datastream->datastream_id, "orientation" ); |
xively | 0:efdea27c3b81 | 84 | xi_datapoint_t* current_orientation = &orientation_datastream->datapoints[0]; |
xively | 0:efdea27c3b81 | 85 | |
xively | 0:efdea27c3b81 | 86 | feed.datastreams[1].datapoint_count = 1; |
xively | 0:efdea27c3b81 | 87 | xi_datastream_t* side_rotation_datastream = &feed.datastreams[1]; |
xively | 0:efdea27c3b81 | 88 | strcpy( side_rotation_datastream->datastream_id, "side_rotation" ); |
xively | 0:efdea27c3b81 | 89 | xi_datapoint_t* current_side_rotation = &side_rotation_datastream->datapoints[0]; |
xively | 0:efdea27c3b81 | 90 | |
xively | 3:7ad3f6543b6e | 91 | feed.datastreams[2].datapoint_count = 1; |
xively | 3:7ad3f6543b6e | 92 | xi_datastream_t* temperature_datastream = &feed.datastreams[2]; |
xively | 3:7ad3f6543b6e | 93 | strcpy( temperature_datastream->datastream_id, "temperature" ); |
xively | 3:7ad3f6543b6e | 94 | xi_datapoint_t* current_temperature = &temperature_datastream->datapoints[0]; |
xively | 3:7ad3f6543b6e | 95 | |
xively | 0:efdea27c3b81 | 96 | // create the cosm library context |
xively | 0:efdea27c3b81 | 97 | xi_context_t* xi_context |
xively | 0:efdea27c3b81 | 98 | = xi_create_context( XI_HTTP, XI_API_KEY, feed.feed_id ); |
xively | 0:efdea27c3b81 | 99 | |
xively | 0:efdea27c3b81 | 100 | // check if everything works |
xively | 0:efdea27c3b81 | 101 | if( xi_context == NULL ) |
xively | 0:efdea27c3b81 | 102 | { |
xively | 0:efdea27c3b81 | 103 | return -1; |
xively | 0:efdea27c3b81 | 104 | } |
xively | 0:efdea27c3b81 | 105 | |
xively | 0:efdea27c3b81 | 106 | mbed_printf("feed:%d datastreams:[%s,%s]\n", feed.feed_id, |
xively | 0:efdea27c3b81 | 107 | orientation_datastream->datastream_id, |
xively | 0:efdea27c3b81 | 108 | side_rotation_datastream->datastream_id); |
xively | 0:efdea27c3b81 | 109 | |
xively | 0:efdea27c3b81 | 110 | while(1) { |
xively | 0:efdea27c3b81 | 111 | |
xively | 0:efdea27c3b81 | 112 | switch( axl.getSide() ) { |
xively | 0:efdea27c3b81 | 113 | case MMA7660::Front: |
xively | 0:efdea27c3b81 | 114 | xi_set_value_str( current_side_rotation, "front" ); |
xively | 0:efdea27c3b81 | 115 | break; |
xively | 0:efdea27c3b81 | 116 | case MMA7660::Back: |
xively | 0:efdea27c3b81 | 117 | xi_set_value_str( current_side_rotation, "back" ); |
xively | 0:efdea27c3b81 | 118 | break; |
xively | 0:efdea27c3b81 | 119 | default: |
xively | 0:efdea27c3b81 | 120 | xi_set_value_str( current_side_rotation, "unknown" ); |
xively | 0:efdea27c3b81 | 121 | break; |
xively | 0:efdea27c3b81 | 122 | } |
xively | 0:efdea27c3b81 | 123 | |
xively | 0:efdea27c3b81 | 124 | switch( axl.getOrientation() ) { |
xively | 0:efdea27c3b81 | 125 | case MMA7660::Down: |
xively | 0:efdea27c3b81 | 126 | mbed_printf("down %s\n", |
xively | 0:efdea27c3b81 | 127 | (axl.getSide() == MMA7660::Front ? "front" : "back")); |
xively | 0:efdea27c3b81 | 128 | xi_set_value_str( current_orientation, "down" ); |
xively | 0:efdea27c3b81 | 129 | break; |
xively | 0:efdea27c3b81 | 130 | case MMA7660::Up: |
xively | 0:efdea27c3b81 | 131 | mbed_printf("up %s\n", |
xively | 0:efdea27c3b81 | 132 | (axl.getSide() == MMA7660::Front ? "front" : "back")); |
xively | 0:efdea27c3b81 | 133 | xi_set_value_str( current_orientation, "up" ); |
xively | 0:efdea27c3b81 | 134 | break; |
xively | 0:efdea27c3b81 | 135 | case MMA7660::Right: |
xively | 0:efdea27c3b81 | 136 | mbed_printf("right %s\n", |
xively | 0:efdea27c3b81 | 137 | (axl.getSide() == MMA7660::Front ? "front" : "back")); |
xively | 0:efdea27c3b81 | 138 | xi_set_value_str( current_orientation, "right" ); |
xively | 0:efdea27c3b81 | 139 | break; |
xively | 0:efdea27c3b81 | 140 | case MMA7660::Left: |
xively | 0:efdea27c3b81 | 141 | mbed_printf("left %s\n", |
xively | 0:efdea27c3b81 | 142 | (axl.getSide() == MMA7660::Front ? "front" : "back")); |
xively | 0:efdea27c3b81 | 143 | xi_set_value_str( current_orientation, "left" ); |
xively | 0:efdea27c3b81 | 144 | break; |
xively | 0:efdea27c3b81 | 145 | default: |
xively | 0:efdea27c3b81 | 146 | xi_set_value_str( current_orientation, "unknown" ); |
xively | 0:efdea27c3b81 | 147 | break; |
xively | 0:efdea27c3b81 | 148 | } |
xively | 3:7ad3f6543b6e | 149 | |
xively | 3:7ad3f6543b6e | 150 | xi_set_value_f32( current_temperature, tmp.read() ); |
xively | 3:7ad3f6543b6e | 151 | |
xively | 0:efdea27c3b81 | 152 | mbed_printf( "update...\n" ); |
xively | 0:efdea27c3b81 | 153 | xi_feed_update(xi_context, &feed); |
xively | 0:efdea27c3b81 | 154 | mbed_printf( "done...\n" ); |
xively | 3:7ad3f6543b6e | 155 | |
xively | 3:7ad3f6543b6e | 156 | wait( 15.0 ); |
xively | 0:efdea27c3b81 | 157 | } |
xively | 0:efdea27c3b81 | 158 | } |