This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information

Power control with RaspberryPi, JVx and Vert.x

My idea was to create a programmable timer for some lamps at home. I wanted to control the timer via mobile phone (not only smartphone). I decided to use simple SMS'.

The timer app was not really a challenge and the SMS interface was also very straight forward, so I added a little bit network communication and complexity. The result:

Power control

Power control

My Hardware:

  • Raspberry Pi Model B
  • Cinterion TC65i
  • Standard table lamp
  • Solid State Relay (found here)
  • Laptop as cluster client
  • Mobile Phone :)

My software:

My first question was - Which application server should I use for my Pi? I thought that JBoss or Tomcat would be overhead and I would save resources. I decided for Vert.x and a simple socket server. I wanted to show the integration of Vert.x with JVx and so my server got a JVx application with some business logic for publishing messages.

The server "thing" was clear and for reading SMS' I wrote a simple polling app. The app reads SMS from my hardware via RS232 and forwards received SMS via JVx APIs to my server (with Vert.x' NetClient).
I didn't use the EventBus for sending messages becasue I wanted to show the seamless integration of JVx and Vert.x.

The code for forwarding messages:

MasterConnection macon = new MasterConnection(new NetSocketConnection("10.0.0.39"));
macon.setApplicationName("demo");
macon.setUserName("user");
macon.setPassword("password");
macon.setAliveInterval(-1);
macon.open();

macon.callAction("publishMessage", msg.getOriginator(), msg.getText());

The server application is a standard JVx application (only server-side). It uses a standard XmlSecurityManager without the need for a database. The "business logic" is very simple:

public Vertx getVertx()
{
    return (Vertx)get("vertx");
}

public void publishMessage(String pNr, String pMessage)
{
    System.out.println("publishMessage (" + pMessage + ")");
       
    String sNumber = pNr;

    sNumber = sNumber.substring(0, 5);

    //hide last characters
    for (int i = 5; i < pNr.length(); i++)
    {
        sNumber += "*";
    }
       
    JsonObject jsobj = new JsonObject();
    jsobj.putString("number", sNumber);
    jsobj.putString("message", pMessage);
       
    getVertx().eventBus().publish("sms.received", jsobj);
}

I made some tests with vert.x clustering and decided to implement a (very) simple JavaFX application that shows published SMS'. The application contains following code:

vertx = Vertx.newVertx(25502, "10.0.0.11");

EventBus ebus = vertx.eventBus();
ebus.registerHandler("sms.received", new Handler<Message<JsonObject>>()
{
    public void handle(final Message<JsonObject> pMessage)
    {
        Platform.runLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    mdb.insert(false);
                    mdb.setValues(new String[] {"NUMBER", "MESSAGE"},
                                  new String[] {pMessage.body.getString("number"),
                                                pMessage.body.getString("message")});
                    mdb.saveSelectedRow();
                }
                catch (ModelException me)
                {
                    throw new RuntimeException(me);
                }
            }
        });
    }
});

Finally, I created a short video that shows all in action. It shows three applications on the same laptop. The first is the JavaFX application that shows published SMS. The second is the Power control (includes SMS check), started via PuTTY. The third application is the server instance, started via PuTTY. You can see a table lamp that will be turned on and off with SMS, sent from a smartphone.

Enjoy it :)


Power Control with RaspberryPi