Beliebte Suchanfragen

Cloud Native

DevOps

IT-Security

Agile Methoden

Java

//

IoT with Java 8 and TinkerForge – 004

24.7.2014 | 5 minutes of reading time

Coffee in the office is often an insidious thing: First it is too hot, then too cold. Most of you will know this problem. We are going to tackle it today and sketch solution. Of course as always with JavaFX and the PTC-Bricklet from TinkerForge.

Software is made of coffee. At least I often hear things like that. It makes it even more infuriating that coffee never has the right temperature. This is very annoying – at least for me. At the beginning it is too hot so you wait and keep on doing your work.
Eventually you remember your coffee, take a sip – and then it is to cold. That does not have to be the case.

The PTC-Bricklet
================
The PTC-Bricklet from the TinkerForge tool kit measures temperatures via a Pt100– and Pt1000 element. This way 2, 3 and 4 conductor types are supported which provides great flexibility. The bricklet measures temperatureswith a resolution of 0,5% in a range of -246 to 849 degrees celsius. The values are decoded with 15 bit which equals a resolution of 0,03125?C which in turns displayed in 0,01 degree steps.
Now the resolution should be sufficient for most applications. Today i am using a PTC100 in our setup. It does not matter which one you use. But it should be a water proof version otherwise you might get a nasty surprise.

Using the PTC
=============
As usual (look at the previous articles) the bricklet is linked to the MasterBrick. Shortly after the sensor is shown in the display of the BrickViewer . It is always advisable to update the bricklet software via BrickViewer. Finally the sensor is useable. The PTC-Bricklet is also equipped with a Java interface which enables the implementation via ActionListener (listing1).

1public class PTC implements Runnable {
2 
3    private String UID;
4    private ObservableList seriesData;
5    private SevenSegment sevenSegment = new SevenSegment("iW3");
6 
7    public PTC(final String UID, final XYChart.Series series) {
8        this.UID = UID;
9        this.seriesData = series.getData();
10    }
11 
12    @Override
13    public void run() {
14        IPConnection ipcon = new IPConnection();
15        BrickletPTC ptc = new BrickletPTC(UID, ipcon);
16        try {
17            ipcon.connect(Localhost.HOST, Localhost.PORT);
18            ptc.setTemperatureCallbackPeriod(1000);
19            ptc.addTemperatureListener(
20              new BrickletPTC.TemperatureListener() {
21                @Override
22                public void temperature(int temperature) {
23                    //schreibe temp in 7Seg
24                    final double celcius = temperature / 100.0;
25                    System.out.println("celcius = " + celcius);
26 
27                    Platform.runLater(()->{
28                        final XYChart.Data data 
29                            = new XYChart.Data(new Date(), celcius);
30                        seriesData.add(data);
31                    });
32                    sevenSegment.printValue(celcius);
33 
34                }
35            });
36        } catch (IOException 
37            | AlreadyConnectedException 
38            | TimeoutException 
39            | NotConnectedException e) {
40            e.printStackTrace();
41        }
42    }
43}

Within the ActionListener the necessary values is delivered. In our case it is the temperature in degrees Celcius multiplied by the factor 100. The delivered value has to be divided by 100 so it can be displayed.

The 7 Segment Bricklet
======================
Now what do we do with the measured values? On the one hand we display them in a LineChart – like we did in the previous articles. However, there is also the 7 segment bricklet at which we will take a look today as well.
This bricklet provides four 7 segment displays. Each of the 29 segements can be switched on and off – and it can be adjusted in brightness. The segments can be encoded via bits or bit patterns. This way it is necessary to first convert the displayed values into the respective segment pattern. The documentation of TinkerForge provides typical bit patterns for the most common characters (also contained in the example code).

