Mode 3
Dependencies: C12832_lcd EthernetInterface LM75B MMA7660 libxively mbed-rtos mbed
Fork of Mode_3_MBED by
main.cpp@16:9a4ff6887439, 2014-06-10 (annotated)
- Committer:
- bhakti08
- Date:
- Tue Jun 10 00:24:35 2014 +0000
- Revision:
- 16:9a4ff6887439
- Parent:
- 15:53f65089126b
Mode 3 final;
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 | 7:0eff5db44b8b | 3 | |
zaincbs | 15:53f65089126b | 4 | #define XI_FEED_ID 1344466483 // set Xively Feed ID (numerical, no quoutes) |
zaincbs | 15:53f65089126b | 5 | #define XI_API_KEY "mgjx3VwlsvsMBhekqcASRLnzMPHi9Aw2gZCZCzyH0vQkefn3" // set Xively API key (double-quoted string) |
errordeveloper | 10:86ffba646df1 | 6 | |
errordeveloper | 10:86ffba646df1 | 7 | #include "app_board_io.h" |
xively | 7:0eff5db44b8b | 8 | |
xively | 0:efdea27c3b81 | 9 | #include "xively.h" |
xively | 0:efdea27c3b81 | 10 | #include "xi_err.h" |
xively | 0:efdea27c3b81 | 11 | |
errordeveloper | 10:86ffba646df1 | 12 | #include "MMA7660.h" |
errordeveloper | 10:86ffba646df1 | 13 | #include "LM75B.h" |
errordeveloper | 11:bdf601a405fc | 14 | #include "C12832_lcd.h" |
zaincbs | 15:53f65089126b | 15 | #define ECHO_SERVER_PORT 7 |
bhakti08 | 16:9a4ff6887439 | 16 | #include <string.h> |
bhakti08 | 16:9a4ff6887439 | 17 | |
bhakti08 | 16:9a4ff6887439 | 18 | #define FWD 3 |
bhakti08 | 16:9a4ff6887439 | 19 | #define REV 4 |
bhakti08 | 16:9a4ff6887439 | 20 | #define LEFT 1 |
bhakti08 | 16:9a4ff6887439 | 21 | #define RIGHT 2 |
bhakti08 | 16:9a4ff6887439 | 22 | #define STOP 0 |
bhakti08 | 16:9a4ff6887439 | 23 | #define STRAIGHT_WHEEL 5 |
bhakti08 | 16:9a4ff6887439 | 24 | #define servo_1 6 |
bhakti08 | 16:9a4ff6887439 | 25 | #define servo_2 7 |
bhakti08 | 16:9a4ff6887439 | 26 | |
xively | 0:efdea27c3b81 | 27 | MMA7660 axl(p28, p27); |
bhakti08 | 14:e32799a15720 | 28 | //LM75B tmp(p28, p27); |
bhakti08 | 14:e32799a15720 | 29 | AnalogIn tmp(p19); |
errordeveloper | 11:bdf601a405fc | 30 | C12832_LCD lcd; |
zaincbs | 15:53f65089126b | 31 | DigitalOut cL(LED1); |
zaincbs | 15:53f65089126b | 32 | Serial pc(USBTX,USBRX); |
bhakti08 | 16:9a4ff6887439 | 33 | BusOut motor(p5,p6,p7); |
errordeveloper | 11:bdf601a405fc | 34 | #include "logo.h" |
xively | 0:efdea27c3b81 | 35 | |
xively | 0:efdea27c3b81 | 36 | int main() { |
zaincbs | 15:53f65089126b | 37 | cL = 1; |
zaincbs | 15:53f65089126b | 38 | /*lcd_print_xively_logo();*/ |
xively | 0:efdea27c3b81 | 39 | EthernetInterface eth; |
zaincbs | 15:53f65089126b | 40 | eth.init(); //Use DHCP |
zaincbs | 15:53f65089126b | 41 | eth.connect(); |
zaincbs | 15:53f65089126b | 42 | pc.printf("IP Address is %s\n", eth.getIPAddress()); |
zaincbs | 15:53f65089126b | 43 | TCPSocketServer server; |
zaincbs | 15:53f65089126b | 44 | server.bind(ECHO_SERVER_PORT); |
zaincbs | 15:53f65089126b | 45 | server.listen(); |
zaincbs | 15:53f65089126b | 46 | while (true) { |
zaincbs | 15:53f65089126b | 47 | pc.printf("\nWait for new connection...\n"); |
zaincbs | 15:53f65089126b | 48 | TCPSocketConnection client; |
zaincbs | 15:53f65089126b | 49 | server.accept(client); |
zaincbs | 15:53f65089126b | 50 | client.set_blocking(false, 150000); // Timeout after (1.5)s |
xively | 0:efdea27c3b81 | 51 | |
zaincbs | 15:53f65089126b | 52 | pc.printf("Connection from: %s\n", client.get_address()); |
zaincbs | 15:53f65089126b | 53 | char buffer[256]; |
bhakti08 | 16:9a4ff6887439 | 54 | motor = STOP; |
bhakti08 | 16:9a4ff6887439 | 55 | int n = client.receive(buffer, sizeof(buffer)); |
bhakti08 | 16:9a4ff6887439 | 56 | if (n <= 0) continue; |
bhakti08 | 16:9a4ff6887439 | 57 | buffer[n] = 0; |
bhakti08 | 16:9a4ff6887439 | 58 | pc.printf("String is : %s\r\n",buffer); |
bhakti08 | 16:9a4ff6887439 | 59 | |
bhakti08 | 16:9a4ff6887439 | 60 | client.send_all(buffer, n); |
bhakti08 | 16:9a4ff6887439 | 61 | if (!(strcmp (buffer, "w"))) |
bhakti08 | 16:9a4ff6887439 | 62 | motor = FWD; |
bhakti08 | 16:9a4ff6887439 | 63 | else if (!(strcmp(buffer,"x"))) |
bhakti08 | 16:9a4ff6887439 | 64 | motor = REV; |
bhakti08 | 16:9a4ff6887439 | 65 | else if (!(strcmp(buffer,"z"))) |
bhakti08 | 16:9a4ff6887439 | 66 | motor = STRAIGHT_WHEEL; |
bhakti08 | 16:9a4ff6887439 | 67 | else if (!(strcmp(buffer,"d"))) |
bhakti08 | 16:9a4ff6887439 | 68 | motor = RIGHT; |
bhakti08 | 16:9a4ff6887439 | 69 | else if (!(strcmp(buffer,"a"))) |
bhakti08 | 16:9a4ff6887439 | 70 | motor = LEFT; |
bhakti08 | 16:9a4ff6887439 | 71 | else if (!(strcmp(buffer,"s"))) |
bhakti08 | 16:9a4ff6887439 | 72 | motor = STOP; |
bhakti08 | 16:9a4ff6887439 | 73 | else if (!(strcmp(buffer,"o"))) |
bhakti08 | 16:9a4ff6887439 | 74 | motor = servo_1; |
bhakti08 | 16:9a4ff6887439 | 75 | else if (!(strcmp(buffer,"p"))) |
bhakti08 | 16:9a4ff6887439 | 76 | motor = servo_2; |
zaincbs | 15:53f65089126b | 77 | |
zaincbs | 15:53f65089126b | 78 | pc.printf("Received:%s", buffer); |
xively | 0:efdea27c3b81 | 79 | |
xively | 0:efdea27c3b81 | 80 | xi_feed_t feed; |
bhakti08 | 16:9a4ff6887439 | 81 | memset( &feed, NULL, sizeof( xi_feed_t ) ); |
xively | 0:efdea27c3b81 | 82 | |
bhakti08 | 16:9a4ff6887439 | 83 | feed.feed_id = XI_FEED_ID; |
bhakti08 | 16:9a4ff6887439 | 84 | feed.datastream_count = 4; |
zaincbs | 15:53f65089126b | 85 | |
bhakti08 | 16:9a4ff6887439 | 86 | feed.datastreams[0].datapoint_count = 1; |
bhakti08 | 16:9a4ff6887439 | 87 | xi_datastream_t* temperature_datastream = &feed.datastreams[0]; |
bhakti08 | 16:9a4ff6887439 | 88 | strcpy( temperature_datastream->datastream_id, "Temperature" ); |
bhakti08 | 16:9a4ff6887439 | 89 | xi_datapoint_t* current_temperature = &temperature_datastream->datapoints[0]; |
bhakti08 | 16:9a4ff6887439 | 90 | |
bhakti08 | 16:9a4ff6887439 | 91 | feed.datastreams[1].datapoint_count = 1; |
bhakti08 | 16:9a4ff6887439 | 92 | xi_datastream_t* x_axis_datastream = &feed.datastreams[1]; |
bhakti08 | 16:9a4ff6887439 | 93 | strcpy( x_axis_datastream->datastream_id, "X_axis" ); |
bhakti08 | 16:9a4ff6887439 | 94 | xi_datapoint_t* accel_x = &x_axis_datastream->datapoints[0]; |
bhakti08 | 16:9a4ff6887439 | 95 | |
bhakti08 | 16:9a4ff6887439 | 96 | feed.datastreams[2].datapoint_count = 1; |
bhakti08 | 16:9a4ff6887439 | 97 | xi_datastream_t* y_axis_datastream = &feed.datastreams[2]; |
bhakti08 | 16:9a4ff6887439 | 98 | strcpy( y_axis_datastream->datastream_id, "Y_axis" ); |
bhakti08 | 16:9a4ff6887439 | 99 | xi_datapoint_t* accel_y = &y_axis_datastream->datapoints[0]; |
bhakti08 | 16:9a4ff6887439 | 100 | |
bhakti08 | 16:9a4ff6887439 | 101 | feed.datastreams[3].datapoint_count = 1; |
bhakti08 | 16:9a4ff6887439 | 102 | xi_datastream_t* z_axis_datastream = &feed.datastreams[3]; |
bhakti08 | 16:9a4ff6887439 | 103 | strcpy( z_axis_datastream->datastream_id, "Z_axis" ); |
bhakti08 | 16:9a4ff6887439 | 104 | xi_datapoint_t* accel_z = &z_axis_datastream->datapoints[0]; |
bhakti08 | 16:9a4ff6887439 | 105 | |
bhakti08 | 16:9a4ff6887439 | 106 | xi_context_t* xi_context |
bhakti08 | 16:9a4ff6887439 | 107 | = xi_create_context( XI_HTTP, XI_API_KEY, feed.feed_id ); |
bhakti08 | 16:9a4ff6887439 | 108 | if( xi_context == NULL ) |
bhakti08 | 16:9a4ff6887439 | 109 | { |
bhakti08 | 16:9a4ff6887439 | 110 | pc.printf("Error in Xi_Context\r\n"); |
bhakti08 | 16:9a4ff6887439 | 111 | exit (0); |
zaincbs | 15:53f65089126b | 112 | } |
bhakti08 | 16:9a4ff6887439 | 113 | |
bhakti08 | 16:9a4ff6887439 | 114 | pc.printf("In xively thread\r\n"); |
bhakti08 | 16:9a4ff6887439 | 115 | xi_set_value_f32( current_temperature, tmp.read() ); |
bhakti08 | 16:9a4ff6887439 | 116 | xi_set_value_f32( accel_x ,axl.x() ); |
bhakti08 | 16:9a4ff6887439 | 117 | xi_set_value_f32( accel_y ,axl.y() ); |
bhakti08 | 16:9a4ff6887439 | 118 | xi_set_value_f32( accel_z ,axl.z() ); |
bhakti08 | 16:9a4ff6887439 | 119 | pc.printf("Value set\r\n"); |
bhakti08 | 16:9a4ff6887439 | 120 | xi_feed_update( xi_context, &feed ); |
bhakti08 | 16:9a4ff6887439 | 121 | pc.printf("Update\r\n"); |
bhakti08 | 16:9a4ff6887439 | 122 | //client.close(); |
zaincbs | 15:53f65089126b | 123 | |
zaincbs | 15:53f65089126b | 124 | } |
zaincbs | 15:53f65089126b | 125 | |
bhakti08 | 16:9a4ff6887439 | 126 | } |