Overview

IoT with Java8 and TinkerForge – 001

1 Comment

The Internet of Things (IoT) is one of the new interesting topics in the java world. Finally Java 8 SE Embedded provides an alternative to Java-ME. In this
new series we will have a look into the topic IoT from a java developer’s point of view.

The world of IoT is growing. More and more java developers start experimenting in it. How can you start? What are the possibilities? We will now approach this topic step by step. To get started I have chosen to work with TinkerForge.

 

Electronics for Software Developers
==================================
For many software developers electronics has always the connotation of soldering iron and much filigrane manual work before they can start with the actual important thing…coding.

But there is also some kind of lego-toolkit for the java developer. With this I mean the products of TinkerForge . Here the developer can choose from different elements which can be combined – for example by linking sensors to the MasterBrick. The MasterBrick itself is supplied by power via USB. This way we have already completed our first test arrangement. Estimated time for construction: 3 minutes. Thus we now can directly turn to the software.
To be able to use the hardware you have to install two services on your PC. For one thing the service which is responsible for the communication between MasterBrick, sensors and PC. For another thing a viewer which is responsible for the configuration, firmware updates and the like. Additionally the last one can also be used to analyze sensor data. I had no problems with the installation on Windows 8. Estimated time for installation: 5 minutes.
This way the setup is finished within 15 minutes and you can get going.



Hello IoT World!
==================================

In software engineering everyone starts a new topic with a HelloWorld. We will not break with this tradition. To do so we need a MasterBrick with one linked sensor as described above. To keep the example as simple as possible i have decided to use the temperature sensor. So the question is how to do the activation. The arrangement awakes when the connection to the USB port is completed. The MasterBrick shows a blue signal that it is alive. At first we start the service and the viewer (brickv) which we have installed before.
Every element owns a UID which we will need later for the communication. To get the UID of the specific element you have to connect your sensor to the viewer which provides the data of the sensor. In my case the sensor has the UID dXj.

svenruppert_iot_tinkerforge_001_brickv_001

The source code for this article can be found here (!Link). This repository contains also the language bindings which are needed for the communication with the TinkerForge components. The usage of the temperature sensor can be accomplished in a few steps.

  • Creation of an IPConnetion
  • Creation of an instance representing a temperature sensor
  • Configuration of the temperature sensor
  • Addition of the ActionListeners


For the further processing the last step is the most important. The measured values of the sensor will be processed in the anonymous inner class within the temperature method. Every time a timer interval is passed and the measured value has changed the method is invoked.

import com.tinkerforge.BrickletTemperature;
import com.tinkerforge.IPConnection;
 
public class ExampleCallback {
    private static final String host = "localhost";
    private static final int port = 4223;
    private static final String UID = "dXj"; 
    public static void main(String args[]) throws Exception {
        IPConnection ipcon = new IPConnection(); 
        BrickletTemperature temp = new BrickletTemperature(UID, ipcon); 
        ipcon.connect(host, port); 
        temp.setTemperatureCallbackPeriod(1000);
        temp.addTemperatureListener(new
          BrickletTemperature.TemperatureListener() {
            public void temperature(short temperature) {
                System.out.println("Temperature: "
                   + temperature/100.0 + " °C");
            }
        });
        ipcon.disconnect();
    }
}

Recording and Displaying the nightly measured values
==================================

Thereby the start is realized quickly and easily. Now as a little finger exercise we are going to record nightly measured values and check if the heater is switched off in the night as it is supposed to do. So the sensor is going to be
active the whole night and tranfers measured values in a transient list. The list itself is displayed in a simple JavaFX application.

public class HelloTinkerForge extends Application {
 
    private static final String host = "localhost";
    private static final int port = 4223;
    private static final String UID = "dXj"; 
 
 
    public static void main(String args[]) throws Exception {
        launch(args);
    }
 
    public static XYChart.Series series;
 
    @Override
    public void start(Stage stage) {
        stage.setTitle("Line Chart TinkerForge Sample");
        final DateAxis dateAxis = new DateAxis();
        final NumberAxis yAxis = new NumberAxis();
        dateAxis.setLabel("Time of Temp");
        final LineChart lineChart 
           = new LineChart<>(dateAxis, yAxis);
 
        lineChart.setTitle("Temp Monitoring");
 
        series = new XYChart.Series();
        series.setName("My temps");
        final ObservableList seriesData = series.getData();
 
        lineChart.getData().add(series);
        Scene scene = new Scene(lineChart, 800, 600);
        stage.setScene(scene);
        stage.show();
        new Worker(seriesData).start();
 
    }
 
    public static class Worker extends Thread {
        final ObservableList seriesData;
        public Worker(final ObservableList seriesData) {
            setDaemon(true);
            setName("Thread Temp");
            this.seriesData = seriesData;
        }
 
        @Override
        public void run() {
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    IPConnection ipcon = new IPConnection();
                    BrickletTemperature temp 
                        = new BrickletTemperature(UID, ipcon);
                    try {
                        ipcon.connect(host, port);
                        temp.setTemperatureCallbackPeriod(1000);
                        temp.addTemperatureListener(
                          new BrickletTemperature.TemperatureListener() {
                            public void temperature(short temperature) {
                                Platform.runLater(new Runnable() {
                                    @Override
                                    public void run() {
                                        final double temp 
                                           = temperature / 100.0;
                                        final int counter 
                                           = seriesData.size() + 1;
                                        final XYChart.Data data 
                                           = new XYChart.Data(
                                                 new Date(), temp);
                                        seriesData.add(data);
                                    }
                                });
                            }
                        });
                    } catch (IOException | 
                             AlreadyConnectedException | 
                             TimeoutException | 
                             NotConnectedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
}

svenruppert_iot_tinkerforge_001_temp_001

 

Conclusion

==================================

As shown in this simple example we were able to connect to our first sensor and display the measured values.
The basic concept is relatively easy and the usage robust. In the following articles we will look at different sensors in
different environments.

May the IoT-Games begin….

The source code to this article can be found underhttps://bitbucket.org/rapidpm/jaxenter.de-0012-iot-tinkerforge .
For people who are interested in more complex examples I recommend the following https://bitbucket.org/rapidpm/modules.

Kommentare

  • Alexander Casall

    27. June 2014 von Alexander Casall

    Hi Sven,

    why don’t you use the JavaFX Services and Tasks? And why do you create a new Thread and then you perform all of the Thread logic in a Platform.runLater which will be performed in the UI Thread?

    Cheers,
    Alex

Comment

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