something

Dependencies:   BMP280 Task671-mbedos-FZ429-TCP-dynamic

Fork of Task671-mbedos-FZ429-TCP-dynamic by University of Plymouth - Stages 1, 2 and 3

Committer:
noutram
Date:
Tue Nov 21 14:21:48 2017 +0000
Revision:
1:76bd6f78cabc
Parent:
0:65ff7ad381e8
Added annotation

Who changed what in which revision?

UserRevisionLine numberNew 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 1:76bd6f78cabc 19 " <p>The LDR 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 1:76bd6f78cabc 36 AnalogIn ldr(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(&eth);
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 1:76bd6f78cabc 69 //Uses a C++ string to make it easier to concatinate
noutram 0:65ff7ad381e8 70 string response;
noutram 1:76bd6f78cabc 71 //This is a C string
noutram 1:76bd6f78cabc 72 char ldr_str[64];
noutram 1:76bd6f78cabc 73
noutram 1:76bd6f78cabc 74 //Read the LDR value
noutram 1:76bd6f78cabc 75 float u = ldr;
noutram 0:65ff7ad381e8 76
noutram 1:76bd6f78cabc 77 //Convert to a C String
noutram 1:76bd6f78cabc 78 sprintf(ldr_str, "%5.3f", u );
noutram 1:76bd6f78cabc 79 printf("LDR: %5.3f\n\r", u);
noutram 1:76bd6f78cabc 80
noutram 1:76bd6f78cabc 81 //Build the C++ string response
noutram 0:65ff7ad381e8 82 response = HTTP_MESSAGE_BODY1;
noutram 1:76bd6f78cabc 83 response += ldr_str;
noutram 0:65ff7ad381e8 84 response += HTTP_MESSAGE_BODY2;
noutram 0:65ff7ad381e8 85
noutram 1:76bd6f78cabc 86 //Send static HTML response (as a C string)
noutram 0:65ff7ad381e8 87 clt_sock.send(response.c_str(), response.size()+6);
noutram 0:65ff7ad381e8 88 }
noutram 0:65ff7ad381e8 89 }