You are viewing an older revision! See the latest version

Interfacing with Java

Here is a Java library for interfacing Java programs with mbed using the RPC. This could be used for:

  • Creating a GUI for mbed programs
  • Controlling actuators from and reading data into Java programs

Java is potentially a great choice for interfacing to mbed over a network as it can be run in a browser and across many different platforms. This page shows how to use the library.

Information

To use the mbedRPC library your mbed needs to be running code which can receive and execute RPC commands. Information and examples of this, can be found on the Interfacing-Using-RPC page. Compile and run the program for the type of transport mechanism you wish to use.

The mbedRPC Library

Here is the mbedRPC library as a jar file: mbedRPC.jar

Here is an example of the code required to get started and flash some LEDs. This will run as a Java application.

mbedRPC_HelloWorld

import org.mbed.RPC.*;
public class HelloWorld implements mbedRPC{
	DigitalOut d1;
	DigitalOut d4;
	mbed mbed;
	
	public static void main(String[] args) {
		HelloWorld demo = new HelloWorld();
		demo.flash(10);
	}
	
	public HelloWorld(){	
		System.out.println("mbed RPC Hello World");
		//Create an mbed object for communication over serial
		mbed = new SerialRxTxRPC("COM5", 9600);
		//Or:   mbed = new HTTPRPC("http://192.168.2.2")
		
		//Create new Digital Outputs on the mbed
		d1 = new DigitalOut(mbed, LED1);
		d4 = new DigitalOut(mbed, LED4);
		
	}
	public void flash(int n){
		
		for(int i = 0; i< n; i++){
			//Call the DigitalOut objects' methods 
			d1.write(1);
			d4.write(0);
			wait(500);
			d1.write(0);
			d4.write(1);
			wait(500);
		}
		mbed.delete();
		System.out.println("Complete");
		
	}
	//A function to create a wait
	public static void wait (int n){
        long startTime,currentTime;
        startTime =System.currentTimeMillis();
        do{
            currentTime = System.currentTimeMillis();
        }
        while ((currentTime - startTime) < n);
	}
}

And here is this class for download: HelloWorld.java

The Code API

The aim in writing the library has been to mirror the mbed API so that programming in Java is very similar to programming for mbed.

The first thing you have to do is import the library into your program. Note that to do this you need to place the Jar in the Java library folders or if you're using an IDE then add it to the build path for that project. If you want to distribute the project you produce then you'll need to package this jar with it.

import org.mbed.RPC.*

You then need to create your class. To assist in using the mbedRPC library implement mbedRPC in the class declaration

public class HelloWorld implements mbedRPC{....

The next step is to create an mbed object which sets up the communication and knows how to execute RPC methods. You can set this up using different transport mechanisms, currently serial or HTTP.

SerialRPC mbed = new SerialRPC("COM5", 9600);
OR:
SerialRxTx mbed = new SerialRxTx("COM5", 9600);
OR:
HTTPRPC mbed = new HTTPRPC("http://192.168.2.2");

For more information on using serial in Java see below.

You can now create objects on mbed. This is done in almost the same way as if you were programming on mbed but as well as specifying the pins you must also pass in the mbed object for the mbed you want to create the object on.

DigitalOut d1 = new DigitalOut(mbed, LED2);
DigitalOut d2 = new DigitalOut(mbed, p20);
AnalogIn ain = new AnalogIn(mbed, p20);
PwmOut p1 = new PwmOut(mbed, LED3);

Alternatively you can tie to objects already created on mbed and then execute their methods.

DigitalOut d3 = new DigitalOut(mbed, "myled");

Finally you can call methods on the objects you have created or tied to. The methods are those described in the Handbook. However unlike on mbed there are no overloaded operators so you have to call the methods by name.

d1.write(1);
d2.write(0);
float i = ain.read();
p1.period(1);
p1.write(1);

Information

If you want to make an RPC call which isn't wrapped by the classes we have provided then use:

String RPCresponse = mbed.RPC(<String ObjectName>,<String MethodName>,<String[] Arguments>);

Setting Up Serial in Java

To use Java in serial and with the mbedRPC library you need to install either the Java Sun Communications API or use RxTx depending on operating system. You can then choose to create your mbed object according to the type of communication you have installed. RxTx would be the best choice for most users.

Applet demo

As mbed can run as a server it is possible to serve a Java applet from mbed which can then use the RPC to control mbed. This offers a great way to create a web based front end for your mbed project.

Here is an example of an applet controlling mbed:

Note that the Jar needs to know the address of mbed to then send the RPC commands this is done by using the code base to return the mbed's address.

Applet Security

In general Applets are only allowed to communicate with the domain they are downloaded from. This means that if you use an applet it must be hosted on mbed.

On mbed you need a HTML file with the applet embedded in it:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<title>mbed Java Applet Example</title>
</head>
<body>
<h1>mbed Applet Demo</h1>
<P align = "CENTER">
<APPLET ARCHIVE="mbedapp.jar" code="HelloWorld" WIDTH="400" HEIGHT="200">
   The Applet should be here
</APPLET> 
</P>
</body>
</html>

To try this applet out download the zip from above. Save the 3 files in this zip to your mbed's drive (and make sure there arn't any bin files which will be detected as more recent). Connect your mbed to a router and then browse to http://<address of mbed>/launch.htm. The address mbed is assigned will be printed out over the serial port. You will be able to control the LEDs on the mbed by clicking in the applet. Note that this HTTP server program won't flash LED1 as I removed that so you can control all 4 LEDs over RPC, instead the program will show its alive over serial.


All wikipages