Beliebte Suchanfragen
//

IoT with Java 8 and TinkerForge – 003

10.7.2014 | 5 minutes of reading time

Today we are going to dive further into IoT. The focus is on the DualButton-Bricklet of TinkerForge. How can we communicate bidirectionally? What are the possibilities of interaction with the electronics? All this will be the preperation for further manual
elements.

We are used to the fact that we can have skin contact to the electronics. To be more precise: finger contact. The DualButton-Bricklet provides the possibility to send manual impulses which can be processed on the Java side. But also the way back to the bricklet
is possible. Today we are going to look at this more closely.

The DualButton-Bricklet
=======================
The DualButton-Bricklet can be linked to the MasterBrick like the others. This way we have two push buttons. Both buttons have a blue implemented LED. The current state of the button (pressed/not pressed) and the LED (on/off) can be read all the time.
Those states can also be set programmatically.

Usage of the Buttons
====================
The linking of the DualButton-Bricklet can also be achieved through the configuration of the encapsulating instance of the element and the implementation of the ActionListener (!kursiv) within which we get the values. Because of the two
seperate buttons on the bricklet there are basically two states “pressed” and “released”. Both states are represented by 0 for “released” and 1 for “pressed”. Similar to the previous articles we are going to put the bricklet and the ActionListener
in a seperate class which implements the interface Runnable . This class is called Worker .

1public class Worker implements Runnable{
2    @Override
3    public void run() {
4        try {
5            ipcon.connect(host, port);
6            final BrickletDualButton.StateChangedListener changedListener
7                    = (buttonL, buttonR, ledL, ledR) -> runLater(() -> {
8 
9                if (buttonL == BrickletDualButton.BUTTON_STATE_PRESSED) {
10                    setMsg("Left button pressed");
11                    activateLeftButton();
12                }
13                if (buttonL == BrickletDualButton.BUTTON_STATE_RELEASED) {
14                    setMsg("Left button released");
15                }
16                if (buttonR == BrickletDualButton.BUTTON_STATE_PRESSED) {
17                    setMsg("Right button pressed");
18                    activateRightButton();
19                }
20                if (buttonR == BrickletDualButton.BUTTON_STATE_RELEASED) {
21                    setMsg("Right button released");
22                }
23            });
24 
25            db.addStateChangedListener(changedListener);
26            db.setLEDState(LED_STATE_ON, LED_STATE_ON );
27 
28        } catch (IOException
29                | AlreadyConnectedException
30                | TimeoutException
31                | NotConnectedException e) {
32            e.printStackTrace();
33        }
34    }
35 
36    private void setMsg(String msg) {
37        tx.setText(msg.concat(newline).concat(tx.getText()));
38    }
39}

Within the ActionListener we again get the required values. In our case we get always both states of the buttons. If only the pressed-state is relevant we can easily check and interact on the individual state like shown in listing 1.
If you want to know when the taster has been released you also have to remember the last state change. This way you can get the difference over the timestamp. At first we set the state of both LEDs once to active/illuminated.
In our example both methods activateRightButton() and activateLeftButton() of the class worker have been moved to the class ButtonMaster . The only reason for this is that both methods (listing2) can be reused in another location.

1private void activateRightButton() {
2    try {
3        db.setLEDState(LED_STATE_OFF, LED_STATE_ON);
4        bL.setText("InActive");
5        bR.setText("Active");
6    } catch (TimeoutException | NotConnectedException e) {
7        e.printStackTrace();
8    }
9}
10 
11private void activateLeftButton() {
12    try {
13        db.setLEDState(LED_STATE_ON, LED_STATE_OFF);
14        bL.setText("Active");
15        bR.setText("InActive");
16 
17    } catch (TimeoutException | NotConnectedException e) {
18        e.printStackTrace();
19    }
20}

Within these methods the state of each LED is set on the one hand, on the other hand the text of both JavaFX buttons is set inverted (active/inActive). This way pressing a button is not just shown through the LEDs but also with the text of the two Buttons in
the JavaFX GUI. This way we have shown the path from the hardware to the GUI. But how does it go from JavaFX to the button?

Linking to JavaFX
=================

The linking of both buttons to JavaFX can basically be achieved the same way as in the articles before. Remember: The basic principal is that the JavaFX application is started out of a thread. The latter configures the element and adds the ActionListener in the run()-method (!kursiv). Important is that the parts which are updating JavaFX-GUI elements have to be encapsulated in Platform.runLater(). The call is then quite easy:

