Beliebte Suchanfragen

Cloud Native

DevOps

IT-Security

Agile Methoden

Java

//

IoT with Java 8 and TinkerForge – 002

3.7.2014 | 5 minutes of reading time

The last article of this series has covered an introduction to the hardware platform TinkerForge. This time we are going to handle a more specific example. One of the important features of TinkerForge is the possibility to link elements like lego-parts. And exactly that is what we demonstrate with two sensors today. Additionally we show you how can analyze in parallel two or more sensors can be analyzed parallel. The measured values will be analyzed in realtime and afterwards are shown in a simple JavaFX application.

The barometer sensor
====================
In this setup one MasterBrick is linked with two sensors. The first one is known from the first article of this series: It is the temperature sensor  . Along with this we are adding the barometer sensor. With this bricklet you can measure the barometric pressure in range of 10 to 1200 mbar with a resolution of 0.012mbar whereby the measurement is temperature compensated internally. The bricklet is based on a MS5611-01BA01 sensor which also can be used as an altimeter. And with that we have an exception in comparison to the temperature sensor. The barometer sensor provides two measurements.

Linking of n sensors
====================
The link process of each sensor can be done with the configuration of an encapsulating sensor class and the implementation of a corresponding ActionListeners. Within the last the measured values can be analyzed. At this point Listing 1 is shown as a short reminder.

1import com.tinkerforge.BrickletTemperature;
2import com.tinkerforge.IPConnection;
3 
4public class ExampleCallback {
5    private static final String host = "localhost";
6    private static final int port = 4223;
7    private static final String UID = "dXj"; 
8    public static void main(String args[]) throws Exception {
9        IPConnection ipcon = new IPConnection(); 
10        BrickletTemperature temp = new BrickletTemperature(UID, ipcon); 
11        ipcon.connect(host, port); 
12        temp.setTemperatureCallbackPeriod(1000);
13        temp.addTemperatureListener(new
14          BrickletTemperature.TemperatureListener() {
15            public void temperature(short temperature) {
16                System.out.println("Temperature: "
17                   + temperature/100.0 + " °C");
18            }
19        });
20        ipcon.disconnect();
21    }
22}

It is the same way with the barometer sensor. There is also an encapsulating class, in this case the BrickletBarometer . As shown in Listing 2 the configuration is almost identical to the Listing1 except the UID. The reason for that is that both sensors are linked to the same MasterBrick . Additionally it is shown how both measurements can be processed. It is really simple because there exists an ActionListener for each measurement.

1public class ExampleCallback {
2    private static final String host = "localhost";
3    private static final int port = 4223;
4    private static final String UID = "jY4";
5    public static void main(String args[]) throws Exception {
6        IPConnection ipcon = new IPConnection(); 
7        BrickletBarometer b = new BrickletBarometer(UID, ipcon); 
8        ipcon.connect(host, port); 
9        b.setAirPressureCallbackPeriod(1000);
10        b.setAltitudeCallbackPeriod(1000);
11        b.addAirPressureListener(
12            new BrickletBarometer.AirPressureListener() {
13                public void airPressure(int airPressure) {
14                    System.out.println("Air Pressure: "
15                        + airPressure/1000.0 + " mbar");
16                }
17            }
18        );
19        b.addAltitudeListener(new BrickletBarometer.AltitudeListener() {
20            public void altitude(int altitude) {
21                System.out.println("Altitude: " + altitude/100.0 + " m");
22            }
23        });
24        ipcon.disconnect();
25    }
26}

Linking of JavaFX
=================
The link process of the sensors to JavaFX can be reached fairly easy. The basic principal is that a thread is started out of the JavaFX application in which the sensor is configured and the ActionListener is called (within the run()). Important with that is that all parts which will update JavaFX gui components must be encapsulated within Platform.runLater. Because we have more than one sensor in this example every sensor is outsourced in its own class. Listing3 shows the code for the temperature sensor. The usage is then fairly easy:

