Skip to main content
  1. Posts/

IoT with TinkerForge and Java

·13 mins
Sven Ruppert
Author
Sven Ruppert
20+ years of Java, specialised in Security, Vaadin and Developer Relations. When not coding, you’ll find me in the woods with an axe.
Table of Contents

Introduction
#

TinkerForge is an innovative and modular hardware system that allows users to build electronic devices quickly and flexibly. Whether you’re an experienced engineer, a hobbyist, or a complete newbie, TinkerForge offers a range of components that can be easily connected and programmed, allowing for rapid prototyping and the creation of custom electronics projects. Since its launch, TinkerForge has gained popularity in various areas, including education, research, and industrial automation, due to its user-friendly design and extensive feature set.

  1. Introduction
  2. The origins and evolution of TinkerForge
  3. TinkerForge architecture
    1. Bricks
    2. Bricklets
    3. Master-Extensions
  4. Programming and control
    1. TinkerForge-API
    2. TinkerForge-GUI
    3. Remote access and control
  5. Applications of TinkerForge
    1. Training
    2. Prototyping
    3. Industrial automation
    4. DIY projects and maker community
  6. Advantages of TinkerForge
  7. Limitations and challenges
  8. How do I start programming TinkerForge electronics with Java?
    1. Requirements
    2. TinkerForge architecture
    3. Connecting your TinkerForge hardware
      1. Connect Bricklets to the Master Brick
      2. Connect the master brick to the computer.
    4. Installing the TinkerForge Java API
    5. Writing the first Java program
      1. Running and testing the program
      2. Dealing with errors
      3. Event-driven programming
      4. Controlling actors
    6. Conclusion

The origins and evolution of TinkerForge
#

TinkerForge was founded in 2011. The founders Matthias Bolte and Olaf Lüke imagined a system that would lower the hurdles in hardware development. Their goal was to provide a range of components that could be easily combined into complex systems without requiring in-depth knowledge of electronics or soldering skills.

The idea arose from the frustration the founders experienced while working on electronics projects, which often required a lot of time just to get the essential components to work together. They recognised that there was a need for a more intuitive and modular system that would allow anyone to create hardware projects quickly and effectively.

Over the years, TinkerForge has grown from a niche product into a widely used platform supported by a vibrant community of developers and manufacturers. The system is now used in various applications, from simple DIY projects to advanced industrial automation systems.

TinkerForge architecture
#

The TinkerForge system is based on a modular architecture consisting of three main component types: Bricks, Bricklets and Master Extensions.

Bricks
#

Bricks are the core building blocks of the TinkerForge system. They are small, self-contained modules that provide specific functions, such as controlling motors, reading sensors, or connecting to a computer. Each brick has a specific purpose and can be easily attached using a stackable system.

The most common types of bricks include:

Master Brick : The Master Brick is the central node that controls and supplies power to other Bricks and Bricklets. It provides the communication interface between the TinkerForge stack and a computer or microcontroller.

ESP32 Brick : The ESP32 Brick offers six Bricklet Connections and is equipped with a powerful ESP32 microcontroller.

HAT-Brick : With the HAT Brick up to act, bricklets can be connected to a Raspberry Pi.

IMU-Brick 2.0 : The Inertial Measurement Unit Brick contains a 9 DOF (Degrees of Freedom) sensor that provides orientation data, making it suitable for navigation and motion-tracking applications.

Bricklets
#

Bricklets are smaller modules that connect to bricks to expand their functionality. They are essentially sensors, actuators, or other peripheral devices that can be connected to a brick to provide additional input or output options. Bricklets are connected to Bricks via standardised connectors, and each can support multiple Bricklets.

Some examples of Bricklets are:

Temperature Bricklet : This module measures temperature and can be used in projects requiring environmental monitoring.

Distance IR Bricklet : This sensor measures the distance to an object using infrared light, which is helpful in robotics or object detection applications.

LCD 20x4 Bricklet : A simple display module that enables text output, perfect for building user interfaces or displaying sensor data.

Master-Extensions
#

Master Extensions are additional boards that can be connected to the master brick to provide additional functionality or expand connectivity. These extensions allow the TinkerForge system to connect to various communication protocols or external devices.

Examples of master extensions are:

WIFI extension 2.0 : This module adds wireless communication capabilities to the TinkerForge system, allowing bricks to be controlled remotely via a computer or smartphone.

RS485 extension : This extension enables long-distance communication via RS485, making it suitable for industrial environments where long-distance wired connections are required.

