![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
something
Dependencies: BMP280 Task671-mbedos-FZ429-TCP-dynamic
Fork of Task671-mbedos-FZ429-TCP-dynamic by
main.cpp@0:65ff7ad381e8, 2017-11-20 (annotated)
- Committer:
- noutram
- Date:
- Mon Nov 20 17:31:23 2017 +0000
- Revision:
- 0:65ff7ad381e8
- Child:
- 1:76bd6f78cabc
Simple TCP/IP Server (dynamic web page)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:65ff7ad381e8 | 1 | #if !FEATURE_LWIP |
noutram | 0:65ff7ad381e8 | 2 | #error [NOT_SUPPORTED] LWIP not supported for this target |
noutram | 0:65ff7ad381e8 | 3 | #endif |
noutram | 0:65ff7ad381e8 | 4 | |
noutram | 0:65ff7ad381e8 | 5 | #include "mbed.h" |
noutram | 0:65ff7ad381e8 | 6 | #include "EthernetInterface.h" |
noutram | 0:65ff7ad381e8 | 7 | #include "TCPServer.h" |
noutram | 0:65ff7ad381e8 | 8 | #include "TCPSocket.h" |
noutram | 0:65ff7ad381e8 | 9 | #include <iostream> |
noutram | 0:65ff7ad381e8 | 10 | #include <string> |
noutram | 0:65ff7ad381e8 | 11 | |
noutram | 0:65ff7ad381e8 | 12 | #define HTTP_STATUS_LINE "HTTP/1.0 200 OK" |
noutram | 0:65ff7ad381e8 | 13 | #define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8" |
noutram | 0:65ff7ad381e8 | 14 | #define HTTP_MESSAGE_BODY1 "" \ |
noutram | 0:65ff7ad381e8 | 15 | "<html>" "\r\n" \ |
noutram | 0:65ff7ad381e8 | 16 | " <body style=\"display:flex;text-align:center\">" "\r\n" \ |
noutram | 0:65ff7ad381e8 | 17 | " <div style=\"margin:auto\">" "\r\n" \ |
noutram | 0:65ff7ad381e8 | 18 | " <h1>Hello World</h1>" "\r\n" \ |
noutram | 0:65ff7ad381e8 | 19 | " <p>The POT value is " |
noutram | 0:65ff7ad381e8 | 20 | |
noutram | 0:65ff7ad381e8 | 21 | #define HTTP_MESSAGE_BODY2 "" \ |
noutram | 0:65ff7ad381e8 | 22 | "</p>" "\r\n" \ |
noutram | 0:65ff7ad381e8 | 23 | " </div>" "\r\n" \ |
noutram | 0:65ff7ad381e8 | 24 | " </body>" "\r\n" \ |
noutram | 0:65ff7ad381e8 | 25 | "</html>" |
noutram | 0:65ff7ad381e8 | 26 | |
noutram | 0:65ff7ad381e8 | 27 | #define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n" \ |
noutram | 0:65ff7ad381e8 | 28 | HTTP_HEADER_FIELDS "\r\n" \ |
noutram | 0:65ff7ad381e8 | 29 | "\r\n" \ |
noutram | 0:65ff7ad381e8 | 30 | HTTP_MESSAGE_BODY "\r\n" |
noutram | 0:65ff7ad381e8 | 31 | |
noutram | 0:65ff7ad381e8 | 32 | #define IP "10.0.0.10" |
noutram | 0:65ff7ad381e8 | 33 | #define NETMASK "255.0.0.0" |
noutram | 0:65ff7ad381e8 | 34 | #define GATEWAY "10.0.0.1" |
noutram | 0:65ff7ad381e8 | 35 | |
noutram | 0:65ff7ad381e8 | 36 | AnalogIn pot(PA_0); |
noutram | 0:65ff7ad381e8 | 37 | |
noutram | 0:65ff7ad381e8 | 38 | |
noutram | 0:65ff7ad381e8 | 39 | int main() |
noutram | 0:65ff7ad381e8 | 40 | { |
noutram | 0:65ff7ad381e8 | 41 | printf("Basic HTTP server example\n"); |
noutram | 0:65ff7ad381e8 | 42 | |
noutram | 0:65ff7ad381e8 | 43 | //Configure an ethernet connection |
noutram | 0:65ff7ad381e8 | 44 | EthernetInterface eth; |
noutram | 0:65ff7ad381e8 | 45 | eth.set_network(IP, NETMASK, GATEWAY); |
noutram | 0:65ff7ad381e8 | 46 | eth.connect(); |
noutram | 0:65ff7ad381e8 | 47 | printf("The target IP address is '%s'\n", eth.get_ip_address()); |
noutram | 0:65ff7ad381e8 | 48 | |
noutram | 0:65ff7ad381e8 | 49 | //Now setup a web server |
noutram | 0:65ff7ad381e8 | 50 | TCPServer srv; //TCP/IP Server |
noutram | 0:65ff7ad381e8 | 51 | TCPSocket clt_sock; //Socket for communication |
noutram | 0:65ff7ad381e8 | 52 | SocketAddress clt_addr; //Address of incoming connection |
noutram | 0:65ff7ad381e8 | 53 | |
noutram | 0:65ff7ad381e8 | 54 | /* Open the server on ethernet stack */ |
noutram | 0:65ff7ad381e8 | 55 | srv.open(ð); |
noutram | 0:65ff7ad381e8 | 56 | |
noutram | 0:65ff7ad381e8 | 57 | /* Bind the HTTP port (TCP 80) to the server */ |
noutram | 0:65ff7ad381e8 | 58 | srv.bind(eth.get_ip_address(), 80); |
noutram | 0:65ff7ad381e8 | 59 | |
noutram | 0:65ff7ad381e8 | 60 | /* Can handle 5 simultaneous connections */ |
noutram | 0:65ff7ad381e8 | 61 | srv.listen(5); |
noutram | 0:65ff7ad381e8 | 62 | |
noutram | 0:65ff7ad381e8 | 63 | while (true) { |
noutram | 0:65ff7ad381e8 | 64 | using namespace std; |
noutram | 0:65ff7ad381e8 | 65 | //Block and wait on an incoming connection |
noutram | 0:65ff7ad381e8 | 66 | srv.accept(&clt_sock, &clt_addr); |
noutram | 0:65ff7ad381e8 | 67 | printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port()); |
noutram | 0:65ff7ad381e8 | 68 | |
noutram | 0:65ff7ad381e8 | 69 | string response; |
noutram | 0:65ff7ad381e8 | 70 | char pot_str[64]; |
noutram | 0:65ff7ad381e8 | 71 | float u = pot; |
noutram | 0:65ff7ad381e8 | 72 | sprintf(pot_str, "%5.3f", u ); |
noutram | 0:65ff7ad381e8 | 73 | printf("POT: %5.3f\n\r", u); |
noutram | 0:65ff7ad381e8 | 74 | |
noutram | 0:65ff7ad381e8 | 75 | response = HTTP_MESSAGE_BODY1; |
noutram | 0:65ff7ad381e8 | 76 | response += pot_str; |
noutram | 0:65ff7ad381e8 | 77 | response += HTTP_MESSAGE_BODY2; |
noutram | 0:65ff7ad381e8 | 78 | |
noutram | 0:65ff7ad381e8 | 79 | //Send static HTML response |
noutram | 0:65ff7ad381e8 | 80 | clt_sock.send(response.c_str(), response.size()+6); |
noutram | 0:65ff7ad381e8 | 81 | } |
noutram | 0:65ff7ad381e8 | 82 | } |