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.
main.cpp@6:9cf6630fa25d, 2021-02-17 (annotated)
- Committer:
- 4180_1
- Date:
- Wed Feb 17 18:55:05 2021 +0000
- Revision:
- 6:9cf6630fa25d
- Parent:
- 5:66c4a71d22e9
ver 1.2 Added Basic IoT demo controlling LEDs from web page data
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mab5449 | 0:6b383744246e | 1 | #include "mbed.h" |
mab5449 | 0:6b383744246e | 2 | #include "EthernetInterface.h" |
4180_1 | 6:9cf6630fa25d | 3 | BusOut leds(LED1,LED2,LED3,LED4); //LEDs are controlled by web page text data |
mab5449 | 0:6b383744246e | 4 | // Network interface |
mab5449 | 0:6b383744246e | 5 | EthernetInterface net; |
mab5449 | 0:6b383744246e | 6 | |
mab5449 | 0:6b383744246e | 7 | // Socket demo |
4180_1 | 6:9cf6630fa25d | 8 | int main() |
4180_1 | 6:9cf6630fa25d | 9 | { |
4180_1 | 5:66c4a71d22e9 | 10 | // Show MAC in case it is needed to enable DHCP on a secure network |
4180_1 | 5:66c4a71d22e9 | 11 | char mac[6]; |
4180_1 | 5:66c4a71d22e9 | 12 | mbed_mac_address(mac); |
4180_1 | 6:9cf6630fa25d | 13 | 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]); |
mab5449 | 0:6b383744246e | 14 | // Bring up the ethernet interface |
4180_1 | 5:66c4a71d22e9 | 15 | printf("Waiting for IP address from DHCP Server\n\r"); |
4180_1 | 5:66c4a71d22e9 | 16 | wait(1.0); |
4180_1 | 5:66c4a71d22e9 | 17 | net.connect(); |
4180_1 | 3:576f312d2601 | 18 | printf("\n\rEthernet socket example\n\r"); |
mab5449 | 0:6b383744246e | 19 | |
mab5449 | 0:6b383744246e | 20 | // Show the network address |
mab5449 | 0:6b383744246e | 21 | const char *ip = net.get_ip_address(); |
4180_1 | 5:66c4a71d22e9 | 22 | printf("IP address is: %s\n\r", ip ? ip : "Timeout - No IP obtained"); |
mab5449 | 0:6b383744246e | 23 | |
mab5449 | 0:6b383744246e | 24 | // Open a socket on the network interface, and create a TCP connection to mbed.org |
mab5449 | 0:6b383744246e | 25 | TCPSocket socket; |
mab5449 | 0:6b383744246e | 26 | socket.open(&net); |
4180_1 | 3:576f312d2601 | 27 | socket.connect("hamblen.ece.gatech.edu", 80); |
mab5449 | 0:6b383744246e | 28 | |
mab5449 | 0:6b383744246e | 29 | // Send a simple http request |
4180_1 | 3:576f312d2601 | 30 | char sbuffer[] = "GET /hello.txt HTTP/1.1\r\nHost: hamblen.ece.gatech.edu\r\n\r\n"; |
mab5449 | 0:6b383744246e | 31 | int scount = socket.send(sbuffer, sizeof sbuffer); |
4180_1 | 3:576f312d2601 | 32 | //print out packet |
4180_1 | 4:d3627ca18f87 | 33 | printf("sent %d [%.*s]\n\r", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); |
mab5449 | 0:6b383744246e | 34 | |
4180_1 | 3:576f312d2601 | 35 | // Recieve a simple http response and print out the response line and text |
4180_1 | 3:576f312d2601 | 36 | char rbuffer[400]; //enough for a very short text page - almost out of RAM! |
mab5449 | 0:6b383744246e | 37 | int rcount = socket.recv(rbuffer, sizeof rbuffer); |
4180_1 | 5:66c4a71d22e9 | 38 | rbuffer[rcount] = 0; //terminate to print as a C string; |
4180_1 | 6:9cf6630fa25d | 39 | //Print packet read from HTTP web page server |
4180_1 | 3:576f312d2601 | 40 | printf("recv %d [%.*s]\n\r", rcount, strstr(rbuffer, "\r\n"), rbuffer); |
mab5449 | 0:6b383744246e | 41 | |
mab5449 | 0:6b383744246e | 42 | // Close the socket to return its memory and bring down the network interface |
mab5449 | 0:6b383744246e | 43 | socket.close(); |
mab5449 | 0:6b383744246e | 44 | |
4180_1 | 6:9cf6630fa25d | 45 | // Basic IoT demo controlling mbeds 4 LEDs from Internet web page's ASCII text data |
4180_1 | 6:9cf6630fa25d | 46 | // Web page demo contains a line "Data:0101" |
4180_1 | 6:9cf6630fa25d | 47 | char *data; |
4180_1 | 6:9cf6630fa25d | 48 | data = strstr(rbuffer,"Data:"); //Find Data: line on web page with '0's or '1's |
4180_1 | 6:9cf6630fa25d | 49 | data = data + 5; //Skip to data |
4180_1 | 6:9cf6630fa25d | 50 | for (int i=0; i<=3; i++) //Parse 4 web page characters to control LEDs |
4180_1 | 6:9cf6630fa25d | 51 | leds[i] = data[i] - '0'; //convert ASCII '0' or '1's to binary to control 4 leds |
4180_1 | 6:9cf6630fa25d | 52 | //Another device could change the web page contents to control LEDs from anywhere |
4180_1 | 6:9cf6630fa25d | 53 | |
mab5449 | 0:6b383744246e | 54 | // Bring down the ethernet interface |
mab5449 | 0:6b383744246e | 55 | net.disconnect(); |
4180_1 | 5:66c4a71d22e9 | 56 | printf("Done\n\r"); |
mab5449 | 0:6b383744246e | 57 | } |