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.
main.cpp
00001 //#if !FEATURE_LWIP 00002 // #error [NOT_SUPPORTED] LWIP not supported for this target 00003 //#endif 00004 00005 // ref 00006 // https://os.mbed.com/questions/76794/How-to-set-Static-IP-address-with-Nucleo/ 00007 // https://os.mbed.com/docs/mbed-os/v5.11/apis/ethernet.html 00008 #include "mbed.h" 00009 #include "EthernetInterface.h" 00010 #include "TCPServer.h" 00011 #include "TCPSocket.h" 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_BODY "" \ 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>Finally It works !</p>" "\r\n" \ 00021 " </div>" "\r\n" \ 00022 " </body>" "\r\n" \ 00023 "</html>" 00024 00025 #define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n" \ 00026 HTTP_HEADER_FIELDS "\r\n" \ 00027 "\r\n" \ 00028 HTTP_MESSAGE_BODY "\r\n" 00029 00030 00031 Serial pc(SERIAL_TX, SERIAL_RX); 00032 int main() 00033 { 00034 pc.baud(57600); 00035 00036 pc.printf("Basic HTTP server example\r\n"); 00037 00038 EthernetInterface eth; 00039 eth.set_network("192.168.1.11","255.255.252.0","192.168.1.10"); 00040 //eth.set_network(IP_Adress,GATEWAY,MASK); 00041 eth.connect(); 00042 00043 pc.printf("The target IP address is '%s'\r\n", eth.get_ip_address()); 00044 00045 TCPServer srv; 00046 TCPSocket clt_sock; 00047 SocketAddress clt_addr; 00048 00049 /* Open the server on ethernet stack */ 00050 srv.open(ð); 00051 00052 /* Bind the HTTP port (TCP 22) to the server */ 00053 srv.bind(eth.get_ip_address(), 80); 00054 00055 /* Can handle 5 simultaneous connections */ 00056 srv.listen(5); 00057 00058 char buffer[300]; 00059 00060 while (true) { 00061 srv.accept(&clt_sock, &clt_addr); 00062 pc.printf("accept %s:%d\r\n", clt_addr.get_ip_address(), clt_addr.get_port()); 00063 clt_sock.send(HTTP_RESPONSE, strlen(HTTP_RESPONSE)); 00064 00065 // example to move messages between uart and tcp / ip socket 00066 while (0) { 00067 if (1) { 00068 while (pc.readable()) { 00069 char c = pc.getc(); // Read hyperterminal 00070 buffer[0] = c; 00071 clt_sock.send(buffer, 1); 00072 //echo 00073 pc.printf("%c", c); 00074 } 00075 } else { 00076 // blocking mode 00077 int recdata = clt_sock.recv(buffer,300); 00078 if (recdata >0 ){ 00079 buffer[recdata]=0; 00080 printf("%s %d\r\n", buffer, recdata); 00081 // echo 00082 buffer[recdata]=10; 00083 buffer[recdata+1]=13; 00084 clt_sock.send(buffer, recdata+2); 00085 } 00086 } 00087 } 00088 00089 } 00090 }
Generated on Fri Jul 22 2022 16:57:38 by
1.7.2