Recent changes
FIR Filter
Homepage
MPL115A2
Compiler Error 42
From the mbed microcontroller Cookbook.  

Interfacing Using RPC

There are many occasions when its useful to be able to talk to your mbed from a computer. This could be to control actuators using the mbeds outputs, gather data using the mbeds sensors or create remote applications over a network. Creating an interface between mbed and a computer can be difficult because it requires you to specify a communication format and then to write code on both the mbed and the computer you are interfacing with.

The mbed is capable of receving and interpretting RPC commands and this can be used to greatly simplfy creating an interface. The RPC commands are in a predefined format and can be sent over any transport mechanism that can send a stream of text. They allow you to directly interact with objects on mbed.

This page shows the code that needs to be running on mbed for RPC to work, we've also created libraries for several popular languages which allow you to use RPC over several transport mechanisms without having to have to do any work to set up the transport mechanism or formatting of your messages. Libraries have been developed for: MATLAB, LabVIEW, Python, Java and .NET.

Information

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.

This is an example of the RPC command required to create an LED object and turn it on:

Code

/DigitalOut/new LED1 myled
/myled/write 1

You can also interact with objects that are created in code on the mbed you just refer to them by their name.

Troubleshooting!

If you want to interact with an object then make sure you define it on mbed with a name: DigitalOut myled(LED1, "myled")

On mbed you need to pass these commands into the rpc function:

Code

char buf[256], outbuf[256];
strcpy(buf, "/DigitalOut/new LED1 myled");
rpc(buf, outbuf); 

You can use any communication method to receive the string and pass it into RPC function

RPC Over Serial

To carry out RPC over serial you need to receive the commands from the serial port and then pass them into the RPC function. Finally you return the result. This can be done using a program like this:

» Import this program

00001 #include "mbed.h"
00002 #include "rpc.h"
00003 Serial pc(USBTX, USBRX);
00004 int main() {
00005     // setup the classes that can be created dynamically
00006     Base::add_rpc_class<AnalogIn>();
00007     Base::add_rpc_class<AnalogOut>();
00008     Base::add_rpc_class<DigitalIn>();
00009     Base::add_rpc_class<DigitalOut>();
00010     Base::add_rpc_class<DigitalInOut>();
00011     Base::add_rpc_class<PwmOut>();
00012     Base::add_rpc_class<Timer>();
00013     Base::add_rpc_class<SPI>();
00014     Base::add_rpc_class<BusOut>();
00015     Base::add_rpc_class<BusIn>();
00016     Base::add_rpc_class<BusInOut>();
00017     Base::add_rpc_class<Serial>();
00018     // receive commands, and send back the responses
00019     char buf[256], outbuf[256];
00020     while(1) {
00021         pc.gets(buf, 256);
00022         rpc(buf, outbuf); 
00023         pc.printf("%s\n", outbuf);
00024     }
00025 }

Or here is a bin file which you can download straight on to your mbed: rpc_serial_lpc1768.bin Note that its not a case of either having rpc or your own code. You can do both at the same time you just need to make sure you receive the serial commands when they are sent.

Once you have this running on mbed you can control mbed from a terminal by just typing commands in the format above (make sure you have your terminal set to transmit CR+LF).

RPC Over HTTP

The HTTP Server has an rpc handler, so by using the HTTP server example program below and download it on to your mbed you will be able to use RPC over HTTP. The rpc commands are sent by adding them to the URL of the mbed in a browser.

Information

RPC Commands over HTTP are in the format: http://<url of mbed>/rpc/<Object name>/<Method name> <Arguments separated by spaces>

The mbed will pass the URL it is assigned over the USB serial.

The important line of code for RPC is to add the RPC handler.

Code

svr.addHandler<RPCHandler>("/rpc"); 

» Import this programRPC_HTTP

A slight Alteration to Donatien's HTTP Server Example so that it registers all the classes with RPC support to demonstrate RPC over HTTP

Adding RPC to your own code

Nearly all projects involve using more than just the mbed's Digital and Analog I/O and so the RPC interface above can rapidly become insufficient and you're back to creating your own communication protocol and programming the low level communication on both the mbed and in your software application. The RPC Interface Library provides a mechanism which can help over come these problems by allowing you to call custom functions over RPC and read and write to variables on mbed using RPC. The cook book page for the RPC Interface Library gives detailed information on how to use this library within your own code.

» Import this library into a program

RPCFunction Class to call custom functions over RPC
RPCVariable< T > Class to read and set an attached variable using the RPC
SerialRPCInterface Provides an Interface to mbed over RPC

Software Libraries

You now have a program running on mbed which handles the RPC commands. To make a complete application your software needs to set up the transport mechanism on its end and format the commands into strings. To make this easier we've created libraries that not only give you simple access to the RPC from several programming languages but also include classes for much of the mbed API. The software libraries also include classes for the RPC Interface Library to help extend RPC into your own code.

