Simple TCP/IP Server (dynamic web page)

Dependents:   TASKHTML

Committer:
noutram
Date:
Wed Dec 04 11:15:56 2019 +0000
Revision:
4:094d7d0f9a1f
Parent:
3:265121e8c2cc
Now more responsive

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:65ff7ad381e8 1 #include "mbed.h"
noutram 0:65ff7ad381e8 2 #include "EthernetInterface.h"
noutram 0:65ff7ad381e8 3 #include "TCPServer.h"
noutram 0:65ff7ad381e8 4 #include "TCPSocket.h"
noutram 0:65ff7ad381e8 5 #include <iostream>
noutram 0:65ff7ad381e8 6 #include <string>
noutram 0:65ff7ad381e8 7
noutram 0:65ff7ad381e8 8 #define HTTP_STATUS_LINE "HTTP/1.0 200 OK"
noutram 0:65ff7ad381e8 9 #define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8"
noutram 0:65ff7ad381e8 10 #define HTTP_MESSAGE_BODY1 "" \
noutram 0:65ff7ad381e8 11 "<html>" "\r\n" \
noutram 0:65ff7ad381e8 12 " <body style=\"display:flex;text-align:center\">" "\r\n" \
noutram 0:65ff7ad381e8 13 " <div style=\"margin:auto\">" "\r\n" \
noutram 0:65ff7ad381e8 14 " <h1>Hello World</h1>" "\r\n" \
noutram 1:76bd6f78cabc 15 " <p>The LDR value is "
noutram 0:65ff7ad381e8 16
noutram 0:65ff7ad381e8 17 #define HTTP_MESSAGE_BODY2 "" \
noutram 0:65ff7ad381e8 18 "</p>" "\r\n" \
noutram 0:65ff7ad381e8 19 " </div>" "\r\n" \
noutram 0:65ff7ad381e8 20 " </body>" "\r\n" \
noutram 0:65ff7ad381e8 21 "</html>"
noutram 0:65ff7ad381e8 22
noutram 0:65ff7ad381e8 23 #define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n" \
noutram 0:65ff7ad381e8 24 HTTP_HEADER_FIELDS "\r\n" \
noutram 0:65ff7ad381e8 25 "\r\n" \
noutram 0:65ff7ad381e8 26 HTTP_MESSAGE_BODY "\r\n"
noutram 0:65ff7ad381e8 27
noutram 0:65ff7ad381e8 28 #define IP "10.0.0.10"
noutram 0:65ff7ad381e8 29 #define NETMASK "255.0.0.0"
noutram 0:65ff7ad381e8 30 #define GATEWAY "10.0.0.1"
noutram 0:65ff7ad381e8 31
noutram 1:76bd6f78cabc 32 AnalogIn ldr(PA_0);
noutram 0:65ff7ad381e8 33
noutram 0:65ff7ad381e8 34
noutram 0:65ff7ad381e8 35 int main()
noutram 0:65ff7ad381e8 36 {
noutram 0:65ff7ad381e8 37 printf("Basic HTTP server example\n");
noutram 0:65ff7ad381e8 38
noutram 0:65ff7ad381e8 39 //Configure an ethernet connection
noutram 0:65ff7ad381e8 40 EthernetInterface eth;
noutram 0:65ff7ad381e8 41 eth.set_network(IP, NETMASK, GATEWAY);
noutram 0:65ff7ad381e8 42 eth.connect();
noutram 0:65ff7ad381e8 43 printf("The target IP address is '%s'\n", eth.get_ip_address());
noutram 0:65ff7ad381e8 44
noutram 0:65ff7ad381e8 45 //Now setup a web server
noutram 2:ecf4c78019ab 46 TCPSocket srv; //TCP/IP Server
noutram 3:265121e8c2cc 47 TCPSocket* clt_sock; //Socket for communication
noutram 0:65ff7ad381e8 48 SocketAddress clt_addr; //Address of incoming connection
noutram 0:65ff7ad381e8 49
noutram 0:65ff7ad381e8 50 /* Open the server on ethernet stack */
noutram 0:65ff7ad381e8 51 srv.open(&eth);
noutram 0:65ff7ad381e8 52
noutram 0:65ff7ad381e8 53 /* Bind the HTTP port (TCP 80) to the server */
noutram 0:65ff7ad381e8 54 srv.bind(eth.get_ip_address(), 80);
noutram 0:65ff7ad381e8 55
noutram 0:65ff7ad381e8 56 /* Can handle 5 simultaneous connections */
noutram 0:65ff7ad381e8 57 srv.listen(5);
noutram 0:65ff7ad381e8 58
noutram 0:65ff7ad381e8 59 while (true) {
noutram 0:65ff7ad381e8 60 using namespace std;
noutram 0:65ff7ad381e8 61 //Block and wait on an incoming connection
noutram 3:265121e8c2cc 62 clt_sock=srv.accept();
noutram 0:65ff7ad381e8 63 printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port());
noutram 0:65ff7ad381e8 64
noutram 1:76bd6f78cabc 65 //Uses a C++ string to make it easier to concatinate
noutram 0:65ff7ad381e8 66 string response;
noutram 1:76bd6f78cabc 67 //This is a C string
noutram 1:76bd6f78cabc 68 char ldr_str[64];
noutram 1:76bd6f78cabc 69
noutram 1:76bd6f78cabc 70 //Read the LDR value
noutram 1:76bd6f78cabc 71 float u = ldr;
noutram 0:65ff7ad381e8 72
noutram 1:76bd6f78cabc 73 //Convert to a C String
noutram 1:76bd6f78cabc 74 sprintf(ldr_str, "%5.3f", u );
noutram 1:76bd6f78cabc 75 printf("LDR: %5.3f\n\r", u);
noutram 1:76bd6f78cabc 76
noutram 1:76bd6f78cabc 77 //Build the C++ string response
noutram 0:65ff7ad381e8 78 response = HTTP_MESSAGE_BODY1;
noutram 1:76bd6f78cabc 79 response += ldr_str;
noutram 0:65ff7ad381e8 80 response += HTTP_MESSAGE_BODY2;
noutram 0:65ff7ad381e8 81
noutram 1:76bd6f78cabc 82 //Send static HTML response (as a C string)
noutram 3:265121e8c2cc 83 clt_sock->send(response.c_str(), response.size()+6);
noutram 3:265121e8c2cc 84
noutram 3:265121e8c2cc 85 clt_sock->close();
noutram 4:094d7d0f9a1f 86 wait_us(100000);
noutram 0:65ff7ad381e8 87 }
noutram 0:65ff7ad381e8 88 }