IoT with Java8 and TinkerForge Part 1

IoT is something cool, but I think you know it already.
Today I started with TinkerForge. TinkerForge is a german company that are building electronic elements. You can put them together like LEGO. This means, that you don´t need any special electronic stuff or knowledge.
And the best, you can code plain Java to use them.

But how we can start?

First of all, you need a MasterBrick and at minimumm one sensor. For my first experiment I am using the simple temperature sensor. After unpacking it I needed around 3 min to put all together. After this you can plug it into your USB port. Now the hardware part is ready.

To speak with this components you have to install two things.
First the usb-driver, that is used for the communication with the MasterBrick,
second the BrickViewr. With this you can check the installation, update the firmware and so on.

You will find the software for linux/osx/windows.

Hello IoT World: 

We will start with the first HelloWorld. Here with the Hello IoTWorld.
After you connected the MasterBrick with the USB-port you will see a blue light and the MasterBrick will be ready for communication.


Now we could start the BrickViewer to check the installation. The importand part is the UID from the sensor. With this we can connect it later. After you pressed the connect button, you can see all the informations.

At https://bitbucket.org/rapidpm/jaxenter.de-0012-iot-tinkerforge are all source codes you will need, including the TinkerForge API itself.
I spoke with TinkerForge and we decided to put the API into maven.

After we have done this, I will inform you. If you are interested, follow me on Twitter please ( @SvenRuppert )

The basic steps are quite simple to use this sensor.

  • create a IPConnection 
  • create an instance of the class BrickletTemperature 
  • configure the sensor 
  • add an ActionListener.

That is all.

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();
    }
}
After this short example we could do our first javafx test. We want to check the temperature over some time and show this inside an LineChart.



Here we are... and please note, the DateAxis is from http://myjavafx.blogspot.de/2013/09/javafx-charts-display-date-values-on.html
 
 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();
                    }
                }
            });
        }
    }
}

Short and simple.. Well this is a simple example, but the next step will be with more sensor-elements...
 stay tuned.. and happy coding.

Kommentare

Beliebte Posts