Set Up B4J

Explore the Raspberry Pi
Site Admin
Posts: 211
Joined: 07 Oct 2017, 12:16

Set Up B4J

Postby rwblinn » 09 Oct 2017, 15:12

The Experiments show how to develop simple solutions running on the Raspberry Pi.
Various hardware components are used, like LEDs, switches and sensors.
There are 2 kind of applications:

B4J Console Application: Running in a Terminal Window, but can also run as a Process
B4J Server Application: Running as a Process; User access via a Webbrowser with URL http://ipaddress:port (like

In addition solutions based on MQTT are also described.

For B4J Applications: The B4J Development environment.
Java must be installed. Check out by running $java -version from the command line. The newest Raspberry Pi NOOBS should have Java pre-installed.
For the solutions based on the Raspberry Pi GPIO, a T-Cobbler connected to a breadboard is used.

B4J Console Application Structure
Each application has a B4J project file (.b4j) and if required, additional classes (.bas) or code helpers (like webutils.bas).
B4J generates a the application a Java JAR file.
Some of the applications run also on other environments (Windows etc.).
The application is started ($java -jar applicationname.jar) and accessed in a Terminal Window.
For the solutions using the Raspberry Pi GPIO, the jPi4J Libary (Raspberry Pi GPIO controller) is required.

B4J Server Application Structure
Each application has a B4J project file (.b4j), a Websocket Class (.bas) and additional classes (.bas) or code helpers (like webutils.bas).
B4J generates a the application a Java JAR file.
Some of the applications run also on other environments (Windows etc.).
The application is accessed via an HTML file (index.html) stored in the www folder (projectfolder/objects/www). The B4J WebSockets client library b4j_ws.js is also required.
This means a typical application has following files: project.b4j, projectwebsocket.bas, webutils.bas, objects/www/index.html, objects/www/b4j_ws.js.
Of course, depending on the project additionals files are used.
For the solutions using the Raspberry Pi GPIO, the jPi4J Libary (Raspberry Pi GPIO controller) is required.

Tools used

B4J on Windows and the B4J-Bridge on the Raspberry Pi.
PuTTY is a free implementation of Telnet and SSH for Windows and Unix platforms, along with an xterm terminal emulator.
WinSCP Free SFTP, SCP and FTP client for Windows
MQTT (Message Queue Telemetry Transport)
Remote Desktop xrdp
Install by $sudo apt-get install xrdp, No further setup required. Note: Ensure SSH is turned on (use $sudo raspi-config, select advanced options SSH). Install on Windows: No installation required as using the standard Windows Programm Remote Desktop.

The B4J-Bridge is very useful when working with the Raspberry Pi.

Create a folder /home/pi/b4j and move to this new folder
Download the B4J Bridge to the Raspberry Pi by either running command:
$wget OR copy the b4j_bridge.jar from the Development Device to folder /home/pi/b4j.
To run the B4J Bridge as root when logged in as user Pi:
$sudo -jar b4j-bridge.jar
If the path to java is set, then call direct $sudo java -jar b4j-bridge.
It is useful to run the B4J-Bridge from a dedicated terminal, like f.e. from the development device using Putty.
When doing so, the output of the running application can be watched.

The B4J-Bridge Terminal screen, displays (logs) information, like
pi@raspberrypi ~/b4j $ sudo java -jar b4j-bridge.jar
B4J-Bridge v1.00
Waiting for connections (port=6790)...
My IP address is:
Starting program
Starting program

Temp File
The B4J Application jar file is stored in a temp folder on the Raspberry Pi.
Example: \home\pi\b4j\tempjars\AsyncInput2.
Hint: When testing various B4J apps, ensure to cleanup this folder from time-to-time.

Keep the Raspberry Pi current
Login as User Pi and regular run

$sudo apt-get update && sudo apt-get upgrade
$sudo apt-get autoremove
$sudo rpi-update
$sudo reboot

Give the Raspberry Pi a fix IP address
Login as User Pi
Obtain Gateway Address by invoking command $netstat -r -n
Set fix ip address by editing the interfaces file:
$sudo nano /etc/network/interfaces
iface eth0 inet dhcp
iface eth0 inet static
address 192.168.nnn.nnn
gateway 192.168.nnn.nnn

Wiring Pi Libary
WiringPi is a GPIO access library written in C for the BCM2835 used in the Raspberry Pi.
Go here for information about installing wiringPi, but in a nutshell:
$sudo apt-get update
$sudo apt-get upgrade
$sudo apt-get install git-core
$git clone git://
$cd wiringPi
$git pull origin
Check if GPIO is working by $gpio -v
List all GPIOs by $gpio readall

Pi4J Libary
The Pi4J is a object-oriented I/O API and implementation libraries for Java Programmers to access the full I/O capabilities of the Raspberry Pi platform.
Information about the Pi4J Library and learn the Pi4J API.
Raspberry Pi GPIO Pin Wiring
Find the Mapping of the GPIO Pins to WiringPi.
Note that the Pin numbering could be confusing. Look closely at the mapping.
Another good overview, is to run on the Raspberry Pi the command $gpio readall.
Example: A LED is connected to the Raspberry Pi GPIO Physical Pin number 12 = BCM 18 = wPi 1 = Name GPIO.1.
For usage with B4J, Pin GPIO.1 = wPi Pin1 = Pin1 used in the example below.
B4J Setup
A wrapper, jPi4J, is available for the Pi4J Library. Summary of the Libraries required. In the B4J additional library folder, following files should be listed:
jPi4J.jar, jPi4J.xml,pi4j-core.jar, pi4j-device.jar, pi4j-gpio-extension.jar
As the Raspberry Pi is being developed further, ensure to use the latest Pi4J Library files. Download the latest jar files from to the PC. Copy following jar files to the B4J additional library folder: pi4j-core, pi4j-device, pi4j-gpio-extension
A new jPi4J library is not required. Use the one provided in (see link above). Ensure

