Mocking IoT Tinkerforge Sensors

A couple of days have passed since the TinkerForge-API has been published on maven central. But what can you do with if there is no hardware around. This was my situation as i wanted to try it out and play with the weatherstation from jaxenter (weatherstation).
So what can you do now? You can either mock the hardware sensor and struggle with the protocol of the sensors or you can mock the software sensor. The latter approach seems easier to me. So lets start.
If you only want to have a single value request (e.g. getTemperature()) the task is easy. You only have to override the method on sensor instantiation.
new BrickletTemperature("dV6", new IPConnection()){
    public short getTemperature() throws TimeoutException, NotConnectedException {
           return 42;
The more interesting task is to hack the callback listeners so that you continuously get measured values. Ok, this would be no real data but it should be sufficient for testing and playing. To hack the sensor we need to dive a little bit into the internals of the sensors. If you add a listener to a sensor it is put into an internal list which is processed if a specific callback event (for example for temperature) is fired. In this process the changed value is passed.
So the task is to create a thread which fires a specific callback event on a sensor.
//Creates Standard-BrickletTemperature with injected IPConnection
BrickletTemperature brickletTemperature = new BrickletTemperature("kjh6", ipConnection);

//Determine the callback event constants (note: callbackreached listeners are ignored)
List ints = new ArrayList<>();
Field[] declaredFields = deviceClass.getDeclaredFields();
for (Field declaredField : declaredFields) {
    String fieldName = declaredField.getName();
    if (fieldName.startsWith("CALLBACK_") && !fieldName.endsWith("REACHED")) {
         int callbackIndex = declaredField.getInt(device);

//Creates a Value generator for the bricklet
try {
      createBrickletMock(ipConnection, brickletTemperature, (byte) 200, ints);
} catch (NoSuchMethodException e) {e.printStackTrace();}
The callback events of each sensor are mapped to a constant in the sensor class which starts with CALLBACK (callback reached events have CALLBACK_<name>_REACHED) so that you can use reflection to read the values.
static <Bricklet extends Device> void startCallbackListenerThread(IPConnection ipcon, Device bricklet, byte startValue, List<Integer> callbackIndizes) throws NoSuchMethodException {
        Class<IPConnection> ipConnectionClass = IPConnection.class;
        Method callDeviceListener = ipConnectionClass.getDeclaredMethod("callDeviceListener", Device.class, byte.class, byte[].class);

        //start thread for each callback event
        for (int callbackIndex : callbackIndizes) {
            new Thread(() -> {

                try {

                    Random random = new Random();
                    while (true) {

                        //Generates values -1, 0 or 1
                        int randomDiff = random.nextInt(3) - 1;

                        //Invoke on device
                        callDeviceListener.invoke(ipcon, bricklet, (byte) callbackIndex, new byte[]{0, 0, 0, 0, 0, 0, 0, 0, (byte) (startValue + randomDiff), 0});

                        //wait 5s
                } catch (IllegalAccessException | InvocationTargetException | InterruptedException e) {
So have fun coding.


  1. I am glad to this blog offering distinct and handy understanding regarding this topic. Looking for the best microsonic ultrasonic sensors at an affordable price then, is the best option for it. Visit here microsonic ultrasonic sensors

  2. Dieser Kommentar wurde vom Autor entfernt.

  3. This is a great article with lots of informative resources. I appreciate your work this is really helpful for everyone. Check out our website Iot Solutions for more Neebal related info!

  4. I definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work. roxtec comseal

  5. You have done good work by publishing this article here about eaton vfd. I found this article too much informative, and also it is beneficial to enhance our knowledge. Grateful to you for sharing an article like this.

  6. You have mentioned great information here about Enclosure accessories. I would like to say this is a well-informed article and also beneficial article for us. Keep sharing this kind of articles, Thank you.

  7. I think this is one of the most significant pieces of information about Autonics for me. And I'm glad to read your post. Thank you for sharing!


Kommentar veröffentlichen

Beliebte Posts