Ethernet extension : This extension provides a wired Ethernet connection to the TinkerForge system, ensuring reliable and fast communication in networked environments.

Programming and control
#

One of TinkerForge’s outstanding features is its ease of programming. The system is designed to be accessible to users of all experience levels. TinkerForge offers various ways to control and program the Bricks and Bricklets, ensuring users can choose the best method.

TinkerForge-API
#

TinkerForge provides a powerful and easy-to-use application programming interface (API) that allows users to control the Bricks and Bricklets from various programming languages ​​, including Python, C, C++, Java, Ruby and more. The API abstracts the complexity of hardware interaction and simplifies writing code that interacts with the various components.

The API is well-documented and includes examples for each supported language. This documentation provides detailed explanations of each feature, making it easier for users to understand how to control the hardware.

https://www.tinkerforge.com/en/doc/Software/API_Bindings_Java.html

TinkerForge-GUI
#

TinkerForge also offers a graphical user interface (GUI) called Brick Viewer for users who prefer a more visual approach. This software visually represents the connected Bricks and Bricklets and allows users to configure, monitor and control their devices without writing code.

Brick Viewer is handy for beginners who are just starting with the TinkerForge system. It allows users to experiment with the hardware, view sensor data in real-time, and adjust settings without understanding the underlying code.

https://www.tinkerforge.com/de/doc/Software/Brickv.html#brickv

Remote access and control
#

TinkerForge also supports remote access and control, allowing users to interact with their devices over a network. The WIFI or Ethernet Master extensions allow users to control their TinkerForge systems over the Internet from anywhere in the world. This feature is precious for projects that require remote monitoring or control, such as home automation systems or remote sensing applications.

Applications of TinkerForge
#

TinkerForge’s versatility has led to its use in various applications. The most common areas of use for TinkerForge include:

Training
#

TinkerForge is an excellent tool for electronics and programming lessons. Its modular design allows students to experiment with different components and see immediate results. The ease of use and extensive documentation make it accessible to learners of all ages.

Educational institutions often use TinkerForge in robotics, embedded systems, and computer science courses. It allows students to create projects demonstrating key concepts such as sensor integration, motor control and data communication.

Prototyping
#

One of TinkerForge’s strengths is its ability to enable rapid prototyping. Engineers and designers can quickly assemble and test their ideas without complex wiring or soldering. The system’s modular design means that components can be easily replaced or reconfigured as needed.

TinkerForge is often used in the early stages of product development, where speed and flexibility are crucial. Using TinkerForge, developers can iterate their designs faster, reducing the time to market for new products.

Industrial automation
#

TinkerForge is also used in industrial automation applications where reliability and scalability are important. The system is suitable for use in industrial environments by supporting various communication protocols, such as RS485 and Ethernet.

In these environments, TinkerForge components can monitor and control machines, collect data from sensors, and automate processes. The modular design allows for easy expansion and customisation, making building complex systems tailored to specific requirements possible.

DIY projects and maker community
#

TinkerForge has a strong following among DIYers and hobbyists. The system’s simplicity and flexibility make it ideal for creating custom electronics projects, such as home automation systems, robotics, and art installations.

The maker community around TinkerForge is active, and users share their projects, code and ideas online. This community-focused approach has led to the development of a wide range of open-source projects and resources, further increasing the value of the TinkerForge ecosystem.

Advantages of TinkerForge
#

TinkerForge offers several advantages that have contributed to its popularity:

Modularity : The ability to mix and match components allows users to create highly customised systems without designing everything from scratch.

Ease of use : With a straightforward API and visual interface, TinkerForge is accessible to users of all experience levels.

Scalability : TinkerForge can be used in small, simple projects and large, complex systems. Its modular design allows it to be easily expanded as needed.

Cross-platform support : TinkerForge’s API is available in multiple programming languages, making it easy to integrate into existing projects.

Community and support : A strong community and comprehensive documentation ensure users have access to the resources they need to succeed.

Limitations and challenges
#

Although TinkerForge is a powerful and flexible system, it also has its limitations:

Costs : Although the price is reasonable, building complex systems with TinkerForge can be expensive, especially when multiple bricks and brackets are required.

Limited computing power : Some bricks, especially the master brick, may not have the computing power required for demanding applications. In such cases, an external microcontroller or computer may be required.

Learning curve : Although TinkerForge is designed to be user-friendly, there is still a learning curve, especially for those new to electronics or programming.

