fuck this

Dependencies:   BMP280

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WebUI.cpp Source File

WebUI.cpp

00001 #if !FEATURE_LWIP
00002 #error [NOT_SUPPORTED] LWIP not supported for this target
00003 #endif
00004 //We need to look into what if statements like the one above do.
00005 #include "WebUI.h"
00006 #include "mbed.h"
00007 #include "Sampling.h"
00008 
00009 Thread WebThread;
00010 //Now setup a web server
00011 TCPServer srv;           //TCP/IP Server
00012 TCPSocket clt_sock;      //Socket for communication
00013 SocketAddress clt_addr;  //Address of incoming connection
00014 EthernetInterface eth;
00015 using namespace std;
00016 
00017 #define HTTP_STATUS_LINE "HTTP/1.0 200 OK"
00018 #define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8"
00019 #define HTTP_MESSAGE_BODY1 ""                                    \
00020 "<html>" "\r\n"                                                  \
00021 "<title>Enviromental Readings</title>"               \
00022 "  <body style=\"display:flex;text-align:center\">" "\r\n"       \
00023 "    <div style=\"margin:auto\">" "\r\n"                         \
00024 "      <h1>Most Recent Readings:</h1>" "\r\n"                    \
00025 "      <p><b>Time Taken:</b> "
00026 
00027 #define HTTP_MESSAGE_BODY2 ""                                    \
00028        "</p>" "\r\n"                                             \
00029 "    </div>" "\r\n"                                              \
00030 "  </body>" "\r\n"                                               \
00031 "</html>"
00032 
00033 #define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n"   \
00034                       HTTP_HEADER_FIELDS "\r\n" \
00035                       "\r\n"                    \
00036                       HTTP_MESSAGE_BODY "\r\n"
00037 
00038 #define IP        "10.0.0.10"
00039 #define NETMASK   "255.0.0.0"
00040 #define GATEWAY   "10.0.0.1"
00041 
00042 void WebUISetup(void)
00043 {
00044     //Configure an ethernet connection
00045     eth.set_network(IP, NETMASK, GATEWAY);
00046     eth.connect();
00047     
00048     PC.printf("The target IP address is '%s'\n\r", eth.get_ip_address());
00049     LogEvent(Log_EthConfig);
00050 
00051     /* Open the server on ethernet stack */
00052     srv.open(&eth);
00053 
00054     /* Bind the HTTP port (TCP 80) to the server */
00055     srv.bind(eth.get_ip_address(), 80);
00056 
00057     /* Can handle 5 simultaneous connections */
00058     srv.listen(5);
00059     WebThread.start(&WebUIUpdate);
00060 }
00061 
00062 void WebUIUpdate(void)
00063 {
00064     while (true) {
00065         //Block and wait on an incoming connection
00066         srv.accept(&clt_sock, &clt_addr);
00067         
00068         LogEvent(Log_EthRequest); //Request recived
00069 
00070         //This string will hold the HTML that will be sent to the browser
00071         string response;
00072         //This will be a concatenation of several lines for each reading
00073         char time_str[64];
00074         char temp_str[64];
00075         char pres_str[64];
00076         char ldr_str[64];
00077 
00078         tm T = ReturnDateTimeStruct(timeReadings[currentIndex]);   //Convert time int to structure
00079 
00080         //Fill each string with values
00081         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);
00082         sprintf(temp_str,"<p><b>Temperature:</b> %5.1f C </p>\r\n", tempReadings[currentIndex]);
00083         sprintf(pres_str,"<p><b>Pressure:</b> %5.3f mBar </p>\r\n",presReadings[currentIndex]);
00084         sprintf(ldr_str, "<p><b>LDR:</b> %5.3f </p>\r\n", LDRReadings[currentIndex]);
00085 
00086 
00087         //Build the C++ string response
00088         response = HTTP_MESSAGE_BODY1;
00089         response += time_str;
00090         response += temp_str;
00091         response += pres_str;
00092         response += ldr_str;
00093         response += HTTP_MESSAGE_BODY2;
00094 
00095         //Send static HTML response (as a C string)
00096         clt_sock.send(response.c_str(), response.size()+6);
00097     }
00098 }