fuck this

Dependencies:   BMP280

Committer:
mwthewsey
Date:
Tue Jan 09 10:25:17 2018 +0000
Revision:
13:41c394fa932c
Parent:
9:ac5673cca703
Child:
21:6e733076f49c
Serial,LCD,Sampling,SD,Web Working.

Who changed what in which revision?

UserRevisionLine numberNew 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 {
mwthewsey 13:41c394fa932c 44 //printf("Basic HTTP server example\n\r");
mwthewsey 13:41c394fa932c 45
Swaggie 2:5a38ae8459d5 46 //Configure an ethernet connection
mwthewsey 13:41c394fa932c 47
Swaggie 2:5a38ae8459d5 48 eth.set_network(IP, NETMASK, GATEWAY);
Swaggie 2:5a38ae8459d5 49 eth.connect();
mwthewsey 13:41c394fa932c 50 //printf("The target IP address is '%s'\n\r", eth.get_ip_address());
mwthewsey 13:41c394fa932c 51
mwthewsey 13:41c394fa932c 52
mwthewsey 13:41c394fa932c 53
Swaggie 2:5a38ae8459d5 54 /* Open the server on ethernet stack */
Swaggie 2:5a38ae8459d5 55 srv.open(&eth);
mwthewsey 13:41c394fa932c 56
Swaggie 2:5a38ae8459d5 57 /* Bind the HTTP port (TCP 80) to the server */
Swaggie 2:5a38ae8459d5 58 srv.bind(eth.get_ip_address(), 80);
mwthewsey 13:41c394fa932c 59
Swaggie 2:5a38ae8459d5 60 /* Can handle 5 simultaneous connections */
Swaggie 2:5a38ae8459d5 61 srv.listen(5);
mwthewsey 13:41c394fa932c 62 WebThread.start(&WebUIUpdate);
Swaggie 2:5a38ae8459d5 63 }
Swaggie 2:5a38ae8459d5 64
mwthewsey 13:41c394fa932c 65 void WebUIUpdate(void)
mwthewsey 13:41c394fa932c 66 {
Swaggie 2:5a38ae8459d5 67 while (true) {
Swaggie 2:5a38ae8459d5 68 //Block and wait on an incoming connection
Swaggie 2:5a38ae8459d5 69 srv.accept(&clt_sock, &clt_addr);
mwthewsey 13:41c394fa932c 70
mwthewsey 13:41c394fa932c 71 //Show that an event has been recieved.
mwthewsey 13:41c394fa932c 72 //printf("accept %s:%d\n\r", clt_addr.get_ip_address(), clt_addr.get_port());
mwthewsey 13:41c394fa932c 73
mwthewsey 13:41c394fa932c 74 //This string will hold the HTML that will be sent to the browser
Swaggie 2:5a38ae8459d5 75 string response;
mwthewsey 13:41c394fa932c 76 //This will be a concatenation of several lines for each reading
mwthewsey 13:41c394fa932c 77 char time_str[64];
mwthewsey 13:41c394fa932c 78 char temp_str[64];
mwthewsey 13:41c394fa932c 79 char pres_str[64];
Swaggie 2:5a38ae8459d5 80 char ldr_str[64];
mwthewsey 13:41c394fa932c 81
mwthewsey 13:41c394fa932c 82 tm T = ReturnDateTimeStruct(timeReadings[currentIndex]); //Convert time int to structure
mwthewsey 13:41c394fa932c 83
mwthewsey 13:41c394fa932c 84 //Fill each string with values
mwthewsey 13:41c394fa932c 85 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 86 sprintf(temp_str,"<p><b>Temperature:</b> %5.1f C </p>\r\n", tempReadings[currentIndex]);
mwthewsey 13:41c394fa932c 87 sprintf(pres_str,"<p><b>Pressure:</b> %5.3f mBar </p>\r\n",presReadings[currentIndex]);
mwthewsey 13:41c394fa932c 88 sprintf(ldr_str, "<p><b>LDR:</b> %5.3f </p>\r\n", LDRReadings[currentIndex]);
mwthewsey 13:41c394fa932c 89
mwthewsey 13:41c394fa932c 90
Swaggie 2:5a38ae8459d5 91 //Build the C++ string response
Swaggie 2:5a38ae8459d5 92 response = HTTP_MESSAGE_BODY1;
mwthewsey 13:41c394fa932c 93 response += time_str;
mwthewsey 13:41c394fa932c 94 response += temp_str;
mwthewsey 13:41c394fa932c 95 response += pres_str;
Swaggie 2:5a38ae8459d5 96 response += ldr_str;
Swaggie 2:5a38ae8459d5 97 response += HTTP_MESSAGE_BODY2;
mwthewsey 13:41c394fa932c 98
Swaggie 2:5a38ae8459d5 99 //Send static HTML response (as a C string)
mwthewsey 13:41c394fa932c 100 clt_sock.send(response.c_str(), response.size()+6);
Swaggie 2:5a38ae8459d5 101 }
Swaggie 2:5a38ae8459d5 102 }