you're reading...

HTTYR: Browser Control


This post is one of a series on how to train your robot.

In an earlier post, we learnt how to implement an extremely simple web server on the EV3.

Lets modify that slightly, so that it controls your robot:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

import lejos.robotics.RegulatedMotor;
import lejos.robotics.navigation.DifferentialPilot;
import lejos.utility.PilotProps;

public class WebControl {
  public static final int PORT = 80;
  private ServerSocket ss;
  private Socket sock;
  private DifferentialPilot robot;

  public WebControl() throws IOException {
    PilotProps pp = new PilotProps();
    float wheelDiameter = Float.parseFloat(pp.getProperty(PilotProps.KEY_WHEELDIAMETER, "4.0"));
    float trackWidth = Float.parseFloat(pp.getProperty(PilotProps.KEY_TRACKWIDTH, "18.0"));
    System.out.println("Wheel diameter is " + wheelDiameter);
    System.out.println("Track width is " +trackWidth);
    RegulatedMotor leftMotor = PilotProps.getMotor(pp.getProperty(PilotProps.KEY_LEFTMOTOR, "B"));
    RegulatedMotor rightMotor = PilotProps.getMotor(pp.getProperty(PilotProps.KEY_RIGHTMOTOR, "C"));
    boolean reverse = Boolean.parseBoolean(pp.getProperty(PilotProps.KEY_REVERSE,"false"));
    robot = new DifferentialPilot(wheelDiameter,trackWidth,leftMotor,rightMotor,reverse);
    ss = new ServerSocket(PORT);

  public void run() throws IOException {
    for (;;) {
      sock = ss.accept();
      InputStream is = sock.getInputStream();
      OutputStream os = sock.getOutputStream();

      BufferedReader br = new BufferedReader(new InputStreamReader(is));
      PrintStream ps = new PrintStream(os);

      for (;;) {
        String cmd = br.readLine();
        if (cmd == null)
        String reply = execute(cmd);
        if (reply != null)
        else {


  public String execute(String cmd) {
    String[] tokens = cmd.split(" ");

    if (tokens.length >; 1 && tokens[0].equals("GET")) {
      if (tokens[1].equals("/stop")) {
      } else if (tokens[1].equals("/forward")) {
      } else if (tokens[1].equals("/backward")) {
      } else if (tokens[1].equals("/left")) {
      } else if (tokens[1].equals("/right")) {
      return "HTTP/1.1 200 OK\r\n\r\nOK\r\n";
    return null;

  public static void main(String[] args) throws IOException {
    new WebControl().run();

You will need to run the PilotParams sample to set the pilot parameters for your robot.

We will need a web page with some javascript:

<title>Robot Control</title>
function ex(cmd) {
var req = new XMLHttpRequest();  
var page = ''.concat(cmd);
req.open('GET', page, false); 
<input type="button" value="forward" onclick="ex('forward');" />;
<input type="button" value="backward" onclick="ex('backward');" />
<input type="button" value="left" onclick="ex('left');" />
<input type="button" value="right" onclick="ex('right');" />
<input type="button" value="stop" onclick="ex('stop');" />

If you want to run this over the Internet you will need to implement port forwarding in your router to your robot’s IP address for whatever value you set the PORT variable.

For a more sophisticated web server on the EV3, see this project.



One thought on “HTTYR: Browser Control

  1. hi, where the printstream print the response (ps.println(reply);”line52″)? It works well but i wish notify the user in the web browser when they drive the robot forward. It does drive the ev3 forward but i wish to get respond from the java web server indicate it already accept the request. Also, i have add connect button which assign the ip inserted by user in a text field to the variable page. I also wish to notify the user when they actually connected to ev3 web server, but how get those respond from the server? I have asked in stackoverflow too, http://stackoverflow.com/questions/27685350/how-to-read-response-from-server-created-in-java-to-html-using-ajax?noredirect=1#comment43784994_27685350

    Posted by Pang Chee Chong | 2014/12/29, 11:13

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

About leJOS News

leJOS News keeps you up-to-date with leJOS. It features latest news, explains cool features, shows advanced techniques and highlights amazing projects. Be sure to subscribe to leJOS News and never miss an article again. Best of all, subscription is free!
Follow leJOS News on WordPress.com
%d bloggers like this: