Webserver example for Nuvoton NuMaker boards and mbed OS 5.15 - HTTP 1.1 and multi-threaded.
For Mbed OS 6, please use the NuMaker-simple-httpd example.
This application demonstrates how to run an HTTP server on an mbed OS 5 device. It is derived from http-webserver-example Request parsing is done through nodejs/http-parser.
Fixed for Mbed OS 5.15 or later
Tested on
NuMaker IoT-M487 with Ethernet NuMaker PFM-M487 with Ethernet
Diff: source/main.cpp
- Revision:
- 2:ff1a293c4df3
- Parent:
- 0:41f820ea137a
--- a/source/main.cpp Mon Jul 31 17:12:21 2017 +0200 +++ b/source/main.cpp Mon Jun 29 08:17:16 2020 +0000 @@ -1,9 +1,9 @@ #include "mbed.h" -#include "easy-connect.h" #include "http_server.h" #include "http_response_builder.h" -Serial pc(USBTX, USBRX); +#define USE_FIXED_IP 1 // 1 for fixed IP, 0 for DHCP + DigitalOut led(LED1); // Requests come in here @@ -40,24 +40,35 @@ } int main() { - pc.baud(115200); // Connect to the network (see mbed_app.json for the connectivity method used) - NetworkInterface* network = easy_connect(true); + NetworkInterface *network = NetworkInterface::get_default_instance(); if (!network) { printf("Cannot connect to the network, see serial output\n"); return 1; } +#if USE_FIXED_IP + // This fixed IP address is convenient to test with Windows PC + SocketAddress ip_addr("169.254.108.2"); + SocketAddress ip_mask("255.255.0.0"); + SocketAddress ip_gwaddr("169.254.108.1"); + network->set_network(ip_addr, ip_mask, ip_gwaddr); +#endif + + network->connect(); + HttpServer server(network); - nsapi_error_t res = server.start(8080, &request_handler); + nsapi_error_t res = server.start(80, &request_handler); if (res == NSAPI_ERROR_OK) { - printf("Server is listening at http://%s:8080\n", network->get_ip_address()); + SocketAddress ip_addr; + network->get_ip_address(&ip_addr); + printf("Server is listening at http://%s\n", ip_addr.get_ip_address()); } else { printf("Server could not be started... %d\n", res); } - wait(osWaitForever); + while(1) ThisThread::sleep_for(1000000); }