1public class ButtonMaster extends Application {
2    private static final String host = "localhost";
3    private static final int port = 4223;
4    private static final String UID = "j5K";
5    public static String newline = System.getProperty("line.separator");
6 
7    private final IPConnection ipcon = new IPConnection();
8    private final BrickletDualButton db 
9        = new BrickletDualButton(UID, ipcon);
10 
11    public static void main(String[] args) {
12        launch(args);
13    }
14 
15    public Button bL;
16    public Button bR;
17 
18    public TextArea tx;
19 
20    @Override
21    public void start(Stage stage) {
22        stage.setTitle("Button TinkerForge Sample");
23 
24        final VBox vBox = new VBox();
25        setAnchorZero(vBox);
26        final HBox hBox = new HBox();
27        setAnchorZero(hBox);
28        bL = new Button("LED links");
29        bR = new Button("LED rechts");
30 
31        setAnchorZero(bL);
32        setAnchorZero(bR);
33 
34        bL.setOnAction(actionEvent -> activateLeftButton());
35        bR.setOnAction(actionEvent -> activateRightButton());
36 
37        hBox.getChildren().add(bL);
38        hBox.getChildren().add(bR);
39 
40        vBox.getChildren().add(hBox);
41 
42        tx = new TextArea();
43        vBox.getChildren().add(tx);
44 
45 
46        Scene scene = new Scene(vBox);
47        stage.setScene(scene);
48 
49        runLater(new Worker());
50 
51        stage.show();
52 
53    }
54 
55    private void setAnchorZero(final Node node) {
56        AnchorPane.setBottomAnchor(node, 0.0);
57        AnchorPane.setLeftAnchor(node, 0.0);
58        AnchorPane.setRightAnchor(node, 0.0);
59        AnchorPane.setTopAnchor(node, 0.0);
60    }
61 
62 
63    public class Worker implements Runnable {
64        @Override
65        public void run() {
66            try {
67                ipcon.connect(host, port);
68 
69                db.addStateChangedListener(
70                  (buttonL, buttonR, ledL, ledR) -> runLater(() -> {
71                    if (buttonL == BUTTON_STATE_PRESSED) {
72                        setMsg("Left button pressed");
73                        activateLeftButton();
74                    }
75                    if (buttonL == BUTTON_STATE_RELEASED) {
76                        setMsg("Left button released");
77                    }
78                    if (buttonR == BUTTON_STATE_PRESSED) {
79                        setMsg("Right button pressed");
80                        activateRightButton();
81                    }
82                    if (buttonR == BUTTON_STATE_RELEASED) {
83                        setMsg("Right button released");
84                    }
85                }));
86                db.setLEDState(LED_STATE_ON, LED_STATE_ON);
87            } catch (IOException
88                    | AlreadyConnectedException
89                    | TimeoutException
90                    | NotConnectedException e) {
91                e.printStackTrace();
92            }
93        }
94        private void setMsg(String msg) {
95            tx.setText(msg.concat(newline).concat(tx.getText()));
96        }
97    }
98 
99    private void activateRightButton() {
100        try {
101            db.setLEDState(LED_STATE_OFF, LED_STATE_ON);
102            bL.setText("InActive");
103            bR.setText("Active");
104        } catch (TimeoutException | NotConnectedException e) {
105            e.printStackTrace();
106        }
107    }
108 
109    private void activateLeftButton() {
110        try {
111            db.setLEDState(LED_STATE_ON, LED_STATE_OFF);
112            bL.setText("Active");
113            bR.setText("InActive");
114        } catch (TimeoutException | NotConnectedException e) {
115            e.printStackTrace();
116        }
117    }
118}

Now we show the way from the JavaFX-Buttons to the hardware. When pushing one of the two software buttons the corresponding LED is activated. The only thing that is required for that is the call of the intended method activate(Left/Right)Button().
Now we have a flow of the GUI to the hardware and back. Here we have to note that the pushing of the software Button does not move the physical button. You can see the full JavaFX application in listing3.

Conclusion
==========
With the DualButton-Bricklet for the first time we have the possibility to start manually software processes via mechanical button. We are going to show in detail in the next articles what you can realize with that. Until then: Stay tuned and happy coding.

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

//

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.