Dependence on proprietary hardware : TinkerForge components are proprietary, meaning users are somewhat locked into the ecosystem. While the open API and documentation help mitigate this, it is worth considering for long-term projects.

TinkerForge represents a significant advancement in the field of modular electronics, providing a platform that bridges the gap between hardware and software. Its user-friendly design, modular architecture, and extensive documentation make it an ideal choice for a wide range of users.

How do I start programming TinkerForge electronics with Java?
#

Requirements
#

Before diving into Java programming with TinkerForge, you must have the following:

Basic knowledge of Java : Familiarity with Java syntax and programming concepts.

TinkerForge-Hardware : At least one Meister Brick and a Bricklet (e.g. Temperature Bricklet) for interaction.

Computer with USB port : To connect the master brick to the computer.

Internet connection : To download the required software and libraries.

TinkerForge architecture
#

Before programming, it is essential to understand the vital components of the TinkerForge system:

Master Bricks : The central controller that connects to your computer via USB. It manages communication with all connected Bricklets.

Bricklets : Modular components that fulfil specific functions (e.g. sensors, actuators). They are connected to the master brick via standardised connectors.

Stackable System : Multiple bricks and bricklets can be stacked to create complex systems without soldering.

Connecting your TinkerForge hardware
#

Connect Bricklets to the Master Brick
#

First, the Bricklets should be connected to the Master Brick. Care should be taken to ensure that the connection is tight and that the plugs’ pins are aligned correctly.

Example setup :

  • Master Brick: Connected via USB.
  • Temperature Bricklet: Connected to one of the ports of the master brick.

Connect the master brick to the computer.
#

After connecting the master brick to the computer using a USB cable, it should boot up, as indicated by the LEDs lighting up.

Installing the TinkerForge Java API
#

To interact with TinkerForge hardware via Java, you need the TinkerForge Java API. This example uses Maven to manage the dependencies. The coordinates for the TinkerForge dependencies are:

<dependency>
    <groupId>com.tinkerforge</groupId>
    <artifactId>tinkerforge</artifactId>
    <version>2.1.33</version>
</dependency>

Writing the first Java program
#

