fuck this
Dependencies: BMP280
WebUI.cpp@25:a2aedb498b27, 2018-01-10 (annotated)
- Committer:
- mwthewsey
- Date:
- Wed Jan 10 03:57:59 2018 +0000
- Revision:
- 25:a2aedb498b27
- Parent:
- 21:6e733076f49c
Final Submission
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Swaggie | 2:5a38ae8459d5 | 1 | #if !FEATURE_LWIP |
mwthewsey | 13:41c394fa932c | 2 | #error [NOT_SUPPORTED] LWIP not supported for this target |
Swaggie | 2:5a38ae8459d5 | 3 | #endif |
Swaggie | 2:5a38ae8459d5 | 4 | //We need to look into what if statements like the one above do. |
Swaggie | 2:5a38ae8459d5 | 5 | #include "WebUI.h" |
Swaggie | 2:5a38ae8459d5 | 6 | #include "mbed.h" |
mwthewsey | 13:41c394fa932c | 7 | #include "Sampling.h" |
Swaggie | 2:5a38ae8459d5 | 8 | |
mwthewsey | 13:41c394fa932c | 9 | Thread WebThread; |
mwthewsey | 9:ac5673cca703 | 10 | //Now setup a web server |
mwthewsey | 9:ac5673cca703 | 11 | TCPServer srv; //TCP/IP Server |
mwthewsey | 9:ac5673cca703 | 12 | TCPSocket clt_sock; //Socket for communication |
mwthewsey | 9:ac5673cca703 | 13 | SocketAddress clt_addr; //Address of incoming connection |
mwthewsey | 13:41c394fa932c | 14 | EthernetInterface eth; |
mwthewsey | 13:41c394fa932c | 15 | using namespace std; |
mwthewsey | 9:ac5673cca703 | 16 | |
Swaggie | 2:5a38ae8459d5 | 17 | #define HTTP_STATUS_LINE "HTTP/1.0 200 OK" |
Swaggie | 2:5a38ae8459d5 | 18 | #define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8" |
Swaggie | 2:5a38ae8459d5 | 19 | #define HTTP_MESSAGE_BODY1 "" \ |
Swaggie | 2:5a38ae8459d5 | 20 | "<html>" "\r\n" \ |
mwthewsey | 13:41c394fa932c | 21 | "<title>Enviromental Readings</title>" \ |
Swaggie | 2:5a38ae8459d5 | 22 | " <body style=\"display:flex;text-align:center\">" "\r\n" \ |
Swaggie | 2:5a38ae8459d5 | 23 | " <div style=\"margin:auto\">" "\r\n" \ |
mwthewsey | 13:41c394fa932c | 24 | " <h1>Most Recent Readings:</h1>" "\r\n" \ |
mwthewsey | 13:41c394fa932c | 25 | " <p><b>Time Taken:</b> " |
Swaggie | 2:5a38ae8459d5 | 26 | |
Swaggie | 2:5a38ae8459d5 | 27 | #define HTTP_MESSAGE_BODY2 "" \ |
Swaggie | 2:5a38ae8459d5 | 28 | "</p>" "\r\n" \ |
Swaggie | 2:5a38ae8459d5 | 29 | " </div>" "\r\n" \ |
Swaggie | 2:5a38ae8459d5 | 30 | " </body>" "\r\n" \ |
Swaggie | 2:5a38ae8459d5 | 31 | "</html>" |
Swaggie | 2:5a38ae8459d5 | 32 | |
Swaggie | 2:5a38ae8459d5 | 33 | #define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n" \ |
Swaggie | 2:5a38ae8459d5 | 34 | HTTP_HEADER_FIELDS "\r\n" \ |
Swaggie | 2:5a38ae8459d5 | 35 | "\r\n" \ |
Swaggie | 2:5a38ae8459d5 | 36 | HTTP_MESSAGE_BODY "\r\n" |
Swaggie | 2:5a38ae8459d5 | 37 | |
Swaggie | 2:5a38ae8459d5 | 38 | #define IP "10.0.0.10" |
Swaggie | 2:5a38ae8459d5 | 39 | #define NETMASK "255.0.0.0" |
Swaggie | 2:5a38ae8459d5 | 40 | #define GATEWAY "10.0.0.1" |
Swaggie | 2:5a38ae8459d5 | 41 | |
mwthewsey | 13:41c394fa932c | 42 | void WebUISetup(void) |
mwthewsey | 13:41c394fa932c | 43 | { |
Swaggie | 2:5a38ae8459d5 | 44 | //Configure an ethernet connection |
Swaggie | 2:5a38ae8459d5 | 45 | eth.set_network(IP, NETMASK, GATEWAY); |
Swaggie | 2:5a38ae8459d5 | 46 | eth.connect(); |
mwthewsey | 21:6e733076f49c | 47 | |
mwthewsey | 21:6e733076f49c | 48 | PC.printf("The target IP address is '%s'\n\r", eth.get_ip_address()); |
mwthewsey | 21:6e733076f49c | 49 | LogEvent(Log_EthConfig); |
mwthewsey | 13:41c394fa932c | 50 | |
Swaggie | 2:5a38ae8459d5 | 51 | /* Open the server on ethernet stack */ |
Swaggie | 2:5a38ae8459d5 | 52 | srv.open(ð); |
mwthewsey | 13:41c394fa932c | 53 | |
Swaggie | 2:5a38ae8459d5 | 54 | /* Bind the HTTP port (TCP 80) to the server */ |
Swaggie | 2:5a38ae8459d5 | 55 | srv.bind(eth.get_ip_address(), 80); |
mwthewsey | 13:41c394fa932c | 56 | |
Swaggie | 2:5a38ae8459d5 | 57 | /* Can handle 5 simultaneous connections */ |
Swaggie | 2:5a38ae8459d5 | 58 | srv.listen(5); |
mwthewsey | 13:41c394fa932c | 59 | WebThread.start(&WebUIUpdate); |
Swaggie | 2:5a38ae8459d5 | 60 | } |
Swaggie | 2:5a38ae8459d5 | 61 | |
mwthewsey | 13:41c394fa932c | 62 | void WebUIUpdate(void) |
mwthewsey | 13:41c394fa932c | 63 | { |
Swaggie | 2:5a38ae8459d5 | 64 | while (true) { |
Swaggie | 2:5a38ae8459d5 | 65 | //Block and wait on an incoming connection |
Swaggie | 2:5a38ae8459d5 | 66 | srv.accept(&clt_sock, &clt_addr); |
mwthewsey | 21:6e733076f49c | 67 | |
mwthewsey | 21:6e733076f49c | 68 | LogEvent(Log_EthRequest); //Request recived |
mwthewsey | 13:41c394fa932c | 69 | |
mwthewsey | 13:41c394fa932c | 70 | //This string will hold the HTML that will be sent to the browser |
Swaggie | 2:5a38ae8459d5 | 71 | string response; |
mwthewsey | 13:41c394fa932c | 72 | //This will be a concatenation of several lines for each reading |
mwthewsey | 13:41c394fa932c | 73 | char time_str[64]; |
mwthewsey | 13:41c394fa932c | 74 | char temp_str[64]; |
mwthewsey | 13:41c394fa932c | 75 | char pres_str[64]; |
Swaggie | 2:5a38ae8459d5 | 76 | char ldr_str[64]; |
mwthewsey | 13:41c394fa932c | 77 | |
mwthewsey | 13:41c394fa932c | 78 | tm T = ReturnDateTimeStruct(timeReadings[currentIndex]); //Convert time int to structure |
mwthewsey | 13:41c394fa932c | 79 | |
mwthewsey | 13:41c394fa932c | 80 | //Fill each string with values |
mwthewsey | 13:41c394fa932c | 81 | sprintf(time_str,"%4d/%2d/%2d %2d:%2d:%2d</p>\r\n",T.tm_year,T.tm_mon,T.tm_mday,T.tm_hour,T.tm_min,T.tm_sec); |
mwthewsey | 13:41c394fa932c | 82 | sprintf(temp_str,"<p><b>Temperature:</b> %5.1f C </p>\r\n", tempReadings[currentIndex]); |
mwthewsey | 13:41c394fa932c | 83 | sprintf(pres_str,"<p><b>Pressure:</b> %5.3f mBar </p>\r\n",presReadings[currentIndex]); |
mwthewsey | 13:41c394fa932c | 84 | sprintf(ldr_str, "<p><b>LDR:</b> %5.3f </p>\r\n", LDRReadings[currentIndex]); |
mwthewsey | 13:41c394fa932c | 85 | |
mwthewsey | 13:41c394fa932c | 86 | |
Swaggie | 2:5a38ae8459d5 | 87 | //Build the C++ string response |
Swaggie | 2:5a38ae8459d5 | 88 | response = HTTP_MESSAGE_BODY1; |
mwthewsey | 13:41c394fa932c | 89 | response += time_str; |
mwthewsey | 13:41c394fa932c | 90 | response += temp_str; |
mwthewsey | 13:41c394fa932c | 91 | response += pres_str; |
Swaggie | 2:5a38ae8459d5 | 92 | response += ldr_str; |
Swaggie | 2:5a38ae8459d5 | 93 | response += HTTP_MESSAGE_BODY2; |
mwthewsey | 13:41c394fa932c | 94 | |
Swaggie | 2:5a38ae8459d5 | 95 | //Send static HTML response (as a C string) |
mwthewsey | 13:41c394fa932c | 96 | clt_sock.send(response.c_str(), response.size()+6); |
Swaggie | 2:5a38ae8459d5 | 97 | } |
Swaggie | 2:5a38ae8459d5 | 98 | } |