Alternative TCPSocket example using an HTTP GET to read a short "helloworld" text web page using a different server

Fork of ARMs demo HTTP socket demo. ARM's server was redirected and the demo was no longer working. An alternative server was setup and the code was modified to display the web page text in addition to just "200 OK". Only works for a very short web page - buffer only 400 characters but RAM is running out on the LPC1768 in the demo! Data read from the web page is read and parsed to control the mbed's 4 LEDs for a basic IoT demo.

Files at this revision

API Documentation at this revision

Comitter:
4180_1
Date:
Mon Feb 15 18:19:25 2021 +0000
Parent:
4:d3627ca18f87
Child:
6:9cf6630fa25d
Commit message:
ver 1.1 - added MAC printout and more messages

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Feb 04 20:39:35 2021 +0000
+++ b/main.cpp	Mon Feb 15 18:19:25 2021 +0000
@@ -6,13 +6,19 @@
 
 // Socket demo
 int main() {
+    // Show MAC in case it is needed to enable DHCP on a secure network
+    char mac[6];
+    mbed_mac_address(mac);
+    printf("\r\rmbed's MAC address is %02x:%02x:%02x:%02x:%02x:%02x\n\r", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); 
     // Bring up the ethernet interface
+    printf("Waiting for IP address from DHCP Server\n\r");
+    wait(1.0);
+    net.connect();
     printf("\n\rEthernet socket example\n\r");
-    net.connect();
 
     // Show the network address
     const char *ip = net.get_ip_address();
-    printf("IP address is: %s\n\r", ip ? ip : "No IP");
+    printf("IP address is: %s\n\r", ip ? ip : "Timeout - No IP obtained");
 
     // Open a socket on the network interface, and create a TCP connection to mbed.org
     TCPSocket socket;
@@ -28,6 +34,7 @@
     // Recieve a simple http response and print out the response line and text
     char rbuffer[400]; //enough for a very short text page - almost out of RAM!
     int rcount = socket.recv(rbuffer, sizeof rbuffer);
+    rbuffer[rcount] = 0; //terminate to print as a C string;
     printf("recv %d [%.*s]\n\r", rcount, strstr(rbuffer, "\r\n"), rbuffer);
 
     // Close the socket to return its memory and bring down the network interface
@@ -35,5 +42,5 @@
 
     // Bring down the ethernet interface
     net.disconnect();
-    printf("Done\n");
+    printf("Done\n\r");
 }