RasPi does the Home Automation (Part IV): Cut the ‘rope’

Hi all,

until now all UI-Actions to control my wireless gears had to run directly on the Pi which controls the remote as e.g. deviceControl.turnOn(device); is called.

But now I’d rather make a web service available for this purpose to be more independently.
Question is: Which server should I use?

  • Apache/PHP: too less Java 😉
  • GlassFish/Java EE: too fat for this purpose and hard to take for a RasPi

But wait a minute: wasn’t there a HttpServer class that comes with Java6?
Using HttpServer I wrote a very small Server to provide the desired service.

Now I can switch my devices by an URL like this:

http://RASPI-IP:PORT/CONTEXT?houseCode=HOUSE-CODE&group=GROUP&device=DEVICE&command=COMMAND

e.g. http://raspi.home:22222/homeserver?houseCode=a&group=1&device=2&command=1

SEND URL:
RasPi_HomeServer1

SERVER (RE-)ACTION:
RasPi_HomeServer2

Recently my Pi received a command from Madrid (thanks José for supporting me ;-)):

2013-07-18 08_53_13-Twitter _ Interactions

For a more convenient usage with e.g. an JavaFX-EventHandler I use a HttpCommand to act like a DeviceContol:

public class SendHttpCommand {

    public static void main(String[] args) {
        Settings settings = new Settings();
        HttpCommand httpCommand = new HttpCommand(settings.getProperty("sweethome.server.ip.local"));
        Device terrace = new Device("Terrace", "a", "1", "2");
        String result = httpCommand.turnOn(terrace);
        System.out.println(result);
    }
}

Now the DevicePane looks like this:

public class DevicePane extends AnchorPane {

    @FXML
    private Button offButton;
    @FXML
    private Button onButton;
    @FXML
    private Text deviceNameText;

    private Device device;
    
    private DeviceControl deviceControl;
    private HttpCommand httpCommand;
    
    public DevicePane(final Device device) {
        this.device = device;
        init();
    }

    private void init() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/DevicePane.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();
        } catch (IOException ex) {
            Logger.getLogger(DevicePane.class.getName()).log(Level.SEVERE, null, ex);
        }

        Settings settings = new Settings();
        httpCommand = new HttpCommand(settings.getProperty("sweethome.server.ip.local"));
        
        deviceControl = new DeviceControl();
        
        deviceNameText.setText(device.getName());

        offButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                turnOff();
            }
        });

        onButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                turnOn();
            }
        });
    }

    private void turnOn() {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                deviceControl.turnOn(device);
                httpCommand.turnOn(device);
            }
        });

    }

    private void turnOff() {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                deviceControl.turnOff(device);
                httpCommand.turnOff(device);
            }
        });

    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *