Test of xively server connectivity layer
Dependencies: C12832_lcd EthernetInterface mbed-rtos mbed
main.cpp@0:a9881ec5a2b4, 2013-05-13 (annotated)
- Committer:
- olgierdh
- Date:
- Mon May 13 20:10:35 2013 +0000
- Revision:
- 0:a9881ec5a2b4
- Child:
- 1:c9c854dada35
This is simple mbed test that make the mbed device to crash after few minutes.
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 | 0:a9881ec5a2b4 | 37 | |
olgierdh | 0:a9881ec5a2b4 | 38 | #define MSG "PUT /v2/feeds/117107.csv HTTP/1.1\r\n"\ |
olgierdh | 0:a9881ec5a2b4 | 39 | "Host: api.xively.com\r\n"\ |
olgierdh | 0:a9881ec5a2b4 | 40 | "User-Agent: libxively/0.1.x\r\n"\ |
olgierdh | 0:a9881ec5a2b4 | 41 | "Accept: */*\r\n"\ |
olgierdh | 0:a9881ec5a2b4 | 42 | "X-ApiKey: ISx1ANJU8P4U2dPRqCfACUrHDBOSAKxOS3ZzazZ4Tk0zND0g\r\n"\ |
olgierdh | 0:a9881ec5a2b4 | 43 | "Content-Type: text/plain\r\n"\ |
olgierdh | 0:a9881ec5a2b4 | 44 | "Content-Length: 33\r\n\r\n"\ |
olgierdh | 0:a9881ec5a2b4 | 45 | "0001,test1\n"\ |
olgierdh | 0:a9881ec5a2b4 | 46 | "0002,test2\n"\ |
olgierdh | 0:a9881ec5a2b4 | 47 | "0003,test3\n\0" |
olgierdh | 0:a9881ec5a2b4 | 48 | |
olgierdh | 0:a9881ec5a2b4 | 49 | |
olgierdh | 0:a9881ec5a2b4 | 50 | int main() { |
olgierdh | 0:a9881ec5a2b4 | 51 | EthernetInterface eth; |
olgierdh | 0:a9881ec5a2b4 | 52 | |
olgierdh | 0:a9881ec5a2b4 | 53 | int s = eth.init(); //Use DHCP |
olgierdh | 0:a9881ec5a2b4 | 54 | |
olgierdh | 0:a9881ec5a2b4 | 55 | size_t msg_len = strlen( MSG ); |
olgierdh | 0:a9881ec5a2b4 | 56 | |
olgierdh | 0:a9881ec5a2b4 | 57 | if( s != NULL ) |
olgierdh | 0:a9881ec5a2b4 | 58 | { |
olgierdh | 0:a9881ec5a2b4 | 59 | mbed_printf( "Could not initialise. Will halt!\n" ); |
olgierdh | 0:a9881ec5a2b4 | 60 | exit( 0 ); |
olgierdh | 0:a9881ec5a2b4 | 61 | } |
olgierdh | 0:a9881ec5a2b4 | 62 | |
olgierdh | 0:a9881ec5a2b4 | 63 | s = eth.connect(); |
olgierdh | 0:a9881ec5a2b4 | 64 | |
olgierdh | 0:a9881ec5a2b4 | 65 | if( s != NULL ) |
olgierdh | 0:a9881ec5a2b4 | 66 | { |
olgierdh | 0:a9881ec5a2b4 | 67 | mbed_printf( "Could not connect. Will halt!\n" ); |
olgierdh | 0:a9881ec5a2b4 | 68 | exit( 0 ); |
olgierdh | 0:a9881ec5a2b4 | 69 | } |
olgierdh | 0:a9881ec5a2b4 | 70 | else |
olgierdh | 0:a9881ec5a2b4 | 71 | { |
olgierdh | 0:a9881ec5a2b4 | 72 | mbed_printf( "IP: %s\n", eth.getIPAddress() ); |
olgierdh | 0:a9881ec5a2b4 | 73 | } |
olgierdh | 0:a9881ec5a2b4 | 74 | |
olgierdh | 0:a9881ec5a2b4 | 75 | |
olgierdh | 0:a9881ec5a2b4 | 76 | TCPSocketConnection connection; |
olgierdh | 0:a9881ec5a2b4 | 77 | #define RECV_BUFFER_SIZE 512 |
olgierdh | 0:a9881ec5a2b4 | 78 | char buffer[ RECV_BUFFER_SIZE ]; |
olgierdh | 0:a9881ec5a2b4 | 79 | |
olgierdh | 0:a9881ec5a2b4 | 80 | while( 1 ) |
olgierdh | 0:a9881ec5a2b4 | 81 | { |
olgierdh | 0:a9881ec5a2b4 | 82 | if( connection.connect( ADDRESS, PORT ) == -1 ) |
olgierdh | 0:a9881ec5a2b4 | 83 | { |
olgierdh | 0:a9881ec5a2b4 | 84 | mbed_printf( "Error during connection attempt\n" ); |
olgierdh | 0:a9881ec5a2b4 | 85 | wait( 1.0 ); |
olgierdh | 0:a9881ec5a2b4 | 86 | continue; |
olgierdh | 0:a9881ec5a2b4 | 87 | } |
olgierdh | 0:a9881ec5a2b4 | 88 | |
olgierdh | 0:a9881ec5a2b4 | 89 | if( connection.send( MSG, msg_len ) == -1 ) |
olgierdh | 0:a9881ec5a2b4 | 90 | { |
olgierdh | 0:a9881ec5a2b4 | 91 | mbed_printf( "Error during send attempt\n" ); |
olgierdh | 0:a9881ec5a2b4 | 92 | wait( 1.0 ); |
olgierdh | 0:a9881ec5a2b4 | 93 | goto close; |
olgierdh | 0:a9881ec5a2b4 | 94 | } |
olgierdh | 0:a9881ec5a2b4 | 95 | |
olgierdh | 0:a9881ec5a2b4 | 96 | memset( buffer, 0, RECV_BUFFER_SIZE ); |
olgierdh | 0:a9881ec5a2b4 | 97 | |
olgierdh | 0:a9881ec5a2b4 | 98 | if( connection.receive( buffer, RECV_BUFFER_SIZE ) == -1 ) |
olgierdh | 0:a9881ec5a2b4 | 99 | { |
olgierdh | 0:a9881ec5a2b4 | 100 | mbed_printf( "Error during receive attempt\n" ); |
olgierdh | 0:a9881ec5a2b4 | 101 | wait( 1.0 ); |
olgierdh | 0:a9881ec5a2b4 | 102 | } |
olgierdh | 0:a9881ec5a2b4 | 103 | close: |
olgierdh | 0:a9881ec5a2b4 | 104 | connection.close(); |
olgierdh | 0:a9881ec5a2b4 | 105 | } |
olgierdh | 0:a9881ec5a2b4 | 106 | } |