
Simple TCP/IP Server (static web page_
main.cpp@3:c62ab6a06986, 2019-11-15 (annotated)
- Committer:
- noutram
- Date:
- Fri Nov 15 10:47:27 2019 +0000
- Revision:
- 3:c62ab6a06986
- Parent:
- 2:31688a1ac73a
Updated for new mbed-os version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 3:c62ab6a06986 | 1 | /*#if !FEATURE_LWIP |
noutram | 3:c62ab6a06986 | 2 | #error [NOT_SUPPORTED] LWIP not supported for this target |
noutram | 3:c62ab6a06986 | 3 | #endif |
noutram | 3:c62ab6a06986 | 4 | */ |
noutram | 0:5552cdb52638 | 5 | #include "mbed.h" |
noutram | 0:5552cdb52638 | 6 | #include "EthernetInterface.h" |
noutram | 0:5552cdb52638 | 7 | #include "TCPSocket.h" |
noutram | 0:5552cdb52638 | 8 | |
noutram | 0:5552cdb52638 | 9 | #define HTTP_STATUS_LINE "HTTP/1.0 200 OK" |
noutram | 0:5552cdb52638 | 10 | #define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8" |
noutram | 0:5552cdb52638 | 11 | #define HTTP_MESSAGE_BODY "" \ |
noutram | 0:5552cdb52638 | 12 | "<html>" "\r\n" \ |
noutram | 0:5552cdb52638 | 13 | " <body style=\"display:flex;text-align:center\">" "\r\n" \ |
noutram | 0:5552cdb52638 | 14 | " <div style=\"margin:auto\">" "\r\n" \ |
noutram | 0:5552cdb52638 | 15 | " <h1>Hello World</h1>" "\r\n" \ |
noutram | 0:5552cdb52638 | 16 | " <p>It works !</p>" "\r\n" \ |
noutram | 0:5552cdb52638 | 17 | " </div>" "\r\n" \ |
noutram | 0:5552cdb52638 | 18 | " </body>" "\r\n" \ |
noutram | 3:c62ab6a06986 | 19 | "</html>" "\r\n" |
noutram | 0:5552cdb52638 | 20 | |
noutram | 0:5552cdb52638 | 21 | #define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n" \ |
noutram | 0:5552cdb52638 | 22 | HTTP_HEADER_FIELDS "\r\n" \ |
noutram | 0:5552cdb52638 | 23 | "\r\n" \ |
noutram | 0:5552cdb52638 | 24 | HTTP_MESSAGE_BODY "\r\n" |
noutram | 3:c62ab6a06986 | 25 | #define HTTP_TITLE "<head><title> Plymouth Uni Weather Page </title></head>" "\r\n" |
noutram | 3:c62ab6a06986 | 26 | #define HTTP_FORMAT_1 "<body style=\"display:flex;text-align:center\">" "\r\n" \ |
noutram | 3:c62ab6a06986 | 27 | "<div style=\"margin:auto\">" "\r\n" |
noutram | 3:c62ab6a06986 | 28 | #define HTTP_BOTTOM "</html>" "\r\n" |
noutram | 0:5552cdb52638 | 29 | |
noutram | 0:5552cdb52638 | 30 | #define IP "10.0.0.10" |
noutram | 3:c62ab6a06986 | 31 | #define NETMASK "255.255.255.0" |
noutram | 0:5552cdb52638 | 32 | #define GATEWAY "10.0.0.1" |
noutram | 0:5552cdb52638 | 33 | |
noutram | 0:5552cdb52638 | 34 | |
noutram | 3:c62ab6a06986 | 35 | //const char *const myHTTP = HTTP_RESPONSE; //This if you wish to set above Compiler defines into Flash Silicon |
noutram | 3:c62ab6a06986 | 36 | |
noutram | 3:c62ab6a06986 | 37 | void ReadFlag_ISR(void); |
noutram | 3:c62ab6a06986 | 38 | bool ReadFlag=true; |
noutram | 3:c62ab6a06986 | 39 | |
noutram | 0:5552cdb52638 | 40 | int main() |
noutram | 0:5552cdb52638 | 41 | { |
noutram | 3:c62ab6a06986 | 42 | printf("\r\nBasic HTTP server example\r\n"); |
noutram | 3:c62ab6a06986 | 43 | |
noutram | 3:c62ab6a06986 | 44 | //generic error code handle |
noutram | 3:c62ab6a06986 | 45 | int err=0; |
noutram | 3:c62ab6a06986 | 46 | |
noutram | 3:c62ab6a06986 | 47 | // Ethernet Interface |
noutram | 0:5552cdb52638 | 48 | EthernetInterface eth; |
noutram | 3:c62ab6a06986 | 49 | eth.set_network(IP, NETMASK, GATEWAY); // comment this line out if you wish to use DHCP |
noutram | 0:5552cdb52638 | 50 | eth.connect(); |
noutram | 3:c62ab6a06986 | 51 | printf("\r\nThe target IP address is '%s'\r\n", eth.get_ip_address()); |
noutram | 3:c62ab6a06986 | 52 | |
noutram | 3:c62ab6a06986 | 53 | // Use to be :- TCPServer srv;!!! |
noutram | 3:c62ab6a06986 | 54 | TCPSocket srv; |
noutram | 3:c62ab6a06986 | 55 | TCPSocket *clt_sock; |
noutram | 3:c62ab6a06986 | 56 | |
noutram | 3:c62ab6a06986 | 57 | // Open the server on ethernet stack |
noutram | 0:5552cdb52638 | 58 | srv.open(ð); |
noutram | 3:c62ab6a06986 | 59 | |
noutram | 3:c62ab6a06986 | 60 | // BIND the server to the HTTP port (TCP 80) |
noutram | 3:c62ab6a06986 | 61 | err=srv.bind(eth.get_ip_address(), 80); |
noutram | 3:c62ab6a06986 | 62 | |
noutram | 3:c62ab6a06986 | 63 | printf("IP Address confirm at : %s\n\r",eth.get_ip_address()); |
noutram | 3:c62ab6a06986 | 64 | |
noutram | 3:c62ab6a06986 | 65 | if(err==0) { |
noutram | 3:c62ab6a06986 | 66 | printf("Bind OK\n\r"); |
noutram | 3:c62ab6a06986 | 67 | } else { |
noutram | 3:c62ab6a06986 | 68 | printf("Bind error=%d\n\r",err); |
noutram | 3:c62ab6a06986 | 69 | } |
noutram | 3:c62ab6a06986 | 70 | |
noutram | 3:c62ab6a06986 | 71 | // LISTEN Can handle 5 simultaneous connections |
noutram | 3:c62ab6a06986 | 72 | err=srv.listen(5); |
noutram | 3:c62ab6a06986 | 73 | if(err==0) { |
noutram | 3:c62ab6a06986 | 74 | printf("Listening OK\n\r"); |
noutram | 3:c62ab6a06986 | 75 | } else { |
noutram | 3:c62ab6a06986 | 76 | printf("Listen error=%d\n\r",err); |
noutram | 3:c62ab6a06986 | 77 | } |
noutram | 3:c62ab6a06986 | 78 | |
noutram | 0:5552cdb52638 | 79 | while (true) { |
noutram | 3:c62ab6a06986 | 80 | // ACCEPT Accepting connections now..... |
noutram | 3:c62ab6a06986 | 81 | clt_sock=srv.accept(); |
noutram | 3:c62ab6a06986 | 82 | |
noutram | 3:c62ab6a06986 | 83 | printf("Waiting.....\n\r"); |
noutram | 3:c62ab6a06986 | 84 | |
noutram | 3:c62ab6a06986 | 85 | // the rest of this line to use Flash Silicon *see notes above line number 35" myHTTP,strlen(myHTTP)); |
noutram | 3:c62ab6a06986 | 86 | printf("%s STRING LENGTH is:%d\n\r",HTTP_RESPONSE,strlen(HTTP_RESPONSE)); |
noutram | 3:c62ab6a06986 | 87 | |
noutram | 3:c62ab6a06986 | 88 | //myHTTP,mydatasize)the rest of this line to use Flash Silicon *see notes above line number 35" myHTTP,strlen(myHTTP)); |
noutram | 3:c62ab6a06986 | 89 | clt_sock->send(HTTP_RESPONSE, strlen(HTTP_RESPONSE)); |
noutram | 3:c62ab6a06986 | 90 | if(err==0) { |
noutram | 3:c62ab6a06986 | 91 | printf("Sent OK!\n\r"); |
noutram | 3:c62ab6a06986 | 92 | } else { |
noutram | 3:c62ab6a06986 | 93 | printf("Send error=%d\n\r",err); |
noutram | 3:c62ab6a06986 | 94 | } |
noutram | 3:c62ab6a06986 | 95 | |
noutram | 3:c62ab6a06986 | 96 | clt_sock->close(); |
noutram | 3:c62ab6a06986 | 97 | wait_us(1000000);//Delay 1 second |
noutram | 0:5552cdb52638 | 98 | } |
noutram | 0:5552cdb52638 | 99 | } |