SEND
Fork of Final351CW_FINAL by
Embed:
(wiki syntax)
Show/hide line numbers
network.cpp
00001 /* ELEC351 COURSEWORK 2018 00002 DESIGNED USING MBED ONLINE COMPILER IMPORTED TO KEIL 00003 LIAM GRAZIER // DOUG TILLEY // ALEX BARON 00004 */ 00005 #if !FEATURE_LWIP 00006 #error [NOT_SUPPORTED] LWIP not supported for this target 00007 #endif 00008 #include "mbed.h" 00009 #include "EthernetInterface.h" 00010 #include "TCPServer.h" 00011 #include "TCPSocket.h" 00012 char realtime[32]; //char for storing the RTC 00013 #include <iostream> 00014 #include <string> 00015 #include "BMP280.h" 00016 #include "components.hpp" 00017 #include "network.hpp" 00018 Mutex Net; //mutex lock for the network. 00019 #define HTTP_STATUS_LINE "HTTP/1.0 200 OK" 00020 #define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8" 00021 #define HTTP_MESSAGE_BODY1 "" \ 00022 "<html>" "\r\n" \ 00023 " <body style=\"display:flex;text-align:center\">" "\r\n" \ 00024 " <div style=\"margin:auto\">" "\r\n" \ 00025 " <h1>Sensor Data</h1>" "\r\n" \ 00026 " <p>Light Level LDR value: " "\r\n" 00027 00028 #define HTTP_Pressure1 "" \ 00029 "<html>" "\r\n" \ 00030 " <body style=\"display:flex;text-align:center\">" "\r\n" \ 00031 " <div style=\"margin:auto\">" "\r\n" \ 00032 " <h1> </h1>" "\r\n" \ 00033 " <p>Pressure(mbar): " "\r\n" 00034 00035 #define HTTP_Pressure2 "" \ 00036 "</p>" "\r\n" \ 00037 " </div>" "\r\n" \ 00038 " </body>" "\r\n" \ 00039 "</html>" 00040 #define HTTP_TIME "" \ 00041 "<html>" "\r\n" \ 00042 " <body style=\"display:flex;text-align:center\">" "\r\n" \ 00043 " <div style=\"margin:auto\">" "\r\n" \ 00044 " <h1> </h1>" "\r\n" \ 00045 " <p>Time: " "\r\n" 00046 00047 #define HTTP_Temperature1 "" \ 00048 "<html>" "\r\n" \ 00049 " <body style=\"display:flex;text-align:center\">" "\r\n" \ 00050 " <div style=\"margin:auto\">" "\r\n" \ 00051 " <h1> </h1>" "\r\n" \ 00052 " <p>Temperature(Degrees Celcius): " "\r\n" 00053 00054 #define HTTP_Temperature2 "" \ 00055 "</p>" "\r\n" \ 00056 " </div>" "\r\n" \ 00057 " </body>" "\r\n" \ 00058 "</html>" 00059 #define HTTP_MESSAGE_TIME "" \ 00060 "</p>" "\r\n" \ 00061 " </div>" "\r\n" \ 00062 " </body>" "\r\n" \ 00063 "</html>" 00064 #define HTTP_MESSAGE_BODY2 "" \ 00065 "</p>" "\r\n" \ 00066 " </div>" "\r\n" \ 00067 " </body>" "\r\n" \ 00068 "</html>" 00069 00070 00071 #define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n" \ 00072 HTTP_HEADER_FIELDS "\r\n" \ 00073 "\r\n" \ 00074 HTTP_MESSAGE_BODY "\r\n" 00075 00076 #define IP "10.0.0.10" //ipaddress 00077 #define NETMASK "255.0.0.0" //subnetmask 00078 #define GATEWAY "10.0.0.1" //defaultgateway 00079 void dispstralltime() //function for getting time from the RTC 00080 { 00081 time_t seconds = time(NULL); 00082 strftime(realtime, 32, "%c\n\r", localtime(&seconds)); //realtime value = yyyy/mm/dd/hh/mm/ss 00083 } 00084 void networksend(void) 00085 { 00086 // interrupt routine setup 00087 Net.lock(); 00088 printf("Network Enabled\n\r");//here to show the user in terminal network is initalised 00089 //Configure an ethernet connection 00090 EthernetInterface eth; 00091 eth.set_network(IP, NETMASK, GATEWAY); 00092 eth.connect(); 00093 Net.unlock(); 00094 //Now setup a web server 00095 TCPServer srv; //TCP/IP Server 00096 TCPSocket clt_sock; //Socket for communication 00097 SocketAddress clt_addr; //Address of incoming connection 00098 /* Open the server on ethernet stack */ 00099 srv.open(ð); 00100 /* Bind the HTTP port (TCP 80) to the server */ 00101 srv.bind(eth.get_ip_address(), 80); 00102 /* Can handle 5 simultaneous connections */ 00103 srv.listen(5); 00104 while (true) 00105 { 00106 Net.lock(); 00107 using namespace std; 00108 //Block and wait on an incoming connection 00109 srv.accept(&clt_sock, &clt_addr); 00110 //printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port()); 00111 double temp = sensor.getTemperature(); 00112 double pres = sensor.getPressure(); 00113 //Uses a C++ string to make it easier to concatinate 00114 string response; 00115 //Chars for storing the variables 00116 char adcIn_str[64]; 00117 char pres_str[64]; 00118 char temp_str[64]; 00119 dispstralltime(); //calltime 00120 //Read the LDR value 00121 //declairing floats for storing data 00122 float u = adcIn; 00123 float b = pres; 00124 float a = temp; 00125 //Converterting the floats to chars ready for print 00126 sprintf(adcIn_str, "%5.3f", u ); 00127 sprintf(pres_str, "%4.2f", b); 00128 sprintf(temp_str, "%3.1f", a); 00129 //Build the C++ string response 00130 response += HTTP_TIME; 00131 response += realtime; //write time 00132 response += HTTP_MESSAGE_BODY1; 00133 response += adcIn_str; //writeadc 00134 response += HTTP_Temperature1; 00135 response += temp_str; //writetemp 00136 response += HTTP_Temperature2; 00137 response += HTTP_Pressure1; 00138 response += pres_str; //writepressure 00139 response += HTTP_Pressure2; 00140 response += HTTP_MESSAGE_TIME; 00141 response += HTTP_MESSAGE_BODY2; 00142 //Send static HTML response (as a C string) 00143 clt_sock.send(response.c_str(), response.size()+6); 00144 Net.unlock(); 00145 Thread::signal_wait(SIG_NET);//thead signal triggered by ticket in main. 00146 } 00147 }
Generated on Mon Jul 18 2022 05:40:54 by
1.7.2
