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, ð)) { 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:
Please log in to post comments.
Very well written description! Thanks for contributing Sam!