[IoT] Internet of Things Experiment - TinkerForge Remote Switch (B4J)


Control a remote switch to turn a lamp On / Off via a TinkerForge Remote Switch Bricklet (equipped with a 433MHz radio module).
The Remote Switch Bricklet is connected to a TinkerForge Master Brick. The Master Brick is connected, via USB, to a Raspberry Pi 2.
In summary: Lamp > Remote Switch > Remote Switch 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 Remote Switch Bricklet, Mumbai M-AFS102 Remote Switch
  • 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 Remote Bricklet Bricklet is connected to the Master Brick.
The Remote Switch Mumbai M-AFS102 is plugged in with a Lamp.
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.

When running first time a settings file with some defaults is created. Adjus the settings to your local needs. This is done via the Settings Dialog.

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://, Remote Bricklet UID (like nWr), House Code (default 1), Receiver Code (default 2).
  2. Press the green button ON.
  3. The LED should light and a status update is displayed (Time with Switch ON text).

Install the Experiment

To install the experiment on the Raspberry Pi:

  • Create a folder like /home/pi/b4j/tfremoteswitch with a subfolder /www.
  • Copy tfremoteswitch.jar to /home/pi/b4j/tfremoteswitch 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 tfremoteswitch.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).