something
Dependencies: BMP280 Task671-mbedos-FZ429-TCP-dynamic
Fork of Task671-mbedos-FZ429-TCP-dynamic by
net.h
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 "BMP280.h" 00010 #include <iostream> 00011 #include <string> 00012 00013 #define HTTP_STATUS_LINE "HTTP/1.0 200 OK" 00014 #define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8" 00015 #define HTTP_MESSAGE_BODY1 "" \ 00016 "<html>" "\r\n" \ 00017 " <body style=\"display:flex;text-align:center\">" "\r\n" \ 00018 " <div style=\"margin:auto\">" "\r\n" \ 00019 " <h1>Hello World</h1>" "\r\n" \ 00020 " <p>The LDR value is " 00021 #define HTTP_MESSAGE_BODY2 "" \ 00022 "</p>" "\r\n" \ 00023 " <p1>The Temperature value is" 00024 #define HTTP_MESSAGE_BODY6 "" \ 00025 "</p1>" "\r\n" \ 00026 " <p2>The Pressure value is " 00027 #define HTTP_MESSAGE_BODY4 "" \ 00028 "</p2>" "\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.1" 00039 #define NETMASK "255.0.0.0" 00040 #define GATEWAY "10.0.0.1" 00041 00042 BMP280 bmp(D14,D15,0x76); 00043 AnalogIn ldr(PA_0); 00044 00045 00046 int main() 00047 { 00048 printf("Basic HTTP server example\n"); 00049 00050 //Configure an ethernet connection 00051 EthernetInterface eth; 00052 eth.set_network(IP, NETMASK, GATEWAY); 00053 eth.connect(); 00054 printf("The target IP address is '%s'\n", eth.get_ip_address()); 00055 00056 //Now setup a web server 00057 TCPServer srv; //TCP/IP Server 00058 TCPSocket clt_sock; //Socket for communication 00059 SocketAddress clt_addr; //Address of incoming connection 00060 00061 /* Open the server on ethernet stack */ 00062 srv.open(ð); 00063 00064 /* Bind the HTTP port (TCP 80) to the server */ 00065 srv.bind(eth.get_ip_address(), 80); 00066 00067 /* Can handle 5 simultaneous connections */ 00068 srv.listen(5); 00069 00070 while (true) { 00071 using namespace std; 00072 //Block and wait on an incoming connection 00073 srv.accept(&clt_sock, &clt_addr); 00074 printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port()); 00075 00076 //Uses a C++ string to make it easier to concatinate 00077 string response; 00078 //This is a C string 00079 char ldr_str[64]; 00080 00081 //Read the LDR value 00082 float u = ldr; 00083 00084 //Convert to a C String 00085 sprintf(ldr_str, "%5.3f", u ); 00086 printf("LDR: %5.3f\n\r", u); 00087 00088 string response1,response2; 00089 float tempf = 52, pressuref = 713; 00090 char data_temp[64] , data_press[64]; 00091 //tempf = bmp.getTemperature(); 00092 //pressuref = bmp.getPressure(); 00093 sprintf(data_temp, "%5.3f", tempf ); 00094 sprintf(data_press, "%5.3f", pressuref ); 00095 00096 00097 00098 //Build the C++ string response 00099 response = HTTP_MESSAGE_BODY1; 00100 response += ldr_str; 00101 response += HTTP_MESSAGE_BODY2; 00102 00103 00104 response1 += data_temp; 00105 response1 += HTTP_MESSAGE_BODY6; 00106 00107 00108 response2 += data_press; 00109 response2 += HTTP_MESSAGE_BODY4; 00110 00111 00112 //Send static HTML response (as a C string) 00113 clt_sock.send(response.c_str(), response.size()+6); 00114 clt_sock.send(response1.c_str(), response1.size()+6); 00115 clt_sock.send(response2.c_str(), response2.size()+6); 00116 } 00117 }
Generated on Wed Jul 27 2022 01:52:08 by
1.7.2