1public class SevenSegment {
2 
3    private String UID;
4    private BrickletSegmentDisplay4x7 sevenSegment;
5    private DecimalFormat myFormatter = new DecimalFormat("0000");
6 
7    private static final byte[] digits = {0x3f,0x06,0x5b,0x4f,
8            0x66,0x6d,0x7d,0x07,
9            0x7f,0x6f,0x77,0x7c,
10            0x39,0x5e,0x79,0x71}; // 0~9,A,b,C,d,E,F
11 
12    public SevenSegment(String UID) {
13        this.UID = UID;
14        final IPConnection ipcon = new IPConnection();
15        sevenSegment = new BrickletSegmentDisplay4x7(UID, ipcon);
16        try {
17            ipcon.connect(Localhost.HOST, Localhost.PORT);
18 
19            short[] segments = {digits[4], 
20                digits[2], digits[2], digits[3]};
21            sevenSegment.setSegments(segments, (short)7, false);
22 
23        } catch (IOException 
24            | AlreadyConnectedException 
25            | NotConnectedException 
26            | TimeoutException e) {
27            e.printStackTrace();
28        }
29    }
30 
31 
32    public void printValue(final Double value){
33        int intValue = value.intValue();
34        final char[] chars = myFormatter.format(intValue).toCharArray();
35        System.out.println(chars);
36 
37        final int aChar = chars[0];
38        final byte d1 = digits[aChar-48];
39        final byte d2 = digits[chars[1]-48];
40        final byte d3 = digits[chars[2]-48];
41        final byte d4 = digits[chars[3]-48];
42        short[] segments = {d1, d2, d3, d4};
43        try {
44            sevenSegment.setSegments(   segments   ,(short)7, false);
45        } catch (TimeoutException 
46                    | NotConnectedException e) {
47            e.printStackTrace();
48        }
49    }
50 
51}

In our case the conversion is fairly easy: Take the provided value of the PTC, divide it by 100 and create the necessary bit pattern. In our example i only show the numbers (including leading zeros) in front of the comma on the 7 segment display.
The formatting is done by an instance of the class DecimalFormat with the pattern “0000”.

Each number is then being transferred to the necessary bit pattern. After that the individual bit patterns are stored in a short-Array and passed to the sensor. This way the basic functionality is established.
Every time the PTC provides a value it is converted and transferred to the 7 segment display. Now the temperature can be read.

The usage

Now we have all parts together. The temperature can be measured and the value is shown in the LineChart as well as in the 7 segment display. Now only the test with the coffee itself is missing. So it is time to get a fresh hot coffee. Start the program and put the PTC in the coffee cup. In this example the temperature must be still observed manually. This does not solve the problem described in the beginning but is a first step towards it. Several steps will follow in the next article.

Listing 3 shows the complete JavaFX program.

1public class PTCMaster extends Application {
2 
3    public static XYChart.Series seriesTemp = new XYChart.Series();
4 
5    public static void main(String args[]) throws Exception {
6        launch(args);
7    }
8 
9    @Override
10    public void start(Stage stage) {
11        stage.setTitle("Line Chart TinkerForge Sample");
12 
13        final VBox box = new VBox();
14        seriesTemp.setName("Temp");
15        final ObservableList boxChildren = box.getChildren();
16        boxChildren.add(createLineChart("Temp", seriesTemp));
17        Scene scene = new Scene(box);
18 
19        stage.setScene(scene);
20        stage.show();
21        Platform.runLater(new PTC("i2J", seriesTemp));
22 
23    }
24 
25    private LineChart createLineChart(final String chartName,
26        final XYChart.Series series ){
27        final DateAxis dateAxis = new DateAxis();
28        dateAxis.setLabel("Time");
29        final NumberAxis yAxis = new NumberAxis();
30 
31        final LineChart lineChart 
32            = new LineChart<>(dateAxis, yAxis);
33        lineChart.setTitle(chartName);
34        lineChart.getData().add(series);
35 
36        return lineChart;
37    }
38 
39}

Conclusion
With the components you are able to build and use things within minutes. The reasonability of exactly this application is maybe questionable but one thing is for sure. You can build quickly prototypes with the TinkerForge elements. Due to the simple Java bindings an evaluation of the measured values is also available in a first version. In the next articles we are not only going to learn more elements but also develop more complex applications.
Stay tuned. Happy Coding!

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.