/tinkerforge-sensor

This Library is simplifying the API usage in a pure Java 8 way without any Frameworks. Removes pain of the Sensor UID, how to speak to the sensor and what values can i get from it etc. so that the focus is more on the logic

Primary LanguageJavaApache License 2.0Apache-2.0

Tinkerforge Sensor Library

This library simplifies Tinkerforge's sensor API. It Removes the pain of sensor UIDs, sensor versions, ports and provides a generic API for every sensor.

Sensor documentation

A list of all sensors behaviors and more can be found in the ReadmeDoc

Hackerschool sessions (berlin)

Examples

final Sensor display = stack.sensors().displaySegment();
display.send("GIRL");
display.sendLimit(2, "YOU"); //Sends only two messages in a second (useful for loops)
  • Example getting multiple buttonRGB from Stack.Sensors <- (The order comes from the connected port in the stack - higher port/stack = higher orderNumber)
final Sensor button_01 = stack.sensors().buttonRGB(0);
final Sensor button_02 = stack.sensors().buttonRGB(1);
final Sensor button_03 = stack.sensors().buttonRGB(2);
final Sensor display = stack.sensors().displaySegment();
display.compare().is(display); //returns true - UID is the same
display.compare().is(DisplaySegment.class); //returns true - ClassType is the same
display.compare().isDisplaySegment(); //returns true - obviously
display.compare().isLightAmbient(); //returns false - obviously
  • Example getting Values from stack
stack.values().temperature();
stack.values().temperature_Min();
stack.values().temperature_Max();
stack.values().temperature_Avg();
final Sensor distanceIR = stack.sensors().distanceIR();
distanceIR.values().distance();
distanceIR.values().distance_Min(); //This is only a measure of a short time;
distanceIR.values().distance_Max(); //This is only a measure of a short time;
distanceIR.values().distance_Avg(); //This is only a measure of a short time;
  • Example other Sensor functions - all functions are safe to use and does not produce any user exceptions even if a Sensor were never connected
final Sensor distanceIR = stack.sensors().distanceIR();
distanceIR.isPort(); //returns stack-order-number (higher stack/port = higher number)
distanceIR.isBrick(); //returns false 'DistanceIR' is a Bricklet
distanceIR.isHasLedStatus(); //returns true if the sensor has a status LED
distanceIR.isPresent(); //true if a sensor is connected
distanceIR.ledStatusOn(); //Switch status LED on (On/Off/Heartbeat/Status)
distanceIR.ledAdditionalOn(); //Switch other LEDs on - can be Display-backLight, color-FlashLight, IMU-orientation-LEDs,... (On/Off/Heartbeat/Status)
distanceIR.refreshLimit(6); //(callback period 6/sec) sets the refresh value rate in a second - e.g. for power issues
// [...]
  • event example
private void onSensorEvent(final Sensor currentSensor, final ValueType valueType, final Long value) {
    Sensor display = stack.sensors().getSegmentDisplay();
    
    if (valueType.isMotionDetected() and value == 1) {
        console("Motion detected");
        console("A lux value from any sensor: " + stack.values().lightLux());
        console("Current sensor is new motion detection sensor? " + currentSensor.is(BrickletMotionDetectorV2.class));
        currentSensor.ledStatusOn();
        display.ledAdditionalOn();
        display.value("Off");
    
    } else if (valueType.isMotionDetected()) {
        console("No motion detected");
        currentSensor.ledStatusOff();
        display.ledAdditionalOff;
        display.value("On");
    }
}
  • Connecting with auto closeable Stack
try (Stack stack = new Stack("host", 4223, "optionalPassword")) {
    //Getting [list of sensors]
    Sensors sensors = stack.sensors;
    //Add listener on any sensor and event getting the [SensorEvent]s
    tinkerForge.sensorEventConsumerList.add(ths::onSensorEvent);
}

How it works

  • TinkerForge original Device
  • wrapped by Generic Sensor
  • handles input and sends SensorEvent
  • Auto closeable Stack with addable event consumer
  • Returns List of Sensors Sensors
  • Supported devices Bricks and Bricklets
  • Feel free for pull request as the wrapper classes are not so hard to implement, its just a mapping ;)

TODO

  • 16IO manage also input
  • Thread does not shutdown and keeps program alive :(
  • Spring integration
  • Sensors
  • Get Additional Sensor information like "ChipTemperature"
  • Connections
  • Tinkerforge Auto-reconnect is not working properly - reconnected devices are getting timeouts