the jPi4J.jar and jPi4J.xml are stored in the B4J additional library folder
the jPi4J library is selected in the B4J IDE (Libs Tab)

Information about the Pin Numbering using wiringPi can be found here.

Setup MQTT
Describes how to setup MQTT on the Raspberry Pi to be able to subscribe and publish messages from/to:

TinkerForge - TinkerForge Brick Mqtt Proxy
B4X - jMQTT Client Library

TinkerForge Brick MQTT Proxy
Reference TinkerForge Brick MQTT Proxy: here.

Installation Steps
$sudo apt-get install python3-pip
$sudo apt-get install python-pip
Next must be done as user Root
$sudo pip install tinkerforge paho-mqtt
$mkdir /home/pi/tinkerforge
$mkdir /home/pi/tinkerforge/mqtt
$cd /home/pi/tinkerforge/mqtt
$sudo apt-get install libusb-1.0-0 libudev0 pm-utils
$wget ... _armhf.deb
$sudo dpkg -i brickd_linux_latest_armhf.deb
$sudo apt-get install mosquitto mosquitto-clients
Copy the file to this folder after download from the TinkerForge website

Check the device information of the connected Bricks And Bricklets
$mosquitto_sub -v -t tinkerforge/enumerate/available/#
tinkerforge/enumerate/available/bricklet/humidity [{"_timestamp":1445348164.10837,"uid":"kcz","hardware_version":[1,1,0],"device_identifier":27,"connected_uid":"5VGrDJ","position":"a","firmware_ version":[2,0,2]}]
tinkerforge/enumerate/available/bricklet/temperature [{"_timestamp":1445348164.11728,"uid":"nfu","hardware_version":[1,1,0],"device_identifier":216,"connected_uid":"5VGrDJ","position":"b","firmw are_version":[2,0,3]}]
tinkerforge/enumerate/available/bricklet/lcd_20x4 [{"_timestamp":1445348164.129469,"uid":"rTS","hardware_version":[1,2,0],"device_identifier":212,"connected_uid":"5VGrDJ","position":"c","firmwar e_version":[2,0,6]}]
tinkerforge/enumerate/available/bricklet/barometer [{"_timestamp":1445348164.151401,"uid":"jXZ","hardware_version":[1,0,0],"device_identifier":221,"connected_uid":"5VGrDJ","position":"d","firmwa re_version":[2,0,2]}]

Check the subscriber For a Temperature Bricklet with UID "nfu"
$mosquitto_sub -v -t tinkerforge/bricklet/temperature/nfu/temperature
Output Is the temperature. New messages are received when the temperature has changed
tinkerforge/bricklet/temperature/nfu/temperature {"_timestamp":1445348164.124881,"temperature":1606}
tinkerforge/bricklet/temperature/nfu/temperature {"_timestamp":1445348284.390836,"temperature":1612}
tinkerforge/bricklet/temperature/nfu/temperature {"_timestamp":1445348302.433974,"temperature":1618}
Add the TinkerForge Brick MQTT Proxy to /etc/rc.local via sudo nano /etc/rc.local
#TinkerForge MQTT Broker
cd /home/pi/tinkerforge/mqtt
#Default interval is 3 seconds, change to 1 min by setting value to 60
python --update-interval 60 &
printf "TinkerForge MQTT Broker started"
B4X jMQTT Client Library
Reference jMQTT Official MQTT client: here.
Installation Steps
$sudo wget
$sudo apt-get update
$sudo apt-get install mosquitto mosquitto-clients python-mosquitto
Check if Mosquitto running
$ps -A | grep mosq
Output example:
5723 ? 00:00:00 mosquitto
Mosquitto starts automatically after every boot
Startup script Is located in: /etc/init.d/mosquitto
Mosquitto Control
$sudo service mosquitto restart
$sudo service mosquitto stop
$sudo service mosquitto start

Quickstart building B4J server app and run
Using the B4J Bridge

Start the Raspberry Pi, open a terminal session and login as user Pi.
First time: create folder /home/pi/b4j/tempjars (used by the B4J Bridge) ($cd /home/pi/b4j, $mkdir tempjars).
Copy the www folder from the development device to the folder /home/pi/b4j/tempjars/www.
Start the B4J Bridge $sudo java -jar b4j-bridge.jar and note the My IP address (like
From B4J IDE connect to the B4J-Bridge via menu Tools > B4J Bridge > Connect (using My IP address, check bottom left staes connect).
Build the B4J Application.
Start the application (F5), connect via Browser http://ipaddress:port like (check output on the Raspberry Pi terminal).
Note: The folder /home/pi/b4j/tempjars lists a file called AsynInput1 which is the application.
Note: If updates made to files in the www folder, do not forget to copy those to the Raspberry Pi prior testing.

Return to “Raspberry Pi”

Who is online

Users browsing this forum: No registered users and 1 guest