Beliebte Suchanfragen

Cloud Native

DevOps

IT-Security

Agile Methoden

Java

//

IoT with Java8 and TinkerForge – 001

26.6.2014 | 5 minutes of reading time

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.

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.

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}

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.

1public class HelloTinkerForge extends Application {
2 
3    private static final String host = "localhost";
4    private static final int port = 4223;
5    private static final String UID = "dXj"; 
6 
7 
8    public static void main(String args[]) throws Exception {
9        launch(args);
10    }
11 
12    public static XYChart.Series series;
13 
14    @Override
15    public void start(Stage stage) {
16        stage.setTitle("Line Chart TinkerForge Sample");
17        final DateAxis dateAxis = new DateAxis();
18        final NumberAxis yAxis = new NumberAxis();
19        dateAxis.setLabel("Time of Temp");
20        final LineChart lineChart 
21           = new LineChart<>(dateAxis, yAxis);
22 
23        lineChart.setTitle("Temp Monitoring");
24 
25        series = new XYChart.Series();
26        series.setName("My temps");
27        final ObservableList seriesData = series.getData();
28 
29        lineChart.getData().add(series);
30        Scene scene = new Scene(lineChart, 800, 600);
31        stage.setScene(scene);
32        stage.show();
33        new Worker(seriesData).start();
34 
35    }
36 
37    public static class Worker extends Thread {
38        final ObservableList seriesData;
39        public Worker(final ObservableList seriesData) {
40            setDaemon(true);
41            setName("Thread Temp");
42            this.seriesData = seriesData;
43        }
44 
45        @Override
46        public void run() {
47            Platform.runLater(new Runnable() {
48                @Override
49                public void run() {
50                    IPConnection ipcon = new IPConnection();
51                    BrickletTemperature temp 
52                        = new BrickletTemperature(UID, ipcon);
53                    try {
54                        ipcon.connect(host, port);
55                        temp.setTemperatureCallbackPeriod(1000);
56                        temp.addTemperatureListener(
57                          new BrickletTemperature.TemperatureListener() {
58                            public void temperature(short temperature) {
59                                Platform.runLater(new Runnable() {
60                                    @Override
61                                    public void run() {
62                                        final double temp 
63                                           = temperature / 100.0;
64                                        final int counter 
65                                           = seriesData.size() + 1;
66                                        final XYChart.Data data 
67                                           = new XYChart.Data(
68                                                 new Date(), temp);
69                                        seriesData.add(data);
70                                    }
71                                });
72                            }
73                        });
74                    } catch (IOException | 
75                             AlreadyConnectedException | 
76                             TimeoutException | 
77                             NotConnectedException e) {
78                        e.printStackTrace();
79                    }
80                }
81            });
82        }
83    }
84}

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.

share post

Likes

0

//

More articles in this subject area

Discover exciting further topics and let the codecentric world inspire you.

//

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.