this is a demo code for you to update sensor data to xively by using GPRSInterface library
Dependencies: GPRSInterface libxively mbed
Fork of Seeed_GPRS_Xively_HelloWorld by
main.cpp@2:aed4fdb82b45, 2014-04-09 (annotated)
- Committer:
- lawliet
- Date:
- Wed Apr 09 07:19:08 2014 +0000
- Revision:
- 2:aed4fdb82b45
- Parent:
- 1:4d9cf9cad247
Comment out the print message
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lawliet | 0:f6da0806cb0a | 1 | #include "mbed.h" |
lawliet | 0:f6da0806cb0a | 2 | #include "GPRSInterface.h" |
lawliet | 0:f6da0806cb0a | 3 | #include "xively.h" |
lawliet | 0:f6da0806cb0a | 4 | #include "xi_err.h" |
lawliet | 0:f6da0806cb0a | 5 | |
lawliet | 2:aed4fdb82b45 | 6 | #define PIN_TX P0_19 |
lawliet | 2:aed4fdb82b45 | 7 | #define PIN_RX P0_18 |
lawliet | 0:f6da0806cb0a | 8 | #define XI_FEED_ID 571464242 // set Xively Feed ID (numerical, no quoutes) |
lawliet | 0:f6da0806cb0a | 9 | #define XI_API_KEY "niUYuSJkjqyzPFwnWqpApm7lLNv8fInUD6ijAoRrikqKFWbg" // set Xively API key (double-quoted string) |
lawliet | 0:f6da0806cb0a | 10 | |
lawliet | 0:f6da0806cb0a | 11 | AnalogIn soundSensor(P0_11); |
lawliet | 0:f6da0806cb0a | 12 | GPRSInterface gprs(PIN_TX,PIN_RX,19200,"cmnet",NULL,NULL); |
lawliet | 0:f6da0806cb0a | 13 | |
lawliet | 1:4d9cf9cad247 | 14 | int main() |
lawliet | 1:4d9cf9cad247 | 15 | { |
lawliet | 0:f6da0806cb0a | 16 | gprs.init(); //Use DHCP |
lawliet | 0:f6da0806cb0a | 17 | // attempt DHCP |
lawliet | 1:4d9cf9cad247 | 18 | while(false == gprs.connect()) { |
lawliet | 1:4d9cf9cad247 | 19 | wait(2); |
lawliet | 2:aed4fdb82b45 | 20 | //printf("gprs connect error\n"); |
lawliet | 0:f6da0806cb0a | 21 | } |
lawliet | 0:f6da0806cb0a | 22 | // successful DHCP |
lawliet | 2:aed4fdb82b45 | 23 | //printf("IP Address is %s\n", gprs.getIPAddress()); |
lawliet | 1:4d9cf9cad247 | 24 | |
lawliet | 0:f6da0806cb0a | 25 | xi_feed_t feed; |
lawliet | 0:f6da0806cb0a | 26 | memset( &feed, NULL, sizeof( xi_feed_t ) ); |
lawliet | 1:4d9cf9cad247 | 27 | |
lawliet | 0:f6da0806cb0a | 28 | feed.feed_id = XI_FEED_ID; |
lawliet | 0:f6da0806cb0a | 29 | feed.datastream_count = 1; |
lawliet | 1:4d9cf9cad247 | 30 | |
lawliet | 0:f6da0806cb0a | 31 | feed.datastreams[0].datapoint_count = 1; |
lawliet | 0:f6da0806cb0a | 32 | xi_datastream_t* sound_datastream = &feed.datastreams[0]; |
lawliet | 0:f6da0806cb0a | 33 | strcpy( sound_datastream->datastream_id, "Sound" ); |
lawliet | 0:f6da0806cb0a | 34 | xi_datapoint_t* current_sound = &sound_datastream->datapoints[0]; |
lawliet | 0:f6da0806cb0a | 35 | |
lawliet | 0:f6da0806cb0a | 36 | // create the cosm library context |
lawliet | 0:f6da0806cb0a | 37 | xi_context_t* xi_context = xi_create_context( XI_HTTP, XI_API_KEY, feed.feed_id ); |
lawliet | 0:f6da0806cb0a | 38 | |
lawliet | 0:f6da0806cb0a | 39 | // check if everything works |
lawliet | 1:4d9cf9cad247 | 40 | if( xi_context == NULL ) { |
lawliet | 0:f6da0806cb0a | 41 | return -1; |
lawliet | 0:f6da0806cb0a | 42 | } |
lawliet | 0:f6da0806cb0a | 43 | |
lawliet | 1:4d9cf9cad247 | 44 | while(1) { |
lawliet | 1:4d9cf9cad247 | 45 | xi_set_value_f32(current_sound, soundSensor.read()); |
lawliet | 1:4d9cf9cad247 | 46 | xi_feed_update( xi_context, &feed ); |
lawliet | 2:aed4fdb82b45 | 47 | //printf("update success\n"); |
lawliet | 1:4d9cf9cad247 | 48 | wait( 10 ); |
lawliet | 0:f6da0806cb0a | 49 | } |
lawliet | 0:f6da0806cb0a | 50 | } |