HTTP Server

This page explains how to implement an HTTP Server on your mbed. It uses the Networking stack.

Hello World!

#include "mbed.h"
#include "EthernetNetIf.h"
#include "HTTPServer.h"

EthernetNetIf eth;  
HTTPServer svr;

DigitalOut led1(LED1);

int main() {
  printf("Setting up...\n");
  EthernetErr ethErr = eth.setup();
  if(ethErr)
  {
    printf("Error %d in setup.\n", ethErr);
    return -1;
  }
  printf("Setup OK\n");
  
  svr.addHandler<SimpleHandler>("/"); //Default handler
  svr.bind(80);
  
  printf("Listening...\n");
    
  Timer tm;
  tm.start();
  //Listen indefinitely
  while(true)
  {
    Net::poll();
    if(tm.read()>.5)
    {
      led1=!led1; //Show that we are alive
      tm.start();
    }
  }
  
  return 0;
}

This program is available here:

Packages

Precompiled version:

» Import this library into a programHTTPServer

This library is deprecated.

Library

Architecture

The HTTPServer is composed of:

  • The actual server (HTTPServer)
  • A request dispatcher, instanciated on each request (HTTPRequestDispatcher)
  • Request handlers instanciated by the dispatcher(deriving from HTTPRequestHandler)

HTTP Server Requests Handlers

  • Simple "Hello world" handler
  • Filesystem handler
  • RPC handler

You can also Create your custom handler.

Includes

#include "HTTPServer.h"

Reference

» Import this program

Public Member Functions

  HTTPServer ()
  Instantiates the HTTP Server.
template<typename T >
void  addHandler (const char *path)
  Adds a handler.
void  bind (int port=80)
  Starts listening.

Bind the server to port port and start listening. This function returns immediately. You must call Net::poll() regularly after this so that the server serves requests properly.

Examples

This example sets the server up and demonstrates the use of all-three kinds of handlers.

The local file system is available either under the /files/ path. Since FSHandler is the default handler here, the mbed file system (/webfs/) is made available on the root path of the server as well, so mbed.htm is available at the URLs http://a.b.c.d/files/mbed.htm and http://a.b.c.d/mbed.htm, where a.b.c.d is your mbed's IP address.

#include "mbed.h"
#include "EthernetNetIf.h"
#include "HTTPServer.h"

DigitalOut led1(LED1, "led1");
DigitalOut led2(LED2, "led2");
DigitalOut led3(LED3, "led3");
DigitalOut led4(LED4, "led4");

LocalFileSystem fs("webfs");

EthernetNetIf eth;  
HTTPServer svr;

int main() {
  Base::add_rpc_class<DigitalOut>();

  printf("Setting up...\n");
  EthernetErr ethErr = eth.setup();
  if(ethErr)
  {
    printf("Error %d in setup.\n", ethErr);
    return -1;
  }
  printf("Setup OK\n");
  
  FSHandler::mount("/webfs", "/files"); //Mount /webfs path on /files web path
  FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path
  
  svr.addHandler<SimpleHandler>("/hello");
  svr.addHandler<RPCHandler>("/rpc");
  svr.addHandler<FSHandler>("/files");
  svr.addHandler<FSHandler>("/"); //Default handler
  //Example : Access to mbed.htm : http://a.b.c.d/mbed.htm or http://a.b.c.d/files/mbed.htm
  
  svr.bind(80);
  
  printf("Listening...\n");
    
  Timer tm;
  tm.start();
  //Listen indefinitely
  while(true)
  {
    Net::poll();
    if(tm.read()>.5)
    {
      led1=!led1; //Show that we are alive
      tm.start();
    }
  }
  
  return 0;

}

You can also directly import this example there:

Robustness

