Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BMP280 ELEC350-Practicals-FZ429 TextLCD BME280 ntp-client
Network.hpp
00001 #ifndef _NETWORK_ 00002 00003 #define _NETWORK_ 00004 00005 #include "mbed.h" 00006 00007 #include "FATFileSystem.h" 00008 00009 #include "sample_hardware.hpp" 00010 00011 #include "EthernetInterface.h" 00012 00013 #include "TCPServer.h" 00014 00015 //#include "messageStruct.hpp" 00016 00017 //#include "LCD.hpp" 00018 00019 #include "TCPSocket.h" 00020 00021 #include <iostream> 00022 00023 #include <string> 00024 00025 #include "ntp-client/NTPClient.h" 00026 00027 00028 00029 00030 00031 00032 #define HTTP_STATUS_LINE "HTTP/1.0 200 OK" 00033 #define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8" 00034 #define HTTP_MESSAGE_BODY1 "" \ 00035 "<html>" "\r\n" \ 00036 " <body style=\"display:flex;text-align:center\">" "\r\n" \ 00037 " <div id=\"Sensor Data\" name=\"Sensor Date\" style=\"margin:auto\">" "\r\n" \ 00038 " <h1> <b>Sensor Data</b> </h1>" "\r\n" \ 00039 " <p>The LDR value is " 00040 00041 #define HTTP_MESSAGE_BODY2 "" \ 00042 " Lux </p>" "\r\n" \ 00043 " <div style=\"margin:auto\">" "\r\n" \ 00044 " <p>The Temperature value is " 00045 00046 #define HTTP_MESSAGE_BODY3 "" \ 00047 " Degrees</p>" "\r\n" \ 00048 " <div style=\"margin:auto\">" "\r\n" \ 00049 " <p>The Pressure value is " 00050 00051 #define HTTP_MESSAGE_BODY4 "" \ 00052 " mbar </p>" "\r\n" \ 00053 " <div style=\"margin:auto\">" "\r\n" \ 00054 " <p>The current Time is " 00055 00056 #define HTTP_MESSAGE_BODY5 "" \ 00057 " </p>" "\r\n" \ 00058 " </div>" "\r\n" \ 00059 " </body>" "\r\n" \ 00060 "</html>" 00061 00062 00063 00064 00065 00066 00067 #define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n" \ 00068 HTTP_HEADER_FIELDS "\r\n" \ 00069 "\r\n" \ 00070 HTTP_MESSAGE_BODY "\r\n" 00071 00072 00073 00074 #define IP "10.0.0.10" 00075 00076 #define NETMASK "255.0.0.0" 00077 00078 #define GATEWAY "10.0.0.2" 00079 00080 EthernetInterface eth; 00081 00082 NTPClient ntp(ð); 00083 00084 00085 00086 00087 class Network 00088 { 00089 //friend class LCD_Data; 00090 private: 00091 float temp; //current temperature of sensor 00092 float pressure; //current pressure of sensor 00093 float fLDR; //current light level from LDR 00094 string time; 00095 00096 void update_temp(double t) //use this function to update the current temperature value 00097 { 00098 temp = t; 00099 } 00100 00101 void update_pressure(double p) //use this function to update the current pressure value 00102 { 00103 pressure = p; 00104 } 00105 00106 void update_LDR(double L) 00107 { 00108 fLDR = L; 00109 } 00110 00111 00112 public: 00113 EventQueue Network_Queue; 00114 00115 00116 00117 Network(){ //constructor 00118 00119 //Configure an ethernet connection 00120 eth.set_network(IP, NETMASK, GATEWAY); 00121 eth.connect(); 00122 } 00123 00124 00125 ~Network(){ //deconstructor 00126 00127 } 00128 00129 00130 00131 00132 00133 void update_sensor_info(sample_message msg) //updates all current sensor information, this is called by a ticker every 5 seconds to read from the mailbox 00134 00135 { 00136 update_temp(msg.temp); // Include message class passing of data 00137 update_pressure(msg.pressure); 00138 update_LDR(msg.ldr); 00139 } 00140 00141 00142 void update_Time(string tm) 00143 { 00144 time = tm; 00145 } 00146 00147 00148 void NetPush(){ 00149 00150 00151 //Now setup a web server 00152 00153 TCPServer srv; //TCP/IP Server 00154 TCPSocket clt_sock; //Socket for communication 00155 SocketAddress clt_addr; //Address of incoming connection 00156 00157 00158 00159 00160 /* Open the server on ethernet stack */ 00161 srv.open(ð); 00162 00163 00164 /* Bind the HTTP port (TCP 80) to the server */ 00165 srv.bind(eth.get_ip_address(), 80); 00166 00167 00168 /* Can handle 5 simultaneous connections */ 00169 srv.listen(5); 00170 00171 //Block and wait on an incoming connection 00172 00173 srv.accept(&clt_sock, &clt_addr); 00174 00175 //printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port()); 00176 00177 00178 00179 //Uses a C++ string to make it easier to concatinate 00180 00181 string response; 00182 00183 00184 00185 //This is a C string 00186 00187 char ldr_str[64]; 00188 00189 char temp_str[64]; 00190 00191 char pressure_str[64]; 00192 00193 //printf("%s", time); 00194 00195 //Convert to a C String 00196 00197 sprintf(ldr_str, "%5.3f", fLDR ); 00198 00199 sprintf(temp_str, "%5.3f", temp ); 00200 00201 sprintf(pressure_str, "%5.3f", pressure ); 00202 00203 00204 00205 00206 //Build the C++ string response 00207 00208 response = HTTP_MESSAGE_BODY1; 00209 00210 response += ldr_str; 00211 00212 response += HTTP_MESSAGE_BODY2; 00213 00214 response += temp_str; 00215 00216 response += HTTP_MESSAGE_BODY3; 00217 00218 response += pressure_str; 00219 00220 response += HTTP_MESSAGE_BODY4; 00221 00222 response += time; 00223 00224 response += HTTP_MESSAGE_BODY5; 00225 00226 00227 00228 00229 00230 00231 //Send static HTML response (as a C string) 00232 00233 // clt_sock.send(response.c_str(), response.size()+6); 00234 clt_sock.send(response.c_str(), response.size()); 00235 00236 00237 00238 00239 00240 } 00241 00242 }; 00243 00244 00245 00246 00247 00248 Network m_oNet; 00249 00250 #endif
Generated on Sat Jul 16 2022 02:51:17 by
