Morgan Du / Mbed OS http-webserver-example

Dependencies:   mbed-http

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "http_server.h"
00003 #include "http_response_builder.h"
00004 
00005 #define USE_FIXED_IP 1      // 1 for fixed IP, 0 for DHCP
00006 
00007 DigitalOut led(LED1);
00008 
00009 // Requests come in here
00010 void request_handler(ParsedHttpRequest* request, TCPSocket* socket) {
00011 
00012     printf("Request came in: %s %s\n", http_method_str(request->get_method()), request->get_url().c_str());
00013 
00014     if (request->get_method() == HTTP_GET && request->get_url() == "/") {
00015         HttpResponseBuilder builder(200);
00016         builder.set_header("Content-Type", "text/html; charset=utf-8");
00017 
00018         char response[] = "<html><head><title>Hello from mbed</title></head>"
00019             "<body>"
00020                 "<h1>mbed webserver</h1>"
00021                 "<button id=\"toggle\">Toggle LED</button>"
00022                 "<script>document.querySelector('#toggle').onclick = function() {"
00023                     "var x = new XMLHttpRequest(); x.open('POST', '/toggle'); x.send();"
00024                 "}</script>"
00025             "</body></html>";
00026 
00027         builder.send(socket, response, sizeof(response) - 1);
00028     }
00029     else if (request->get_method() == HTTP_POST && request->get_url() == "/toggle") {
00030         printf("toggle LED called\n");
00031         led = !led;
00032 
00033         HttpResponseBuilder builder(200);
00034         builder.send(socket, NULL, 0);
00035     }
00036     else {
00037         HttpResponseBuilder builder(404);
00038         builder.send(socket, NULL, 0);
00039     }
00040 }
00041 
00042 int main() {
00043 
00044     // Connect to the network (see mbed_app.json for the connectivity method used)
00045     NetworkInterface *network = NetworkInterface::get_default_instance();
00046     if (!network) {
00047         printf("Cannot connect to the network, see serial output\n");
00048         return 1;
00049     }
00050 
00051 #if USE_FIXED_IP
00052     // This fixed IP address is convenient to test with Windows PC
00053     SocketAddress ip_addr("169.254.108.2");
00054     SocketAddress ip_mask("255.255.0.0");
00055     SocketAddress ip_gwaddr("169.254.108.1");
00056     network->set_network(ip_addr, ip_mask, ip_gwaddr);
00057 #endif
00058 
00059     network->connect();
00060 
00061     HttpServer server(network);
00062     nsapi_error_t res = server.start(80, &request_handler);
00063 
00064     if (res == NSAPI_ERROR_OK) {
00065         SocketAddress ip_addr;
00066         network->get_ip_address(&ip_addr);
00067         printf("Server is listening at http://%s\n", ip_addr.get_ip_address());
00068     }
00069     else {
00070         printf("Server could not be started... %d\n", res);
00071     }
00072 
00073     while(1) ThisThread::sleep_for(1000000);
00074 }