Although this HTTP Server should still be considered in beta phasis, it has been built for robustness. A good test is this web page that you can use with the previous example: put it on your mbed and open it in your browser (at an address like http://a.b.c.d/webfs/stress.htm): http://mbed.org/media/uploads/donatien/stress.htm.

It will start an asynchronous HTTP Request every 100ms, sending RPC commands to set the leds on and off. You will see that all requests are not successfully processed, but the server will not crash and will remain up and running properly after you close the page.

License





43 comments:

05 Jul 2010

Donatiens,

How can I set the server name? How can I get the server IP?

tks

05 Jul 2010

Hi,

I forgot to update the Ethernet page, but there is a getIp() function that you can use to retrieve it. Otherwise you can see it on the serial debug output. What do you mean with the server's name? If it's DNS, you have to register the mbed's IP against a proper DNS server.

Donatien

06 Aug 2010

Does every LPC1768 board have a built in ip address? Or is this assignable? Where is this already explained? I am very new to this product and this forum...

10 Aug 2010

user C Neroth wrote:

Does every LPC1768 board have a built in ip address? Or is this assignable? Where is this already explained? I am very new to this product and this forum...

By default, the server will request an IP address using DHCP. You can see the address it has been allocated on the console output. Of course this only works if your network has a DHCP server.

Alternatively you can specify a static IP address when you instantiate the server.

05 Sep 2010

Hi,

the following sequence: <reset>

http://192.168.1.74/rpc/ webfs led4 led3 led2 led1 DigitalOut Base

http://192.168.1.74/rpc/DigitalOut new

http://192.168.1.74/rpc/DigitalOut/new,5 obj10005718

http://192.168.1.74/rpc/obj10005718 write read delete

http://192.168.1.74/rpc/obj10005718/read

hangs the mbed; led1 stops blinking.

I suspect "/rpc/DigitalOut/new,5" is wrong. maybe a pointer to the "new" syntax? I also would like to be able to give the new object a name.

Anyone can comment please?

Art.

05 Sep 2010

Hi, I think i found the first question after reading http://mbed.org/projects/libraries/svn/mbed/trunk/PinNames.h It should have been http://192.168.1.74/rpc/DigitalOut/new,p5 My second question stands. Can I apply a name to the new object?

thanks, Art.

06 Oct 2010

I imported the httpserverhelloworld and compiled it,unchanged. when loaded to my mbed, I get these debug comments in the serial port

Setting up... [..\fwk\if\eth\EthernetNetIf.cpp:setup@86] HW Addr is : 00:02:f7:f0:43:d1. [..\fwk\if\eth\EthernetNetIf.cpp:setup@99] DHCP Started, waiting for IP... [..\fwk\if\eth\EthernetNetIf.cpp:setup@131]

after maybe 5-10 seconds, my mbed shows a runtime error by flashing all 4 LEDs in alternating patterns.

and sometimes I see this a little later: Timeout. Error -65534 in setup.

Is this due to a program error or just a problem linking to the router?

06 Oct 2010

NVM... it was a wiring issue. corrected the wiring and the server runs correctly now.

11 Oct 2010

Finaly got this working, by using my internet browser, as above ..

how could i make this more controlable from a PC Application (Delphi)

I have got the serial RPC to work, and i hoped to be able to create a simmilar method of communication ! does any one have a solution ?

Cheers Ceri.

17 Oct 2010

The code on this page:

http://mbed.org/users/donatien/programs/NetHttpServerExample/5zb4a/docs/HttpServerExample_8cpp_source.html

does this:

svr.addHandler<FSHandler>("");

whereas the code on this page does this:

svr.addHandler<FSHandler>("/");

Is that significant?

Also, while I'm here, I've found that if I put a file named "hello.html" on my mbed it can't be served by the HTTPServer, but if I name it "hello.htm" then it works. I suspect this has something to do with the implementation of LocalFileSystem, and probably for good reason, but it had me stumped for a while.

Rob.

19 Oct 2010

Hello,

I got the HTTP server working on my mbed and can acces it with IE. Does anybody know how to configure my router so that I can set up a virtual server to the mbed? The local listening port should be 80 but what should the remote port be? I want to control the mbed from over the internet, from everywhere I am (just like with remote desktop control to remotely control a PC)

Greetings

27 Oct 2010

Hi,

Now I'm wondering, does it work with an RPC server because if I just type in the DNS (or wan ip) of my home router and then try to forword it to my mbed connected to the router at home, it doesn't work. Does anybody know how to do this? I want to control my mbed from leuven (and anywhere in the world)

06 Nov 2010

I managed to run the demo HTTP Server. As I learned from the HTTPRequestHandler instruction that

svr.addHandler<SimpleHandler>("/hello");

returns an "hello" message.

Now I wonder where the "Hello World!" response of my webserver (http://192.168.1.51/) comes from since the demo code

svr.addHandler<SimpleHandler>("/"); Default handler

has not even a simple letter after the slash? The mbed does´nt contain an index.htm file!? By the way at this state the filesystem is not activated.

chris

07 Nov 2010

Hi Chris,

The "Hello World!" is coming out of the SimpleHandler. If you want to let the mbed serve HTML files you have to link an FSHandler to the LocalFileSystem (see the HTTPServerExample from Donatien).

H

07 Nov 2010

Thanx Henny,

I located it now in the source: 00040 const char* resp = "Hello world !"; From the description I expected to pass the message as argument - did´nt expect to see it hard coded ;-)

Chris

15 Nov 2010

MULTIPLE CONNECTIONS

Hi Donatien,

Is the Http-Server able to manage multiple connections at the same time? In case I want to connect it to Internet and two or more clients send a "get request" at the same time, what happens?

Best Regards Sergio

13 Dec 2010

I think there's an issue that the maximum message size is 980 bytes. The mbed tells me I'm going to send about 1200 bytes but Firefox only receives 980 bytes...

15 Jan 2011

Seems that HTTPServer cannot deal out images ... don't really know why. Can someone explain?

09 Feb 2011

I was wondering what does "Net::poll" command do ?

05 May 2011

The stress.html filementioned is missing. Anyone has an example of a html triggering RPC? Just to see how it looks like