//
you're reading...
How-to's

HTTYR: Responding to phone events

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

The technique described here only works if you are using a Wifi dongle.

Suppose that you want your robot to know when you come home, so that it can greet you. Or you want it to come and tell you when you receive a text message. If you have an Android phone, you can use an application such as AutomateIt for this.

To let you know that you have come home, you just wake up the phone before you get to your door to make it connect to your Wifi.

AutomateIt lets you set up rules that fire when events occur. Those events can include your phone waking up, connecting to the Wifi network, disconnecting from the Wifi network, receiving a text, etc.. One of the actions that AutomateIt supports is to connect to a web page in the background (without starting a browser). So, if we had a simple web server on our EV3 robot, we could get our Android phone to tell it about all sort of events, and the robot could respond appropriately.

It does not need to be a very complex web server. It only needs to respond to GET commands, and it can deal with them one at a time.

The URLs that you type into AutomateIt will look like

http://<ip-address>/Hello

or

http://<ip-address>/Text

So here is a very simple web server that will do the job:

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.hardware.Sound;

public class SimpleWebServer {
  public static final int PORT = 80;
  private ServerSocket ss;
  private Socket sock;

  public SimpleWebServer() throws IOException {
    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)
          break;
        String reply = execute(cmd);
        if (reply != null)
          ps.println(reply);
        else {
          br.close();
          ps.close();
          break;
        }
      }

    }
  }

  public String execute(String cmd) {
    System.out.println("cmd: " + cmd);
    String[] tokens = cmd.split(" ");

    if (tokens.length > 1 && tokens[0].equals("GET")) {
      if (tokens[1].equals("/Hello")) {
        Sound.beepSequenceUp();
      } else if (tokens[1].equals("/Goodbye")) {
        Sound.beepSequence();
      } else if (tokens[1].equals("/Text")) {
        Sound.beep();
      }
      return "HTTP/1.1 200 OK\r\n\r\nOK\r\n";
    }
    return null;
  }

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

Discussion

No comments yet.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

You are commenting using your Google+ 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: