Server that executes RPC commands through HTTP.

Dependencies:   EthernetInterface mbed-rpc mbed-rtos mbed

Import programHTTP-Server

Server that executes RPC commands through HTTP.


This program is a small HTTP Server that can execute RPC commands sent through HTTP and sends back a reply. This reply can be either a line of text or an HTML page.

HTTP Request

The server will only read the first line of the HTTP header. It does not check that the header is correct and it does not attempt to read the body. Hence, the server is only interested in the type of the request and the path. Instead of requesting a file, the path contains the RPC command.

RPC command encoding


The RPC command must be encoded in a special way because no spaces are allowed in the path.

Thus, the RPC command is encoded in this way :

/<obj name or type>/<func name>?arg1_name=arg1_val&arg2_name=arg2_val

or, if the RPC command does not have any arguments :

/<obj name or type>/<func name>

So, a complete URL might be :

The name of the arguments does not appear in the final RPC command. So these 3 urls will do exactly the same thing :

Also, the order of the arguments are preserved.

Request handlers

To process requests, the server relies on RequestHandler. Each RequestHandler is assigned to a request type. Each type of request is assigned to a certain role :

  • PUT requests to create new objects
  • DELETE requests to delete objects
  • GET requests to call a function of an object

However, there is a RequestHandler that accepts only GET requests but it can create/delete/call a function. This was necessary to provide an interactive web page that allows creation and deletion of objects.


The reply depends on the formatter. Currently, three formatters are available :

  • The most basic one does not modify the RPC reply. Hence, if you consider sending request from python scripts, this formatter is the most appropriate one.
  • A simple HTML formatter will allow the user to view the RPC reply and a list of RPC objects currently alive from a browser.
  • Finally, a more complex HTML formatter creates an entire web page where the user can create and delete an object as well as calling functions on these objects.

Configure the server

The configuration of the server consists on choosing the formatter and adding one or more request handlers to the server. The main function initializes the server in order to produce HTML code and to receive data only using GET requests. If you want to use a simpler and different version of the server you can change the content of the main function (located in main.cpp) by this code :



    EthernetInterface eth;
    printf("IP Address is %s\n", eth.getIPAddress());
    HTTPServer srv = create_simple_server();

        printf("Error while initializing the server\n");
        return -1;

    return 0;

However, this configuration will not work with the following examples.


I assume that the server is using the InteractiveHTMLFormatter (which should be the case if you did not make any changes).

Using a browser

Here is a quick guide how to run this program :

  1. Compiles this program and copies it to the mbed
  2. Open TeraTerm (install it if you don't have it), select serial and choose the port named "mbed Serial Port"
  3. Reset your mbed
  4. The IP address should appear in teraterm. In this example, I will use
  5. Open your browser and go to
  6. If everything is ok, you should see a webpage.

Now, let's switch on a led. First, we need to create an object to control a led :

Then, let's write an RPC command to switch on led :

Using python

This program creates and switches on led2.

Sending RPC commands over HTTP with Python

import httplib


h = httplib.HTTPConnection(SERVER_ADDRESS)
h.request("GET", "/DigitalOut/new?arg=LED2&name=led2")
r = h.getresponse()

h.request("GET", "/led2/write?arg=1")
r = h.getresponse()


Of course, you might have to change the server address in order to make it work.

Wed Jul 17 10:15:05 2013 +0000
Initial import

Who changed what in which revision?

UserRevisionLine numberNew contents of line
feb11 0:9e4bcb10b3e3 1 #include "mbed.h"
feb11 0:9e4bcb10b3e3 2 #include "EthernetInterface.h"
feb11 0:9e4bcb10b3e3 3 #include "mbed_rpc.h"
feb11 0:9e4bcb10b3e3 4 #include "RPCCommand.h"
feb11 0:9e4bcb10b3e3 5 #include "HTTPServer.h"
feb11 0:9e4bcb10b3e3 6 #include "Formatter.h"
feb11 0:9e4bcb10b3e3 7 #include "RequestHandler.h"
feb11 0:9e4bcb10b3e3 8 #include "RPCType.h"
feb11 0:9e4bcb10b3e3 9
feb11 0:9e4bcb10b3e3 10 #define SERVER_PORT 80
feb11 0:9e4bcb10b3e3 11
feb11 0:9e4bcb10b3e3 12 HTTPServer create_simple_server()
feb11 0:9e4bcb10b3e3 13 {
feb11 0:9e4bcb10b3e3 14 HTTPServer srv;
feb11 0:9e4bcb10b3e3 15 srv.add_request_handler("DELETE", new DeleteRequestHandler());
feb11 0:9e4bcb10b3e3 16 srv.add_request_handler("GET", new GetRequestHandler());
feb11 0:9e4bcb10b3e3 17 srv.add_request_handler("PUT", new PutRequestHandler());
feb11 0:9e4bcb10b3e3 18 return srv;
feb11 0:9e4bcb10b3e3 19 }
feb11 0:9e4bcb10b3e3 20
feb11 0:9e4bcb10b3e3 21 HTTPServer create_interactive_server()
feb11 0:9e4bcb10b3e3 22 {
feb11 0:9e4bcb10b3e3 23 HTTPServer srv(new InteractiveHTMLFormatter());
feb11 0:9e4bcb10b3e3 24 srv.add_request_handler("GET", new ComplexRequestHandler());
feb11 0:9e4bcb10b3e3 25 return srv;
feb11 0:9e4bcb10b3e3 26 }
feb11 0:9e4bcb10b3e3 27
feb11 0:9e4bcb10b3e3 28 int main(void)
feb11 0:9e4bcb10b3e3 29 {
feb11 0:9e4bcb10b3e3 30 RPCType::instance().register_types();
feb11 0:9e4bcb10b3e3 31
feb11 0:9e4bcb10b3e3 32 EthernetInterface eth;
feb11 0:9e4bcb10b3e3 33 eth.init();
feb11 0:9e4bcb10b3e3 34 eth.connect();
feb11 0:9e4bcb10b3e3 35 printf("IP Address is %s\n", eth.getIPAddress());
feb11 0:9e4bcb10b3e3 36
feb11 0:9e4bcb10b3e3 37 HTTPServer srv = create_simple_server();
feb11 0:9e4bcb10b3e3 38
feb11 0:9e4bcb10b3e3 39 if(!srv.init(SERVER_PORT))
feb11 0:9e4bcb10b3e3 40 {
feb11 0:9e4bcb10b3e3 41 printf("Error while initializing the server\n");
feb11 0:9e4bcb10b3e3 42 eth.disconnect();
feb11 0:9e4bcb10b3e3 43 return -1;
feb11 0:9e4bcb10b3e3 44 }
feb11 0:9e4bcb10b3e3 45
feb11 0:9e4bcb10b3e3 46;
feb11 0:9e4bcb10b3e3 47
feb11 0:9e4bcb10b3e3 48 return 0;
feb11 0:9e4bcb10b3e3 49 }
feb11 0:9e4bcb10b3e3 50