1public static class Temp implements Runnable {
2 
3        private String UID;
4        private ObservableList seriesData;
5 
6        public Temp(final String UID, final XYChart.Series series) {
7            this.UID = UID;
8            this.seriesData = series.getData();
9        }
10 
11        @Override
12        public void run() {
13            IPConnection ipcon = new IPConnection();
14            BrickletTemperature temp = new BrickletTemperature(UID, ipcon);
15 
16            try {
17                ipcon.connect(host, port);
18                temp.setTemperatureCallbackPeriod(1000);
19                temp.addTemperatureListener(
20                  new BrickletTemperature.TemperatureListener() {
21                    public void temperature(short temperature) {
22                        Platform.runLater(new Runnable() {
23                            @Override
24                            public void run() {
25                                final double temp = temperature / 100.0;
26                                System.out.println("Temperature: " + temp 
27                                    + " °C");
28                                final XYChart.Data data 
29                                    = new XYChart.Data(new Date(), temp);
30                                seriesData.add(data);
31                            }
32                        });
33                    }
34                });
35            } catch (IOException 
36                       | AlreadyConnectedException 
37                       | TimeoutException 
38                       | NotConnectedException e) {
39                e.printStackTrace();
40            }
41        }
42    }

For the barometer two of those classes are written: the class Luftdruck and the class Altitude. Both of them are equal except the ActionListener and both of them put der measured values in their own series. Every series is shown in its own LineChart. In this implementation it is approved that for each physical sensor two representations per thread exists. In the tests this combinations was stable. Additionally all examples i have refrained from closing the sensors via disconnect() when quitting the JavaFX-application.In Listing 4 you can see the JavaFX program.

1public class Barometer extends Application {
2    public static final String host = "localhost";
3    public static final int port = 4223;
4 
5 
6    public static void main(String args[]) throws Exception {
7        launch(args);
8    }
9 
10    public static XYChart.Series seriesTemp = new XYChart.Series();
11    public static XYChart.Series seriesLuftdruck = new XYChart.Series();
12    public static XYChart.Series seriesAltitude = new XYChart.Series();
13 
14    @Override
15    public void start(Stage stage) {
16        stage.setTitle("Line Chart TinkerForge Sample");
17 
18        final VBox box = new VBox();
19        seriesTemp.setName("Temp");
20        seriesLuftdruck.setName("Luftdruck");
21        seriesAltitude.setName("Altitude");
22 
23        final ObservableList boxChildren = box.getChildren();
24        boxChildren.add(createLineChart("Temp", seriesTemp));
25        boxChildren.add(createLineChart("Luftdruck", seriesLuftdruck));
26        boxChildren.add(createLineChart("Altitude", seriesAltitude));
27 
28        Scene scene = new Scene(box, 2000, 1500);
29 
30        stage.setScene(scene);
31        stage.show();
32        Platform.runLater(new Temp("dXj", seriesTemp));
33        Platform.runLater(new Luftdruck("jY4", seriesLuftdruck));
34        Platform.runLater(new Altitude("jY4", seriesAltitude));
35 
36    }
37 
38    private LineChart createLineChart(
39        final String chartName,
40        final XYChart.Series series ){
41 
42        final DateAxis dateAxis = new DateAxis();
43        dateAxis.setLabel("Time");
44        final NumberAxis yAxis = new NumberAxis();
45 
46        final LineChart lineChart 
47            = new LineChart<>(dateAxis, yAxis);
48        lineChart.setTitle(chartName);
49        lineChart.getData().add(series);
50 
51        return lineChart;
52    }
53}

Conclusion

In this article we have described the first steps in the direction of the analyzation of n-sensors. In these tests you can quickly see that a middle freq of 1 measurment/second already leads to some percent CPU-load. Therefore in this form the scalarility is limited. In the next articles we will deal with the abstraction of the sensors as JavaFX-GUI-elements and with the storage of values in DBMS (SQL/noSQL).

The source code to this article can be found under https://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 .

share post

Likes

0

//

Gemeinsam bessere Projekte umsetzen.

Wir helfen deinem Unternehmen.

Du stehst vor einer großen IT-Herausforderung? Wir sorgen für eine maßgeschneiderte Unterstützung. Informiere dich jetzt.

Hilf uns, noch besser zu werden.

Wir sind immer auf der Suche nach neuen Talenten. Auch für dich ist die passende Stelle dabei.