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@0:f6da0806cb0a, 2014-02-28 (annotated)
- Committer:
- lawliet
- Date:
- Fri Feb 28 02:38:26 2014 +0000
- Revision:
- 0:f6da0806cb0a
- Child:
- 1:4d9cf9cad247
Initial Version of Seeed_GPRS_Xively_HelloWorld
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 | 0:f6da0806cb0a | 6 | #define PIN_TX P0_0 |
lawliet | 0:f6da0806cb0a | 7 | #define PIN_RX P0_1 |
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 | 0:f6da0806cb0a | 14 | int main() { |
lawliet | 0:f6da0806cb0a | 15 | gprs.init(); //Use DHCP |
lawliet | 0:f6da0806cb0a | 16 | // attempt DHCP |
lawliet | 0:f6da0806cb0a | 17 | while(false == gprs.connect()){ |
lawliet | 0:f6da0806cb0a | 18 | wait(2); |
lawliet | 0:f6da0806cb0a | 19 | printf("gprs connect error\n"); |
lawliet | 0:f6da0806cb0a | 20 | } |
lawliet | 0:f6da0806cb0a | 21 | // successful DHCP |
lawliet | 0:f6da0806cb0a | 22 | printf("IP Address is %s\n", gprs.getIPAddress()); |
lawliet | 0:f6da0806cb0a | 23 | |
lawliet | 0:f6da0806cb0a | 24 | xi_feed_t feed; |
lawliet | 0:f6da0806cb0a | 25 | memset( &feed, NULL, sizeof( xi_feed_t ) ); |
lawliet | 0:f6da0806cb0a | 26 | |
lawliet | 0:f6da0806cb0a | 27 | feed.feed_id = XI_FEED_ID; |
lawliet | 0:f6da0806cb0a | 28 | feed.datastream_count = 1; |
lawliet | 0:f6da0806cb0a | 29 | |
lawliet | 0:f6da0806cb0a | 30 | feed.datastreams[0].datapoint_count = 1; |
lawliet | 0:f6da0806cb0a | 31 | xi_datastream_t* sound_datastream = &feed.datastreams[0]; |
lawliet | 0:f6da0806cb0a | 32 | strcpy( sound_datastream->datastream_id, "Sound" ); |
lawliet | 0:f6da0806cb0a | 33 | xi_datapoint_t* current_sound = &sound_datastream->datapoints[0]; |
lawliet | 0:f6da0806cb0a | 34 | |
lawliet | 0:f6da0806cb0a | 35 | // create the cosm library context |
lawliet | 0:f6da0806cb0a | 36 | xi_context_t* xi_context = xi_create_context( XI_HTTP, XI_API_KEY, feed.feed_id ); |
lawliet | 0:f6da0806cb0a | 37 | |
lawliet | 0:f6da0806cb0a | 38 | // check if everything works |
lawliet | 0:f6da0806cb0a | 39 | if( xi_context == NULL ){ |
lawliet | 0:f6da0806cb0a | 40 | return -1; |
lawliet | 0:f6da0806cb0a | 41 | } |
lawliet | 0:f6da0806cb0a | 42 | |
lawliet | 0:f6da0806cb0a | 43 | while(1) { |
lawliet | 0:f6da0806cb0a | 44 | xi_set_value_f32(current_sound, soundSensor.read()); |
lawliet | 0:f6da0806cb0a | 45 | xi_feed_update( xi_context, &feed ); |
lawliet | 0:f6da0806cb0a | 46 | printf("update success\n"); |
lawliet | 0:f6da0806cb0a | 47 | wait( 10 ); |
lawliet | 0:f6da0806cb0a | 48 | } |
lawliet | 0:f6da0806cb0a | 49 | } |