Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ELEC351 by
NETWORK.cpp@35:26b0a9b55d82, 2018-01-07 (annotated)
- Committer:
- thomasmorris
- Date:
- Sun Jan 07 13:32:54 2018 +0000
- Revision:
- 35:26b0a9b55d82
- Child:
- 36:a0098306fc58
Working Networking
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
thomasmorris | 35:26b0a9b55d82 | 1 | #include "NETWORK.hpp" |
thomasmorris | 35:26b0a9b55d82 | 2 | |
thomasmorris | 35:26b0a9b55d82 | 3 | AnalogIn ldr(PA_0); |
thomasmorris | 35:26b0a9b55d82 | 4 | |
thomasmorris | 35:26b0a9b55d82 | 5 | //Now setup a web server |
thomasmorris | 35:26b0a9b55d82 | 6 | TCPServer srv; //TCP/IP Server |
thomasmorris | 35:26b0a9b55d82 | 7 | TCPSocket clt_sock; //Socket for communication |
thomasmorris | 35:26b0a9b55d82 | 8 | SocketAddress clt_addr; //Address of incoming connection |
thomasmorris | 35:26b0a9b55d82 | 9 | |
thomasmorris | 35:26b0a9b55d82 | 10 | |
thomasmorris | 35:26b0a9b55d82 | 11 | void Network_Init() |
thomasmorris | 35:26b0a9b55d82 | 12 | { |
thomasmorris | 35:26b0a9b55d82 | 13 | printf("Basic HTTP server example\n"); |
thomasmorris | 35:26b0a9b55d82 | 14 | //Configure an ethernet connection |
thomasmorris | 35:26b0a9b55d82 | 15 | EthernetInterface eth; |
thomasmorris | 35:26b0a9b55d82 | 16 | eth.set_network(IP, NETMASK, GATEWAY); |
thomasmorris | 35:26b0a9b55d82 | 17 | eth.connect(); |
thomasmorris | 35:26b0a9b55d82 | 18 | printf("The target IP address is '%s'\n", eth.get_ip_address()); |
thomasmorris | 35:26b0a9b55d82 | 19 | //t1.start(Network_Thread); |
thomasmorris | 35:26b0a9b55d82 | 20 | /* Open the server on ethernet stack */ |
thomasmorris | 35:26b0a9b55d82 | 21 | srv.open(ð); |
thomasmorris | 35:26b0a9b55d82 | 22 | |
thomasmorris | 35:26b0a9b55d82 | 23 | /* Bind the HTTP port (TCP 80) to the server */ |
thomasmorris | 35:26b0a9b55d82 | 24 | srv.bind(eth.get_ip_address(), 80); |
thomasmorris | 35:26b0a9b55d82 | 25 | |
thomasmorris | 35:26b0a9b55d82 | 26 | /* Can handle 5 simultaneous connections */ |
thomasmorris | 35:26b0a9b55d82 | 27 | srv.listen(5); |
thomasmorris | 35:26b0a9b55d82 | 28 | } |
thomasmorris | 35:26b0a9b55d82 | 29 | void Networking() |
thomasmorris | 35:26b0a9b55d82 | 30 | { |
thomasmorris | 35:26b0a9b55d82 | 31 | //using namespace std; |
thomasmorris | 35:26b0a9b55d82 | 32 | //Block and wait on an incoming connection |
thomasmorris | 35:26b0a9b55d82 | 33 | srv.accept(&clt_sock, &clt_addr); |
thomasmorris | 35:26b0a9b55d82 | 34 | printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port()); |
thomasmorris | 35:26b0a9b55d82 | 35 | |
thomasmorris | 35:26b0a9b55d82 | 36 | //Uses a C++ string to make it easier to concatinate |
thomasmorris | 35:26b0a9b55d82 | 37 | string response; |
thomasmorris | 35:26b0a9b55d82 | 38 | //This is a C string |
thomasmorris | 35:26b0a9b55d82 | 39 | char ldr_str[64]; |
thomasmorris | 35:26b0a9b55d82 | 40 | |
thomasmorris | 35:26b0a9b55d82 | 41 | //Read the LDR value |
thomasmorris | 35:26b0a9b55d82 | 42 | float u = ldr; |
thomasmorris | 35:26b0a9b55d82 | 43 | |
thomasmorris | 35:26b0a9b55d82 | 44 | //Convert to a C String |
thomasmorris | 35:26b0a9b55d82 | 45 | sprintf(ldr_str, "%5.3f", u ); |
thomasmorris | 35:26b0a9b55d82 | 46 | printf("LDR: %5.3f\n\r", u); |
thomasmorris | 35:26b0a9b55d82 | 47 | |
thomasmorris | 35:26b0a9b55d82 | 48 | //Build the C++ string response |
thomasmorris | 35:26b0a9b55d82 | 49 | response = HTTP_MESSAGE_BODY1; |
thomasmorris | 35:26b0a9b55d82 | 50 | response += ldr_str; |
thomasmorris | 35:26b0a9b55d82 | 51 | response += HTTP_MESSAGE_BODY2; |
thomasmorris | 35:26b0a9b55d82 | 52 | |
thomasmorris | 35:26b0a9b55d82 | 53 | //Send static HTML response (as a C string) |
thomasmorris | 35:26b0a9b55d82 | 54 | clt_sock.send(response.c_str(), response.size()+6); |
thomasmorris | 35:26b0a9b55d82 | 55 | } |