They all use a consistent interface which is a close as possible to the interface for programming on mbed. They've been designed so that is easy to switch between transport mechanisms and so that an application can control multiple mbeds.




calendar Page history
Last modified 19 May 2011, by   user Open OBC   tag interfacing, rpc | 11 comments  

11 comments on Interfacing Using RPC:

28 Sep 2010

RPS Over Serial not working - nor is much else.. I have downloaded the bin and get no response when using PuTTY term emulator and typing commands in the format above. To check that something was working i put a pc.print("hello") line in and recompiled which worked but still no RPC functions. I am rapidly approaching end of my tether with MBED ( yes i am sure it is all my fault but TINI was so much easier and all the examples worked). Any ideas .. Many thanks.

28 Sep 2010

Hi, please ignore my previous note, i have now downloaded teraterm, switched on CR+LF and all is working, still cant get Ethernet to work though, cheers

28 Sep 2010

Hi

CR+LF catches me out a lot! Are your ethernet problems with getting a HTTP Server up and running (ie can you load a html page off mbed) or are they specifically with the RPC handler. A few points which can help when setting up ethernet: If possible connect up the USB serial when you connect mbed to your network. The server will then print out to a terminal a lot of information about what its doing, including telling you if theres an error and the address mbed has been allocated by DCHP. Which also means that if you're connecting it to a non DHCP network you need to specify the IP address see: Ethernet. You might also be able to log on to your routers admin area and look at the DCHP client list to check that mbed has been connected.

28 Sep 2010

Micheal , thanks for the rapid response. I have only got as far as trying the basic HTTPServer, i need to modify it to handle non dhcp environment, i modified the code from the EthernetTest project. I tried other examples e.g. HTTPServer but could not get that to compile once i tried to change it for fixed ip. Overall I suspect i have a hardware problem and am waiting for the Ethernet board from Coolcomponents. I will raise query on the Ethernet forum if i am still stuck. I also tried ethspam which made the hub lights flash but still could not see any arp packets using wireshark.

18 Oct 2010

I have got the Ethernet RPC demo to work, but the only way to communicate with my PC is by typing in lots of web addresses into IE.

http://192.168.51.155/rpc/led3/write 1

Does any one have a DELPHI (2006) solution ?

I have tryed TerraTerm, but it refuses to even open a connection.

Cheers Ceri.

19 Jan 2011

I have a doubt related to RPC. 1) If I control same pin using both rpc and direct Digital out(in compiler) how does it work. pl. tell me if any one knows. which will get preference? for example

include "mbed.h" DigitalOut pin11out(p11);

int main() { while(1) {

pin11out= 1;

} }

Now if I am controlling this p11 bit using Python also simultaneosly from an external GUI also

>>> from mbedrpc import * >>> mbed = HTTPRPC("192.168.0.4") >>> x = DigitalOut(mbed,p11) >>> x.write(0)

What may happen? I am confused.

2) In another case suppose pin 11 is defined as digital input in mbed compiler program and pin11 digital output in python , which will get preference?

02 Sep 2011

Hi, I am trying to use SPI with similar commands via Tera Term. When I try the command /SPI/new myspi(p5,p6,p7) then all 4 LED's start blinking and I have to reset mbed again. And the SPI returns "SPI pinout mappi" Can you please put some light on the implementation of SPI constructors ? Thanks in advance. /media/uploads/rabindra/teratermspi.png

14 Sep 2011

Hi, I am trying to save the path between matlab and mbed as per the instructions in the "Read Me" file in the RPC zip file. However the function can't save the path to mbed and always prints the error message.

Can someone please put detailed instructions as to how to get this done? I am new to this.

Thanks

14 Sep 2011

Hi Salar,

This is due to User Access Control (UAC) in Windows 7 (and also Windows Vista). For details, see http://www.mathworks.com/support/solutions/en/data/1-9574H9/index.html?solution=1-9574H9

The solution is to 1) start MATLAB in Administrator mode (right click on the MATLAB icon, choose "Run as administrator"), 2) Follow the instructions to save the path for the mbed MATLAB library 3) Exit MATLAB

Now, you can start MTALAB as usual and the mbed files will be on the path.

4 weeks ago

Hello mbed Team!

I have just tried the RPC functionality over HTTP with Python. It is spectacular, but I just noticed the I2C peripheral implementation has no RPC support at this moment. Could you please add this support? Thank you!

1 week, 2 days ago

Hello all. By implementing RPC is it possible to go to a sampling rate faster than 200 or 300 ms. Indeed, using the analog value is rpc_read_ana 200 to 300ms for display.

Please login to post comments.