The following example shows a simple Java program that reads temperature data from the Temperature Bricklet. For this purpose, a Java class called “TemperatureReader ` created. The following imports are necessary for this program. Typically, the IDE should automatically display the relevant suggestions.

import com.tinkerforge.BrickletTemperature;
import com.tinkerforge.IPConnection;
import com.tinkerforge.TinkerforgeException;

The temperature bracket’s UID is required below. The UID is usually printed on the bricklet or can be found via the TinkerForge Brick Viewer.

public class TemperatureReader {
    private static final String HOST = "localhost";
    private static final int PORT = 4223;
    // Replace XYZ with your Bricklet UID
    private static final String TEMPERATURE_BRICKLET_UID = "XYZ"; 
}

For simplicity, the actual program is implemented directly in the main method of the class.

public static void main(String[] args) {
    IPConnection ipcon = new IPConnection(); // Create IP connection
    // Create device object
    BrickletTemperature temp = new BrickletTemperature(TEMPERATURE_BRICKLET_UID, ipcon); 
    try {
        ipcon.connect(HOST, PORT); // Connect to brickd
        System.out.println("Connected to TinkerForge");
        // Read temperature
        short temperature = temp.getTemperature();
        System.out.println("Temperature: " + temperature / 100.0 + " °C");
        ipcon.disconnect();
    } catch (TinkerforgeException e) {
        System.err.println("Error: " + e.getMessage());
        e.printStackTrace();
    }
}

note : Replace “XYZ” with the actual UID of the temperature bricklet.

IP connection : Manages the connection to the TinkerForge daemon (**brickd** ), which facilitates communication between the computer and the bricks/bricklets.

BrickletTemperature : Represents the temperature bricklet element and allows interaction with its functions.

getTemperature(): This function gets the current temperature in hundredths of a degree Celsius. The value is converted to degrees Celsius by dividing by 100.0.

Running and testing the program
#

Start the TinkerForge daemon (“brickd”).

Before running the Java program, make sure the TinkerForge daemon is running. This daemon manages communication between the computer and the TinkerForge hardware.

Running the Java program

After starting the method TemperatureReader.main() ’’ you can see the following output on the console.

Connected to TinkerForge

Temperature: 23,45 °C

Of course, the temperature value varies depending on the environment.

Dealing with errors
#

If errors occur, note the following:

Incorrect UID : Make sure the UID in the source code matches the UID of the Temperature Bricklet.

Daemon is not running : Check if the **brickd** is running and connected to the master brick.

Connection problems : Check the USB connections and ensure the master brick is properly connected to the computer.

Event-driven programming
#

Now that a simple Java program for reading temperature data has been created, advanced features can be explored. Instead of actively querying temperature data, callbacks can be set up to receive data asynchronously.

import com.tinkerforge.BrickletTemperature;
import com.tinkerforge.IPConnection;
import com.tinkerforge.TinkerforgeException;
public class TemperatureCallback {
    private static final String HOST = "localhost";
    private static final int PORT = 4223;
    private static final String TEMPERATURE_BRICKLET_UID = "XYZ"; // Replace XYZ with your Bricklet UID
    public static void main(String[] args) {
        IPConnection ipcon = new IPConnection(); // Create IP connection
        // Create device object
        BrickletTemperature temp = new BrickletTemperature(TEMPERATURE_BRICKLET_UID, ipcon); 
        try {
            ipcon.connect(HOST, PORT); // Connect to brickd
            System.out.println("Connected to TinkerForge");
            // Register temperature callback
            temp.addTemperatureListener((temperature) -> {
                System.out.println("Temperature Callback: " + temperature / 100.0 + " °C");
            });
            // Set callback period to 1 second (1000 ms)
            temp.setTemperatureCallbackPeriod(1000);
            // Keep the program running
            System.out.println("Press Ctrl+C to exit.");
            while (true) {
                Thread.sleep(1000);
            }
        } catch (TinkerforgeException | InterruptedException e) {
            System.err.println("Error: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

Explanation:

addTemperatureListener : Registers a callback function that fires every time the temperature is updated.

setTemperatureCallbackPeriod : Defines the interval (in milliseconds) at which the callback is invoked.

Controlling actors
#

If actuators such as LEDs or motors have been connected via Bricklets, they can be controlled programmatically.

Example: Controlling an LED bricklet

import com.tinkerforge.BrickletLED;
import com.tinkerforge.IPConnection;
import com.tinkerforge.TinkerforgeException;
public class LEDControl {
    private static final String HOST = "localhost";
    private static final int PORT = 4223;
    // Replace ABC with your LED Bricklet UID
    private static final String LED_BRICKLET_UID = "ABC"; 
    public static void main(String[] args) {
        IPConnection ipcon = new IPConnection(); // Create IP connection
        BrickletLED led = new BrickletLED(LED_BRICKLET_UID, ipcon); // Create device object
        try {
            ipcon.connect(HOST, PORT); // Connect to brickd
            System.out.println("Connected to TinkerForge");
            // Turn LED on
            led.setColorLEDValue("on");
            System.out.println("LED is ON");
            // Wait for 2 seconds
            Thread.sleep(2000);
            // Turn LED off
            led.setColorLEDValue("off");
            System.out.println("LED is OFF");
            ipcon.disconnect();
        } catch (TinkerforgeException | InterruptedException e) {
            System.err.println("Error: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

Conclusion
#

Programming with Java on the TinkerForge platform provides a powerful way to create interactive and intelligent electronic projects. There are hardly any limits to your imagination here. Thanks to the connection to Java, you can link your projects with many other technologies, which dramatically simplifies the integration of existing infrastructures and projects.

Happy coding and creating!

Related

Building More Complex Apps with Flow

·11 mins
In this part of the series about Vaadin Flow, I will show how I can create the basic framework for the graphic design of a work application. The focus here is on the design of the work area and the organisation of the individual logical application groups. In other words, we create the application layout that can be used for an industrial project.

Vaadin Flow - How to start

We will now create a new Vaadin Flow application step by step and create a basic framework for our own projects with this component-based open-source web framework. So, right from the start, the question arises: How can you start with as little effort as possible without avoiding the usual expenses that sometimes come with creating projects?

Comparing Code Coverage Techniques: Line, Property-Based, and Mutation Testing

What is Test Coverage? # Test coverage is a metric used in software testing to measure the testing performed on a piece of software. It indicates how thoroughly a software program has been tested by identifying which parts of the code have been executed (covered) during testing and which have not. Here are the key aspects of test coverage:

Securing Apache Maven: Understanding Cache-Related Risks

What is a Package Manager - Bird-Eye View # A package manager is a tool or system in software development designed to simplify the process of installing, updating, configuring, and removing software packages on a computer system. It automates managing dependencies and resolving conflicts between different software components, making it easier for developers to work with various libraries, frameworks, and tools within their projects.