RPC HTTP Server

The old example HTTP-Server that is given in the cookbook is outdated and deprecated. It uses the EthernetNetIf libraries, which have been replaced by the new EthernetInterface library. Also, the official mbed library changed some time recently, and the RPC Interface page is now slightly less correct. Since there were no examples that we could find, here we will go over how to use the new versions of the libraries, and example code will follow.

Using information from the RPC Interface page:

Quote:

RPC Commands are in the format: "/<Object name>/<Method name> <Arguments separated by spaces>

If you send just "/" mbed will return a list of objects that can be used

If you send "/<object name>/" then mbed will return the methods which can be used on this object.

To accomplish talk between the HTTP server side, and your C++ code, we can use an already built javascript library, but before we do this, it is nessesary to know how to interface the RPCVariable on the mbed side. Using the variable Request, we first have to initialize the variable in memory, and then set it up as a RPCVariable.

int Request = 0;
RPCVariable<int> RPCRequest(&Request, "Request");

The second argument "Request" is for the url of the command. Using the above code, a potential command could look like this: http://ip.of.mbed/rpc/Request/read or http://ip.of.mbed/rpc/Request/write%201 (the argument is after a space like ip.of.mbed/rpc/Request/write 1 - but a space in url addressing is %20)

Interfacing with Javascript has a nice javascript library that interfaced correctly with the old RPC scheme. We can still use this with a very minor change to the file. mbedrpc.js Download this file and add it to your HTML page. An example HTML page below uses this library and is a simple button to turn off and on a LED using RPCVariables. More information on how you can implement the Javascript library is over at the Interfacing with Javascript page. The mbed C++ code is below the HTML.

Example.htm

<html>
    <head>
    	<title>HTTP-RPC Server Example</title>
        <script src="mbedRPC.js" type="text/javascript" language="javascript"></script>
        <script type="text/javascript">
            mbed = new HTTPRPC();
            int x = 0;
            RPCRequest = new RPCVariable(mbed, "Request"); 

            function send_request() 
            {
            	if(x) { x = 0; }
            	if(!x) { x = 1; }
            	RPCRequest.write(x);
            }
        </script>
    </head>
    <body>
    	<p>mbedRPC javascript example! Using the mbedRPC javascript library to control mbed over http;
        <a href="http://mbed.org/cookbook/Interfacing-using-JavaScript"> for more information goto the cookbook </a>
        <br>
        If you click the below button, the LED1 should switch</p>
    	<button onClick="send_request()">LED1</button>
        
    </body>
</html>

Download the HTML file here Example.htm

main.cpp

#include "mbed.h"
#include "HTTPServer.h"
#include "FsHandler.h"
#include "LocalFileSystem.h"
#include "RpcHandler.h"
#include "mbed_rpc.h"



//  Use LED1 to indicate that the main loop is still executing
DigitalOut myled(LED1);
//  Use the serial connection 'pc' to dump debug information
Serial pc(USBTX, USBRX, "pc");
//  Instantiate a HTTPServer to handle incoming requests
HTTPServer  svr;
//  Instantiate a local file system handler named 'local' which will be used later to access files on the mbed.
LocalFileSystem local("local");

//  Create the EthernetInterface. This is optional, please see the documentation of HTTP Server's start method.
EthernetInterface eth;

//Make these variables accessible over RPC by attaching them to an RPCVariable
int Request = 0;
RPCVariable<int> RPCRequest(&Request, "Request");

int main() {

    HTTPFsRequestHandler::mount("/local/", "/");   //   Mount /local/ filesystem as root web path /
    svr.addHandler<HTTPFsRequestHandler>("/");     //   Serve all default HTTP requests
    svr.addHandler<HTTPRpcRequestHandler>("/rpc"); //   Serve all RPC requests
    

    //  Initialize the EthernetInterface and initiate a connection using default DHCP.
    eth.init();
    eth.connect();
    
    if (!svr.start(80, &eth)) {
        error("Server not starting !");
        exit(0);
    }
    
    while(1) {
        svr.poll();
        myled = Request;
    }
}

