Test of xively server connectivity layer
Dependencies: C12832_lcd EthernetInterface mbed-rtos mbed
main.cpp@1:c9c854dada35, 2013-05-13 (annotated)
- Committer:
- olgierdh
- Date:
- Mon May 13 20:14:33 2013 +0000
- Revision:
- 1:c9c854dada35
- Parent:
- 0:a9881ec5a2b4
Removed credentials
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
olgierdh | 0:a9881ec5a2b4 | 1 | #include "mbed.h" |
olgierdh | 0:a9881ec5a2b4 | 2 | #include "EthernetInterface.h" |
olgierdh | 0:a9881ec5a2b4 | 3 | |
olgierdh | 0:a9881ec5a2b4 | 4 | #include <stdarg.h> |
olgierdh | 0:a9881ec5a2b4 | 5 | #include <stdio.h> |
olgierdh | 0:a9881ec5a2b4 | 6 | |
olgierdh | 0:a9881ec5a2b4 | 7 | #include "C12832_lcd.h" |
olgierdh | 0:a9881ec5a2b4 | 8 | |
olgierdh | 0:a9881ec5a2b4 | 9 | C12832_LCD lcd; |
olgierdh | 0:a9881ec5a2b4 | 10 | |
olgierdh | 0:a9881ec5a2b4 | 11 | extern "C" { |
olgierdh | 0:a9881ec5a2b4 | 12 | |
olgierdh | 0:a9881ec5a2b4 | 13 | void user_printf( const char* buffer ) |
olgierdh | 0:a9881ec5a2b4 | 14 | { |
olgierdh | 0:a9881ec5a2b4 | 15 | lcd.cls(); |
olgierdh | 0:a9881ec5a2b4 | 16 | lcd.locate( 0, 0 ); |
olgierdh | 0:a9881ec5a2b4 | 17 | lcd.printf( buffer ); |
olgierdh | 0:a9881ec5a2b4 | 18 | // wait( 1.0 ); |
olgierdh | 0:a9881ec5a2b4 | 19 | } |
olgierdh | 0:a9881ec5a2b4 | 20 | |
olgierdh | 0:a9881ec5a2b4 | 21 | void mbed_printf( const char* fmt, ... ) |
olgierdh | 0:a9881ec5a2b4 | 22 | { |
olgierdh | 0:a9881ec5a2b4 | 23 | char buffer[ 64 ]; |
olgierdh | 0:a9881ec5a2b4 | 24 | |
olgierdh | 0:a9881ec5a2b4 | 25 | va_list ap; |
olgierdh | 0:a9881ec5a2b4 | 26 | va_start( ap, fmt ); |
olgierdh | 0:a9881ec5a2b4 | 27 | vsnprintf( buffer, 64, fmt, ap ); |
olgierdh | 0:a9881ec5a2b4 | 28 | va_end( ap ); |
olgierdh | 0:a9881ec5a2b4 | 29 | |
olgierdh | 0:a9881ec5a2b4 | 30 | user_printf( buffer ); |
olgierdh | 0:a9881ec5a2b4 | 31 | } |
olgierdh | 0:a9881ec5a2b4 | 32 | |
olgierdh | 0:a9881ec5a2b4 | 33 | } |
olgierdh | 0:a9881ec5a2b4 | 34 | |
olgierdh | 0:a9881ec5a2b4 | 35 | #define ADDRESS "api.cosm.com" |
olgierdh | 0:a9881ec5a2b4 | 36 | #define PORT 80 |
olgierdh | 1:c9c854dada35 | 37 | #define API_KEY "youre_api_key" |
olgierdh | 1:c9c854dada35 | 38 | #define FEED_ID "youre_feed_id" |
olgierdh | 0:a9881ec5a2b4 | 39 | |
olgierdh | 1:c9c854dada35 | 40 | #define MSG "PUT /v2/feeds/" FEED_ID ".csv HTTP/1.1\r\n"\ |
olgierdh | 0:a9881ec5a2b4 | 41 | "Host: api.xively.com\r\n"\ |
olgierdh | 1:c9c854dada35 | 42 | "User-Agent: mbed_test/0.1.x\r\n"\ |
olgierdh | 0:a9881ec5a2b4 | 43 | "Accept: */*\r\n"\ |
olgierdh | 1:c9c854dada35 | 44 | "X-ApiKey: " API_KEY "\r\n"\ |
olgierdh | 0:a9881ec5a2b4 | 45 | "Content-Type: text/plain\r\n"\ |
olgierdh | 0:a9881ec5a2b4 | 46 | "Content-Length: 33\r\n\r\n"\ |
olgierdh | 0:a9881ec5a2b4 | 47 | "0001,test1\n"\ |
olgierdh | 0:a9881ec5a2b4 | 48 | "0002,test2\n"\ |
olgierdh | 0:a9881ec5a2b4 | 49 | "0003,test3\n\0" |
olgierdh | 0:a9881ec5a2b4 | 50 | |
olgierdh | 0:a9881ec5a2b4 | 51 | |
olgierdh | 0:a9881ec5a2b4 | 52 | int main() { |
olgierdh | 0:a9881ec5a2b4 | 53 | EthernetInterface eth; |
olgierdh | 0:a9881ec5a2b4 | 54 | |
olgierdh | 0:a9881ec5a2b4 | 55 | int s = eth.init(); //Use DHCP |
olgierdh | 0:a9881ec5a2b4 | 56 | |
olgierdh | 0:a9881ec5a2b4 | 57 | size_t msg_len = strlen( MSG ); |
olgierdh | 0:a9881ec5a2b4 | 58 | |
olgierdh | 0:a9881ec5a2b4 | 59 | if( s != NULL ) |
olgierdh | 0:a9881ec5a2b4 | 60 | { |
olgierdh | 0:a9881ec5a2b4 | 61 | mbed_printf( "Could not initialise. Will halt!\n" ); |
olgierdh | 0:a9881ec5a2b4 | 62 | exit( 0 ); |
olgierdh | 0:a9881ec5a2b4 | 63 | } |
olgierdh | 0:a9881ec5a2b4 | 64 | |
olgierdh | 0:a9881ec5a2b4 | 65 | s = eth.connect(); |
olgierdh | 0:a9881ec5a2b4 | 66 | |
olgierdh | 0:a9881ec5a2b4 | 67 | if( s != NULL ) |
olgierdh | 0:a9881ec5a2b4 | 68 | { |
olgierdh | 0:a9881ec5a2b4 | 69 | mbed_printf( "Could not connect. Will halt!\n" ); |
olgierdh | 0:a9881ec5a2b4 | 70 | exit( 0 ); |
olgierdh | 0:a9881ec5a2b4 | 71 | } |
olgierdh | 0:a9881ec5a2b4 | 72 | else |
olgierdh | 0:a9881ec5a2b4 | 73 | { |
olgierdh | 0:a9881ec5a2b4 | 74 | mbed_printf( "IP: %s\n", eth.getIPAddress() ); |
olgierdh | 0:a9881ec5a2b4 | 75 | } |
olgierdh | 0:a9881ec5a2b4 | 76 | |
olgierdh | 0:a9881ec5a2b4 | 77 | |
olgierdh | 0:a9881ec5a2b4 | 78 | TCPSocketConnection connection; |
olgierdh | 0:a9881ec5a2b4 | 79 | #define RECV_BUFFER_SIZE 512 |
olgierdh | 0:a9881ec5a2b4 | 80 | char buffer[ RECV_BUFFER_SIZE ]; |
olgierdh | 0:a9881ec5a2b4 | 81 | |
olgierdh | 0:a9881ec5a2b4 | 82 | while( 1 ) |
olgierdh | 0:a9881ec5a2b4 | 83 | { |
olgierdh | 0:a9881ec5a2b4 | 84 | if( connection.connect( ADDRESS, PORT ) == -1 ) |
olgierdh | 0:a9881ec5a2b4 | 85 | { |
olgierdh | 0:a9881ec5a2b4 | 86 | mbed_printf( "Error during connection attempt\n" ); |
olgierdh | 0:a9881ec5a2b4 | 87 | wait( 1.0 ); |
olgierdh | 0:a9881ec5a2b4 | 88 | continue; |
olgierdh | 0:a9881ec5a2b4 | 89 | } |
olgierdh | 0:a9881ec5a2b4 | 90 | |
olgierdh | 0:a9881ec5a2b4 | 91 | if( connection.send( MSG, msg_len ) == -1 ) |
olgierdh | 0:a9881ec5a2b4 | 92 | { |
olgierdh | 0:a9881ec5a2b4 | 93 | mbed_printf( "Error during send attempt\n" ); |
olgierdh | 0:a9881ec5a2b4 | 94 | wait( 1.0 ); |
olgierdh | 0:a9881ec5a2b4 | 95 | goto close; |
olgierdh | 0:a9881ec5a2b4 | 96 | } |
olgierdh | 0:a9881ec5a2b4 | 97 | |
olgierdh | 0:a9881ec5a2b4 | 98 | memset( buffer, 0, RECV_BUFFER_SIZE ); |
olgierdh | 0:a9881ec5a2b4 | 99 | |
olgierdh | 0:a9881ec5a2b4 | 100 | if( connection.receive( buffer, RECV_BUFFER_SIZE ) == -1 ) |
olgierdh | 0:a9881ec5a2b4 | 101 | { |
olgierdh | 0:a9881ec5a2b4 | 102 | mbed_printf( "Error during receive attempt\n" ); |
olgierdh | 0:a9881ec5a2b4 | 103 | wait( 1.0 ); |
olgierdh | 0:a9881ec5a2b4 | 104 | } |
olgierdh | 0:a9881ec5a2b4 | 105 | close: |
olgierdh | 0:a9881ec5a2b4 | 106 | connection.close(); |
olgierdh | 0:a9881ec5a2b4 | 107 | } |
olgierdh | 0:a9881ec5a2b4 | 108 | } |