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: BME280 BMP280 TextLCD mbed
Fork of CW_watchdog_09012018_copy 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 "sd.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 //Mutex stdio_mutex; 00045 00046 void network() 00047 { 00048 //printf("Basic HTTP server example\n"); 00049 //Configure an ethernet connection 00050 00051 EthernetInterface eth; 00052 eth.set_network(IP, NETMASK, GATEWAY); 00053 eth.connect(); 00054 //stdio_mutex.lock(); 00055 printf("The target IP address is '%s'\n", eth.get_ip_address()); 00056 //stdio_mutex.unlock(); 00057 //Now setup a web server 00058 TCPServer srv; //TCP/IP Server 00059 TCPSocket clt_sock; //Socket for communication 00060 SocketAddress clt_addr; //Address of incoming connection 00061 00062 /* Open the server on ethernet stack */ 00063 srv.open(ð); 00064 00065 /* Bind the HTTP port (TCP 80) to the server */ 00066 srv.bind(eth.get_ip_address(), 80); 00067 00068 /* Can handle 5 simultaneous connections */ 00069 srv.listen(5); 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 = 7; 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 tempr, pressurer; 00090 tempr = 64; 00091 pressurer = 322; 00092 char data_temp[64] , data_press[64]; 00093 //tempf = bmp.getTemperature(); 00094 //pressuref = bmp.getPressure(); 00095 sprintf(data_temp, "%5.3f", tempr ); 00096 sprintf(data_press, "%5.3f", pressurer ); 00097 00098 00099 00100 //Build the C++ string response 00101 response = HTTP_MESSAGE_BODY1; 00102 response += ldr_str; 00103 response += HTTP_MESSAGE_BODY2; 00104 00105 00106 response1 += data_temp; 00107 response1 += HTTP_MESSAGE_BODY6; 00108 00109 00110 response2 += data_press; 00111 response2 += HTTP_MESSAGE_BODY4; 00112 00113 00114 //Send static HTML response (as a C string) 00115 clt_sock.send(response.c_str(), response.size()+6); 00116 clt_sock.send(response1.c_str(), response1.size()+6); 00117 clt_sock.send(response2.c_str(), response2.size()+6); 00118 //} 00119 }
Generated on Wed Jul 27 2022 14:53:09 by
1.7.2
