[IoT] Internet of Things Experiment - IO-16 via MQTT (B4J)


Switch a LED ON / OFF, connected to a TinkerForge IO16 Bricklet. The IO16 Bricklet is connected to a TinkerForge Master Brick. The Master Brick is connected, via USB, to a Raspberry Pi 2.
In summary: LED > IO16 Bricklet > Master Brick > Raspberry Pi
This is a B4J Webserver Application using MQTT.

Ready to develop?

Before starting, ensure to read Get Ready first. Take special note of the section Setup MQTT.

The Solution

  • Hardware: Raspberry Pi, TinkerForge Master Brick, TinkerForge IO16 Bricklet
  • Software: B4J, B4J-Bridge, B4J jMQTT Library, TinkerForge Brick Daemon, TinkerForge Brick MQTT Proxy
Setup the Hardware Components
The TinkerForge Master Brick is connected via USB to the Raspberry Pi. The TinkerForge IO16 Bricklet is connected to the Master Brick.
The LED is connected to the IO16 brickler port A0.
The TinkerForge Brick Daemon and the Brick MQTT Proxy must be installed on the Raspberry Pi.
B4J Application Concept

The TinkerForge Brick MQTT Proxy is handling MQTT messages from & to the Bricklets via the TinkerForge Brick Daemon. The application subscribes to TinkerForge Topics, as defined for the Bricklets . This is being handled within the B4J application by the jMQTT library. When a message from a Bricklet is received, the topic is determined first followed by parsing the message payload into a readable string, from which the value of the Bricklet is taken and displayed on the Webpage.

There are two B4J classes defined, the main project (in tfio16mqtt.b4j) and the Server Websocket Class.
The libraries used are jCore, JavaObject, jMQTT, jServer and Json. The application is accessed via Browser by calling the file Index.html located in the project Objects/www folder.

Run the Experiment

Build the application (B4J code as well as HTML,like the index.html file in the objects/www folder).
When using the B4J Bridge for test purposes, the content of the objects/www folder must be copied to the Raspberry Pi /home/pi/b4j/tempjars/www folder.
Start the B4J Bridge on the Raspberry Pi ($cd /home/pi/b4j, $sudo java -jar b4j-bridge.jar) and connect from the B4J IDE (Tools > B4J Bridge > Connect > Raspberry Pi IP Address)
Run the application from the B4J IDE (press F5).
To access the application from the Webbrowser, use as IP address localhost with port 51042 (e.g. http://localhost:51042). The file index.html is loaded. To start:

  1. Define the Settings MQTT Host TCP address (like tcp://, IO16 UID (like gkM), Port (set a), Selection_Mask (set to 1).
  2. Press the green button ON.
  3. The LED should light and a status update is displayed (Time with LED ON text).

Install the Experiment

To install the experiment on the Raspberry Pi:

  • Create a folder like /home/pi/b4j/tfio16 with a subfolder /www
  • Copy tfio16.jar to /home/pi/b4j/tfio16 and
  • The content of the objects/www folder to /home/pi/b4j/tfio16/www
Run the application from the Raspberry Pi via a terminal by invoking (from /home/pi/b4j/tfio16) $java -jar tfio16.jar. Then access the application from a Webbrowser raspberrypi-ip-address:51042 (e.g.

Notes: Do not forget to copy the content of the objects/www folder every time a change is made, else still the old version is used.



  • Dec 19, 2015: First Version.


  • Update status label in index.html via sub message_arrived. Parse the json payload message and read property value (0 or 1).
  • Request TinkerForge to provide a topic for obtaining the IO16 Port Status (high or low).