Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: C12832_lcd EthernetInterface LM75B MMA7660 mbed-libxively-6eca970 mbed-rtos mbed
Fork of Application-xively-jumpstart-demo by
main.cpp@7:382c67265a5d, 2014-05-20 (annotated)
- Committer:
 - neelmani
 - Date:
 - Tue May 20 04:22:51 2014 +0000
 - Revision:
 - 7:382c67265a5d
 - Parent:
 - 6:9af362ececc4
 
led get
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| avnisha | 6:9af362ececc4 | 1 | #define ETHERNET | 
| avnisha | 6:9af362ececc4 | 2 | |
| avnisha | 6:9af362ececc4 | 3 | #ifdef WIFLY | 
| avnisha | 6:9af362ececc4 | 4 | #include "mbed.h" | 
| avnisha | 6:9af362ececc4 | 5 | #include "WiflyInterface.h" | 
| avnisha | 6:9af362ececc4 | 6 | |
| avnisha | 6:9af362ececc4 | 7 | Serial pc(USBTX, USBRX); | 
| avnisha | 6:9af362ececc4 | 8 | |
| avnisha | 6:9af362ececc4 | 9 | /* wifly object where: | 
| avnisha | 6:9af362ececc4 | 10 | * - p9 and p10 are for the serial communication | 
| avnisha | 6:9af362ececc4 | 11 | * - p25 is for the reset pin | 
| avnisha | 6:9af362ececc4 | 12 | * - p26 is for the connection status | 
| avnisha | 6:9af362ececc4 | 13 | * - "mbed" is the ssid of the network | 
| avnisha | 6:9af362ececc4 | 14 | * - "password" is the password | 
| avnisha | 6:9af362ececc4 | 15 | * - WPA is the security | 
| avnisha | 6:9af362ececc4 | 16 | */ | 
| avnisha | 6:9af362ececc4 | 17 | WiflyInterface wifly(p9, p10, p25, p26, "bubbles", "", NONE); | 
| avnisha | 6:9af362ececc4 | 18 | |
| avnisha | 6:9af362ececc4 | 19 | int main() { | 
| avnisha | 6:9af362ececc4 | 20 | printf("hello\n\r"); | 
| avnisha | 6:9af362ececc4 | 21 | wifly.init(); // use DHCP | 
| avnisha | 6:9af362ececc4 | 22 | while (!wifly.connect()); // join the network | 
| avnisha | 6:9af362ececc4 | 23 | printf("IP Address is %s\n\r", wifly.getIPAddress()); | 
| avnisha | 6:9af362ececc4 | 24 | while (1) {} | 
| avnisha | 6:9af362ececc4 | 25 | wifly.disconnect(); | 
| avnisha | 6:9af362ececc4 | 26 | } | 
| avnisha | 6:9af362ececc4 | 27 | |
| avnisha | 6:9af362ececc4 | 28 | #endif | 
| avnisha | 6:9af362ececc4 | 29 | |
| xively | 0:efdea27c3b81 | 30 | #include "mbed.h" | 
| xively | 0:efdea27c3b81 | 31 | #include "EthernetInterface.h" | 
| xively | 0:efdea27c3b81 | 32 | #include "xively.h" | 
| xively | 0:efdea27c3b81 | 33 | #include "xi_err.h" | 
| xively | 0:efdea27c3b81 | 34 | #include "xi_printf.h" | 
| xively | 0:efdea27c3b81 | 35 | |
| avnisha | 6:9af362ececc4 | 36 | #ifdef WIFLY | 
| avnisha | 6:9af362ececc4 | 37 | #include "WiflyInterface.h" | 
| avnisha | 6:9af362ececc4 | 38 | |
| avnisha | 6:9af362ececc4 | 39 | Serial pc(USBTX, USBRX); | 
| avnisha | 6:9af362ececc4 | 40 | |
| avnisha | 6:9af362ececc4 | 41 | /* wifly object where: | 
| avnisha | 6:9af362ececc4 | 42 | * - p9 and p10 are for the serial communication | 
| avnisha | 6:9af362ececc4 | 43 | * - p25 is for the reset pin | 
| avnisha | 6:9af362ececc4 | 44 | * - p26 is for the connection status | 
| avnisha | 6:9af362ececc4 | 45 | * - "mbed" is the ssid of the network | 
| avnisha | 6:9af362ececc4 | 46 | * - "password" is the password | 
| avnisha | 6:9af362ececc4 | 47 | * - WPA is the security | 
| avnisha | 6:9af362ececc4 | 48 | */ | 
| avnisha | 6:9af362ececc4 | 49 | WiflyInterface wifly(p9, p10, p25, p26, "bubbles", "", NONE); | 
| avnisha | 6:9af362ececc4 | 50 | #endif | 
| avnisha | 6:9af362ececc4 | 51 | |
| xively | 0:efdea27c3b81 | 52 | #include <stdarg.h> | 
| xively | 0:efdea27c3b81 | 53 | #include <stdio.h> | 
| xively | 0:efdea27c3b81 | 54 | |
| xively | 0:efdea27c3b81 | 55 | #include "MMA7660.h" | 
| xively | 3:7ad3f6543b6e | 56 | #include "LM75B.h" | 
| xively | 0:efdea27c3b81 | 57 | |
| avnisha | 6:9af362ececc4 | 58 | // 332668647 | 
| avnisha | 6:9af362ececc4 | 59 | // ZmCG3GmfnJzvt1LZnLdHfS9D5vlfNrXQSxKD1jpzqsAsp3bh | 
| neelmani | 7:382c67265a5d | 60 | //#define XI_FEED_ID 332668647 // set Xively Feed ID (numerical, no quoutes | 
| neelmani | 7:382c67265a5d | 61 | #define XI_FEED_ID 1075535002 | 
| neelmani | 7:382c67265a5d | 62 | //#define XI_API_KEY "ZmCG3GmfnJzvt1LZnLdHfS9D5vlfNrXQSxKD1jpzqsAsp3bh" // set Xively API key (double-quoted string) | 
| neelmani | 7:382c67265a5d | 63 | #define XI_API_KEY "lS4FGex4YTyjAOFLoXAUrChwM8vAbux0SqSblk8KIdQclYp8" | 
| xively | 0:efdea27c3b81 | 64 | |
| xively | 0:efdea27c3b81 | 65 | #include "C12832_lcd.h" | 
| xively | 0:efdea27c3b81 | 66 | |
| xively | 0:efdea27c3b81 | 67 | C12832_LCD lcd; | 
| xively | 0:efdea27c3b81 | 68 | |
| xively | 0:efdea27c3b81 | 69 | MMA7660 axl(p28, p27); | 
| xively | 3:7ad3f6543b6e | 70 | LM75B tmp(p28, p27); | 
| neelmani | 7:382c67265a5d | 71 | AnalogIn pot1(p19); | 
| xively | 0:efdea27c3b81 | 72 | |
| xively | 0:efdea27c3b81 | 73 | extern "C" { | 
| xively | 0:efdea27c3b81 | 74 | |
| xively | 0:efdea27c3b81 | 75 | void user_printf( const char* buffer ) | 
| xively | 0:efdea27c3b81 | 76 | { | 
| xively | 0:efdea27c3b81 | 77 | lcd.cls(); | 
| xively | 0:efdea27c3b81 | 78 | lcd.locate( 0, 3 ); | 
| xively | 0:efdea27c3b81 | 79 | lcd.printf( buffer ); | 
| xively | 0:efdea27c3b81 | 80 | //wait( 1.0 ); | 
| xively | 0:efdea27c3b81 | 81 | } | 
| xively | 0:efdea27c3b81 | 82 | |
| xively | 0:efdea27c3b81 | 83 | void mbed_printf( const char* fmt, ... ) | 
| xively | 0:efdea27c3b81 | 84 | { | 
| xively | 0:efdea27c3b81 | 85 | char buffer[ 64 ]; | 
| xively | 0:efdea27c3b81 | 86 | |
| xively | 0:efdea27c3b81 | 87 | va_list ap; | 
| xively | 0:efdea27c3b81 | 88 | va_start( ap, fmt ); | 
| xively | 0:efdea27c3b81 | 89 | vsnprintf( buffer, 64, fmt, ap ); | 
| xively | 0:efdea27c3b81 | 90 | va_end( ap ); | 
| xively | 0:efdea27c3b81 | 91 | |
| xively | 0:efdea27c3b81 | 92 | user_printf( buffer ); | 
| xively | 0:efdea27c3b81 | 93 | } | 
| xively | 0:efdea27c3b81 | 94 | |
| xively | 0:efdea27c3b81 | 95 | } | 
| xively | 0:efdea27c3b81 | 96 | |
| xively | 0:efdea27c3b81 | 97 | int main() { | 
| xively | 0:efdea27c3b81 | 98 | // set our device specific print function | 
| xively | 0:efdea27c3b81 | 99 | USER_PRINT = user_printf; | 
| avnisha | 6:9af362ececc4 | 100 | |
| avnisha | 6:9af362ececc4 | 101 | #ifdef ETHERNET | 
| xively | 0:efdea27c3b81 | 102 | EthernetInterface eth; | 
| xively | 0:efdea27c3b81 | 103 | |
| xively | 0:efdea27c3b81 | 104 | int s = eth.init(); //Use DHCP | 
| xively | 0:efdea27c3b81 | 105 | |
| xively | 0:efdea27c3b81 | 106 | if( s != NULL ) | 
| xively | 0:efdea27c3b81 | 107 | { | 
| xively | 0:efdea27c3b81 | 108 | mbed_printf( "Could not initialise. Will halt!\n" ); | 
| xively | 0:efdea27c3b81 | 109 | exit( 0 ); | 
| xively | 0:efdea27c3b81 | 110 | } | 
| xively | 0:efdea27c3b81 | 111 | |
| xively | 0:efdea27c3b81 | 112 | s = eth.connect(); | 
| xively | 0:efdea27c3b81 | 113 | |
| xively | 0:efdea27c3b81 | 114 | if( s != NULL ) | 
| xively | 0:efdea27c3b81 | 115 | { | 
| xively | 0:efdea27c3b81 | 116 | mbed_printf( "Could not connect. Will halt!\n" ); | 
| xively | 0:efdea27c3b81 | 117 | exit( 0 ); | 
| xively | 0:efdea27c3b81 | 118 | } | 
| xively | 0:efdea27c3b81 | 119 | else | 
| xively | 0:efdea27c3b81 | 120 | { | 
| xively | 0:efdea27c3b81 | 121 | mbed_printf( "IP: %s\n", eth.getIPAddress() ); | 
| xively | 0:efdea27c3b81 | 122 | } | 
| avnisha | 6:9af362ececc4 | 123 | #else | 
| avnisha | 6:9af362ececc4 | 124 | wifly.init(); // use DHCP | 
| avnisha | 6:9af362ececc4 | 125 | while (!wifly.connect()); // join the network | 
| avnisha | 6:9af362ececc4 | 126 | printf("IP Address is %s\n\r", wifly.getIPAddress()); | 
| avnisha | 6:9af362ececc4 | 127 | #endif | 
| avnisha | 6:9af362ececc4 | 128 | |
| xively | 0:efdea27c3b81 | 129 | xi_feed_t feed; | 
| xively | 0:efdea27c3b81 | 130 | memset( &feed, NULL, sizeof( xi_feed_t ) ); | 
| xively | 0:efdea27c3b81 | 131 | |
| xively | 0:efdea27c3b81 | 132 | feed.feed_id = XI_FEED_ID; | 
| neelmani | 7:382c67265a5d | 133 | feed.datastream_count = 5; | 
| xively | 0:efdea27c3b81 | 134 | |
| xively | 0:efdea27c3b81 | 135 | feed.datastreams[0].datapoint_count = 1; | 
| xively | 0:efdea27c3b81 | 136 | xi_datastream_t* orientation_datastream = &feed.datastreams[0]; | 
| xively | 0:efdea27c3b81 | 137 | strcpy( orientation_datastream->datastream_id, "orientation" ); | 
| xively | 0:efdea27c3b81 | 138 | xi_datapoint_t* current_orientation = &orientation_datastream->datapoints[0]; | 
| xively | 0:efdea27c3b81 | 139 | |
| xively | 0:efdea27c3b81 | 140 | feed.datastreams[1].datapoint_count = 1; | 
| xively | 0:efdea27c3b81 | 141 | xi_datastream_t* side_rotation_datastream = &feed.datastreams[1]; | 
| xively | 0:efdea27c3b81 | 142 | strcpy( side_rotation_datastream->datastream_id, "side_rotation" ); | 
| xively | 0:efdea27c3b81 | 143 | xi_datapoint_t* current_side_rotation = &side_rotation_datastream->datapoints[0]; | 
| xively | 0:efdea27c3b81 | 144 | |
| xively | 3:7ad3f6543b6e | 145 | feed.datastreams[2].datapoint_count = 1; | 
| xively | 3:7ad3f6543b6e | 146 | xi_datastream_t* temperature_datastream = &feed.datastreams[2]; | 
| xively | 3:7ad3f6543b6e | 147 | strcpy( temperature_datastream->datastream_id, "temperature" ); | 
| xively | 3:7ad3f6543b6e | 148 | xi_datapoint_t* current_temperature = &temperature_datastream->datapoints[0]; | 
| xively | 3:7ad3f6543b6e | 149 | |
| neelmani | 7:382c67265a5d | 150 | feed.datastreams[3].datapoint_count = 1; | 
| neelmani | 7:382c67265a5d | 151 | xi_datastream_t* pot1_datastream = &feed.datastreams[3]; | 
| neelmani | 7:382c67265a5d | 152 | strcpy( pot1_datastream->datastream_id, "pot1" ); | 
| neelmani | 7:382c67265a5d | 153 | xi_datapoint_t* current_pot1 = &pot1_datastream->datapoints[0]; | 
| neelmani | 7:382c67265a5d | 154 | |
| neelmani | 7:382c67265a5d | 155 | feed.datastreams[4].datapoint_count = 1; | 
| neelmani | 7:382c67265a5d | 156 | xi_datastream_t* led_datastream = &feed.datastreams[3]; | 
| neelmani | 7:382c67265a5d | 157 | strcpy( led_datastream->datastream_id, "led" ); | 
| neelmani | 7:382c67265a5d | 158 | xi_datapoint_t* current_led = &led_datastream->datapoints[0]; | 
| neelmani | 7:382c67265a5d | 159 | |
| xively | 0:efdea27c3b81 | 160 | // create the cosm library context | 
| xively | 0:efdea27c3b81 | 161 | xi_context_t* xi_context | 
| xively | 0:efdea27c3b81 | 162 | = xi_create_context( XI_HTTP, XI_API_KEY, feed.feed_id ); | 
| xively | 0:efdea27c3b81 | 163 | |
| xively | 0:efdea27c3b81 | 164 | // check if everything works | 
| xively | 0:efdea27c3b81 | 165 | if( xi_context == NULL ) | 
| xively | 0:efdea27c3b81 | 166 | { | 
| xively | 0:efdea27c3b81 | 167 | return -1; | 
| xively | 0:efdea27c3b81 | 168 | } | 
| xively | 0:efdea27c3b81 | 169 | |
| xively | 0:efdea27c3b81 | 170 | mbed_printf("feed:%d datastreams:[%s,%s]\n", feed.feed_id, | 
| xively | 0:efdea27c3b81 | 171 | orientation_datastream->datastream_id, | 
| neelmani | 7:382c67265a5d | 172 | side_rotation_datastream->datastream_id,led_datastream->datastream_id); | 
| xively | 0:efdea27c3b81 | 173 | |
| xively | 0:efdea27c3b81 | 174 | while(1) { | 
| xively | 0:efdea27c3b81 | 175 | |
| xively | 0:efdea27c3b81 | 176 | switch( axl.getSide() ) { | 
| xively | 0:efdea27c3b81 | 177 | case MMA7660::Front: | 
| xively | 0:efdea27c3b81 | 178 | xi_set_value_str( current_side_rotation, "front" ); | 
| xively | 0:efdea27c3b81 | 179 | break; | 
| xively | 0:efdea27c3b81 | 180 | case MMA7660::Back: | 
| xively | 0:efdea27c3b81 | 181 | xi_set_value_str( current_side_rotation, "back" ); | 
| xively | 0:efdea27c3b81 | 182 | break; | 
| xively | 0:efdea27c3b81 | 183 | default: | 
| xively | 0:efdea27c3b81 | 184 | xi_set_value_str( current_side_rotation, "unknown" ); | 
| xively | 0:efdea27c3b81 | 185 | break; | 
| xively | 0:efdea27c3b81 | 186 | } | 
| xively | 0:efdea27c3b81 | 187 | |
| xively | 0:efdea27c3b81 | 188 | switch( axl.getOrientation() ) { | 
| xively | 0:efdea27c3b81 | 189 | case MMA7660::Down: | 
| xively | 0:efdea27c3b81 | 190 | mbed_printf("down %s\n", | 
| xively | 0:efdea27c3b81 | 191 | (axl.getSide() == MMA7660::Front ? "front" : "back")); | 
| xively | 0:efdea27c3b81 | 192 | xi_set_value_str( current_orientation, "down" ); | 
| xively | 0:efdea27c3b81 | 193 | break; | 
| xively | 0:efdea27c3b81 | 194 | case MMA7660::Up: | 
| xively | 0:efdea27c3b81 | 195 | mbed_printf("up %s\n", | 
| xively | 0:efdea27c3b81 | 196 | (axl.getSide() == MMA7660::Front ? "front" : "back")); | 
| xively | 0:efdea27c3b81 | 197 | xi_set_value_str( current_orientation, "up" ); | 
| xively | 0:efdea27c3b81 | 198 | break; | 
| xively | 0:efdea27c3b81 | 199 | case MMA7660::Right: | 
| xively | 0:efdea27c3b81 | 200 | mbed_printf("right %s\n", | 
| xively | 0:efdea27c3b81 | 201 | (axl.getSide() == MMA7660::Front ? "front" : "back")); | 
| xively | 0:efdea27c3b81 | 202 | xi_set_value_str( current_orientation, "right" ); | 
| xively | 0:efdea27c3b81 | 203 | break; | 
| xively | 0:efdea27c3b81 | 204 | case MMA7660::Left: | 
| xively | 0:efdea27c3b81 | 205 | mbed_printf("left %s\n", | 
| xively | 0:efdea27c3b81 | 206 | (axl.getSide() == MMA7660::Front ? "front" : "back")); | 
| xively | 0:efdea27c3b81 | 207 | xi_set_value_str( current_orientation, "left" ); | 
| xively | 0:efdea27c3b81 | 208 | break; | 
| xively | 0:efdea27c3b81 | 209 | default: | 
| xively | 0:efdea27c3b81 | 210 | xi_set_value_str( current_orientation, "unknown" ); | 
| xively | 0:efdea27c3b81 | 211 | break; | 
| xively | 0:efdea27c3b81 | 212 | } | 
| xively | 3:7ad3f6543b6e | 213 | |
| xively | 3:7ad3f6543b6e | 214 | xi_set_value_f32( current_temperature, tmp.read() ); | 
| neelmani | 7:382c67265a5d | 215 | |
| neelmani | 7:382c67265a5d | 216 | xi_set_value_f32( current_pot1, pot1.read() ); | 
| neelmani | 7:382c67265a5d | 217 | |
| neelmani | 7:382c67265a5d | 218 | // read remote value | 
| neelmani | 7:382c67265a5d | 219 | xi_datastream_get( xi_context, feed.feed_id | 
| neelmani | 7:382c67265a5d | 220 | , led_datastream->datastream_id | 
| neelmani | 7:382c67265a5d | 221 | , led_datastream->datapoints); | 
| neelmani | 7:382c67265a5d | 222 | |
| neelmani | 7:382c67265a5d | 223 | current_led = &led_datastream->datapoints[0]; | 
| neelmani | 7:382c67265a5d | 224 | |
| neelmani | 7:382c67265a5d | 225 | int led_value = current_led->value.i32_value; | 
| neelmani | 7:382c67265a5d | 226 | |
| neelmani | 7:382c67265a5d | 227 | mbed_printf("%d",led_value); | 
| neelmani | 7:382c67265a5d | 228 | |
| neelmani | 7:382c67265a5d | 229 | wait(30); | 
| neelmani | 7:382c67265a5d | 230 | |
| neelmani | 7:382c67265a5d | 231 | //led_datastream->datapoints[0] | 
| neelmani | 7:382c67265a5d | 232 | |
| neelmani | 7:382c67265a5d | 233 | //xi_get_value_f32( current_led); | 
| neelmani | 7:382c67265a5d | 234 | |
| neelmani | 7:382c67265a5d | 235 | //const xi_response_t* response = xi_feed_get(xi_context,&feed); | 
| neelmani | 7:382c67265a5d | 236 | |
| neelmani | 7:382c67265a5d | 237 | //char a = (char)response.http.http_content[0]; | 
| neelmani | 7:382c67265a5d | 238 | |
| neelmani | 7:382c67265a5d | 239 | //mbed_printf("%c",a); | 
| neelmani | 7:382c67265a5d | 240 | |
| neelmani | 7:382c67265a5d | 241 | |
| neelmani | 7:382c67265a5d | 242 | //mbed_printf( "%f \n",(float)xi_feed_get( current_led) ); | 
| xively | 4:e7ca62a11595 | 243 | |
| xively | 0:efdea27c3b81 | 244 | mbed_printf( "update...\n" ); | 
| xively | 0:efdea27c3b81 | 245 | xi_feed_update(xi_context, &feed); | 
| xively | 0:efdea27c3b81 | 246 | mbed_printf( "done...\n" ); | 
| xively | 3:7ad3f6543b6e | 247 | |
| xively | 3:7ad3f6543b6e | 248 | wait( 15.0 ); | 
| xively | 0:efdea27c3b81 | 249 | } | 
| xively | 0:efdea27c3b81 | 250 | } | 