Import programHTTP-RPCServerExample

updated RPC command to match javascripting language


16 comments on RPC HTTP Server:

07 Dec 2013

Very well written description! Thanks for contributing Sam!

21 Mar 2014

Hallo Sam. I have tried this example but the led is not lit up when I click the button. I am using the url which is in the format: http://mbed IP address/Example.htm . Can you kindly let me know what might be the problem? I have not changed anything in the imported program nor mbedrpc.js file.

21 Mar 2014

jinhee cho wrote:

Hallo Sam. I have tried this example but the led is not lit up when I click the button. I am using the url which is in the format: http://mbed IP address/Example.htm . Can you kindly let me know what might be the problem? I have not changed anything in the imported program nor mbedrpc.js file.

Without seeing any changes to your files, make sure of the following:

1) you have the correct mbed IP address 2) you correctly load the js file in the html file 3) Check for any browser errors

22 Mar 2014

Ok thank you. I will do what you have outlined and see if the program will work.

24 Mar 2014

Hallo Sam, I keep on getting a browser error in the picture shown./media/uploads/sachungo/js_browser_error.png I looked for any missing semicolon within the <script> tags but found there's none. As for "the send_request definition" I don't know what it means as the function is defined. I don't know anything about browser errors so I'll appreciate it if you help me to sort out the errors.

24 Mar 2014

Ok I managed to solve the second error but not the syntax and reference error.

10 Jun 2014

I have donwload this program but the program doesn't work when I do click on button .It works with http://ip.of.mbed/rpc/Request/write%201 .What is the problem?

Solved

13 Jun 2014

Please, could you explain how did you solve it. I have the same problem

13 Jun 2014

I have also another problem that I can not connect the http server after first time. I know the mbed is running with the help of debug led. But, somehow server stops after the first connection.

11 Mar 2015

there a problem with the htm example above. it should be if()..else and not if()..if()

15 Apr 2015

Can someone tell me how can I use the SD card in my k64f instead of the Localfilesystem. I am kind of stuck here And in which folder I should put the htm and js files? Please help

Solved!

15 Apr 2015

In the mbedrpc.js, the last 3 lines. Shouldn't the RPCvariable be RPCFunction instead? because we are defining the run method on the function not the variable? please correct me if I am mistaken.

16 Apr 2015

So I realized that if 1)I change the "," to " " in the mbedRPC.js and 2)the second if to else if in the example.htm, It will work fine.

16 Apr 2015

Bora YILDIZ wrote:

Please, could you explain how did you solve it. I have the same problem

Do you still have the same problem Bora YILDIZ? I know it is old, but I could solve it now.

31 Aug 2016

Hello. I have tried this example and it works well. I have just changed the ethernet to be in fixed IP : eth.init("192.168.0.2","255.255.255.0","192.168.0.1");

I use an LPC1768, and simply send via a web navigator a request "http://192.168.0.2/rpc/Request/write%201" But after several rpc requests (3 or 5) the mbed is not responding. Maybe the http server crash ? I don't know...

Do someone also meet this issue ? and how to resolve it ?

02 Sep 2016

J. G. wrote:

Hello. I have tried this example and it works well. I have just changed the ethernet to be in fixed IP : eth.init("192.168.0.2","255.255.255.0","192.168.0.1");

I use an LPC1768, and simply send via a web navigator a request "http://192.168.0.2/rpc/Request/write 1" But after several rpc requests (3 or 5) the mbed is not responding. Maybe the http server crash ? I don't know...

Do someone also meet this issue ? and how to resolve it ?

Hello !

it seems I have resolved my issue :) I have simply updated the different librairies. It seems the most important is the update of EthernetInterface. But pay attention, if you update the library using the compiler user interface this will fail (in my case this has not worked). The best consists in deleting it, and then importing it from https://developer.mbed.org/handbook/Ethernet-Interface.

I hope this will helps some people.

Please log in to post comments.