ELEC350 - Team Q / Mbed OS Task671

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #if !FEATURE_LWIP
00002     #error [NOT_SUPPORTED] LWIP not supported for this target
00003 #endif
00004 
00005 #include "mbed.h"
00006 #include "EthernetInterface.h"
00007 #include "TCPServer.h"
00008 #include "TCPSocket.h"
00009 #include <iostream>
00010 #include <string> 
00011 
00012 #define HTTP_STATUS_LINE "HTTP/1.0 200 OK"
00013 #define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8"
00014 #define HTTP_MESSAGE_BODY1 ""                                    \
00015 "<html>" "\r\n"                                                  \
00016 "  <body style=\"display:flex;text-align:center\">" "\r\n"       \
00017 "    <div style=\"margin:auto\">" "\r\n"                         \
00018 "      <h1>Team-Q Elec351</h1>" "\r\n"                           \
00019 "       <p>Time : "                                              \
00020 
00021 #define HTTP_MESSAGE_BODY2 ""                                    \
00022         "&nbsp Date : "                                          \
00023 
00024 #define HTTP_MESSAGE_BODY3 ""                                    \
00025        "</p>" "\r\n"                                             \
00026 "      <p>Light levels are : "   "\r\n"                          \
00027 
00028 #define HTTP_MESSAGE_BODY4 ""                                    \
00029         "</p>" "\r\n"                                            \
00030 "       <p>Current Temperature : " "\r\n"                        \
00031         
00032 #define HTTP_MESSAGE_BODY5 ""                                    \
00033         "</p>" "\r\n"                                            \
00034 "       <p>Current Pressure : " "\r\n"                           \
00035         
00036 #define HTTP_MESSAGE_BODY6 ""                                    \
00037        "</p>" "\r\n"                                             \
00038 "    </div>" "\r\n"                                              \
00039 "  </body>" "\r\n"                                               \
00040 "</html>"
00041 
00042 #define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n"   \
00043                       HTTP_HEADER_FIELDS "\r\n" \
00044                       "\r\n"                    \
00045                       HTTP_MESSAGE_BODY "\r\n"
00046 
00047 #define IP        "10.0.0.10"
00048 #define NETMASK   "255.0.0.0"
00049 #define GATEWAY   "10.0.0.1"
00050 
00051 AnalogIn ldr(PA_0);
00052 
00053 
00054 int main()
00055 {
00056     printf("Basic HTTP server example\n");
00057     
00058     //Configure an ethernet connection
00059     EthernetInterface eth;
00060     eth.set_network(IP, NETMASK, GATEWAY);
00061     eth.connect();
00062     printf("The target IP address is '%s'\n", eth.get_ip_address());
00063     
00064     //Now setup a web server
00065     TCPServer srv;           //TCP/IP Server
00066     TCPSocket clt_sock;      //Socket for communication
00067     SocketAddress clt_addr;  //Address of incoming connection
00068     
00069     /* Open the server on ethernet stack */
00070     srv.open(&eth);
00071     
00072     /* Bind the HTTP port (TCP 80) to the server */
00073     srv.bind(eth.get_ip_address(), 80);
00074     
00075     /* Can handle 5 simultaneous connections */
00076     srv.listen(5);
00077     
00078     while (true) {
00079         using namespace std;
00080         //Block and wait on an incoming connection
00081         srv.accept(&clt_sock, &clt_addr);
00082         printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port());
00083         
00084         //Uses a C++ string to make it easier to concatinate
00085         //"response" is the string that makes up the whole web page
00086         string response;
00087         //This is a C string
00088         //make space to save variables as strings
00089         char time_str[64];
00090         char date_str[64];
00091         char ldr_str[64];
00092         char temp_str[64];
00093         char pres_str[64];
00094         
00095         //Read the time/date/sensor values
00096         //use sensor readings instead of 10's
00097         float a = 10;
00098         float e = 10;
00099         float i = ldr;
00100         float o = 10;
00101         float u = 10;
00102         
00103         //Convert to a C String
00104         sprintf(time_str, "%5.3f", a );
00105         sprintf(date_str, "%5.3f", e );
00106         sprintf(ldr_str, "%5.3f", i );
00107         sprintf(temp_str, "%5.3f", o );
00108         sprintf(pres_str, "%5.3f", u );
00109         
00110         //Build the C++ string response
00111         response = HTTP_MESSAGE_BODY1;
00112         response += time_str;
00113         response += HTTP_MESSAGE_BODY2;
00114         response += date_str;
00115         response += HTTP_MESSAGE_BODY3;
00116         response += ldr_str;
00117         response += HTTP_MESSAGE_BODY4;
00118         response += temp_str;
00119         response += HTTP_MESSAGE_BODY5;
00120         response += pres_str;
00121         response += HTTP_MESSAGE_BODY6;
00122                 
00123         //Send static HTML response (as a C string)
00124         clt_sock.send(response.c_str(), response.size()+6);    
00125     }
00126 }