lpc1768-picotcp-eth to revision 8, PicoTCP to revision 41 (mbed-rtos to revision 12, mbed to revision 63)

Dependencies:   PicoTCP lpc1768-picotcp-eth mbed-rtos mbed

Fork of TCPSocket_HelloWorldTest by Daniel Peter

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 
00004 #define RETRIES_ALLOWED 5
00005 
00006 Serial pc2(USBTX, USBRX);
00007 EthernetInterface eth;
00008 DigitalOut led1(LED1);
00009 
00010 int main()
00011 {
00012 
00013     pc2.baud(115200);
00014     printf("Initialising...\n");
00015     set_time(0);
00016 
00017     // use DHCP
00018     eth.init();
00019 
00020     // attempt DHCP and if timing out then try again
00021     while (eth.connect()) {
00022         printf("DHCP timeout\n");
00023     };
00024 
00025     // successful DHCP
00026     printf("IP Address is %s\n", eth.getIPAddress());
00027 
00028     // loop forever
00029     while (true) {
00030 
00031         // new non blocking socket
00032         TCPSocketConnection sock;
00033         sock.set_blocking(false);
00034 
00035         // toggle led to indicate activity
00036         led1 = !led1;
00037 
00038         // attempt socket connection and if failure then loop again
00039         if (sock.connect("mbed.org", 80)) {
00040             printf("Socket connect timeout\n");
00041             continue;
00042         }
00043 
00044         // send command
00045         printf("Sending at %d seconds\n", time(NULL));
00046         char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n";
00047         int send_result = sock.send_all(http_cmd, sizeof(http_cmd) - 1);
00048         if (send_result <= 0)
00049             printf("Internal stack error : %d\n", pico_err);
00050 
00051         // receive response
00052         int received = 0;
00053         int retries = 0;
00054         while (true) {
00055 
00056             int result;
00057             char buffer[256];
00058 
00059             result = sock.receive(buffer, sizeof(buffer) - 1);
00060             printf("Received: %d %d %d\n", result, received, retries);
00061 
00062             // if timeout or no data
00063             if (result <= 0) {
00064 
00065                 // if data previously received then stop receiving
00066                 if (received > 0)
00067                     break;
00068 
00069                 // if incremented retries exceeded then no response and stop receiving
00070                 if (++retries > RETRIES_ALLOWED) {
00071                     break;
00072                 }
00073 
00074             } else {
00075 
00076                 // zero terminate the buffer
00077                 buffer[result] = '\0';
00078 
00079                 // total size of data received
00080                 received += result;
00081             }
00082 
00083         }
00084 
00085         // close socket at end of send and receive
00086         sock.close();
00087         
00088         // wait before repeating
00089         Thread::wait(1000);
00090 
00091     }
00092 }