### jPi4J GPIO Controller

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.
Find information on how to use in B4J and some hints at the end of this HowTo.
Pi4J Information
Information about the Pi4J Library: http://pi4j.com/
To learn about the Pi4J API: http://pi4j.com/apidocs/index.html
IMPORTANT
When using a Raspberry Pi 3, following error might appear when executing an application:
Unable to determine hardware version. I see: Hardware : BCM2835 - expecting BCM2708 or BCM2709.
If this is a genuine Raspberry Pi then please report this to projects@drogon.net. If this is not a Raspberry Pi then you
are on your own as wiringPi is designed to support the Raspberry Pi ONLY.
There are various threads about this issue.
After various testing, resolved by [status 20170506]:
Reinstalling wiringPi on the Raspberry Pi - see http://wiringpi.com/download-and-install/ .
Checked the wiringPi version using command "gpio -v", resulting in 2.44.
Installed the Pi4J snapshot library files (pi4j-1.2-SNAPSHOT.zip) from http://pi4j.com/download.html in the B4J Additional Libraries folder:
Copied from the snapshot lib folder, the jars pi-core, pi-device, pi-gpio-extension.
Note: To check the CPU use command "cat /proc/cpuinfo". See at the bottom under hardware ie hardware : BCM2835.
GPIO Pin Wiring / Mapping
Find the mapping of the GPIO Pins to wiringPi http://wiringpi.com/pins/ .
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:
LED connected to RasPi Physical Pin 12 = BCM 18 = wPi 1 = Name GPIO.1.
B4J uses the wiringPi pin numbers 1 - 29 (= GPIO.n in the table below) to initialize a pin: Pin1.Initialize(1, True)
Note that when using a T-Cobbler with breadboard, the pin numbering used is in most cases BCM (so 18 means GPIO.1).
GPIO Table
Physical Pin = Name = wPi = BCM
01 = 3.3v
02 = 5v
03 = SDA.1 = wPi 8 = BCM 2
04 = 5V
05 = SCL.1 = wPi 9 = BCM 3
06 = 0v
07 = GPIO.7 = wPi 7 = BCM 4
08 = TxD = wPi 15 = BCM 14
09 = 0v
10 = RxD = wPi 16 = BCM 15
11 = GPIO.0 = wPi 0 = BCM 17
12 = GPIO.1 = wPi 1 = BCM 18
13 = GPIO.2 = wPi 2 = BCM 27
14 = 0v
15 = GPIO.3 = wPi 3 = BCM 22
16 = GPIO.4 = wPi 4 = BCM 23
17 = 3.3v
18 = GPIO.5 = wPi 5 = BCM 24
19 = MOSI = wPi 12 = BCM 10
20 = 0v
21 = MISO = wPi 13 = BCM 9
22 = GPIO.6 = wPi 6 = BCM 25
23 = SCLK = wPi 14 = BCM 11
24 = CE0 = wPi 10 = BCM 8
25 = 0v
26 = CE1 = wPi 11 = BCM 7
27 = SDA.0 = wPi 30 = BCM 0
28 = SCL.0 = wPi 31 = BCM 1
29 = GPIO.21 = wPi 21 = BCM 5
30 = 0v
31 = GPIO.22 = wPi 22 = BCM 6
32 = GPIO.26 = wPi 26 = BCM 12
33 = GPIO.23 = wPi 23 = BCM 13
34 = 0v
35 = GPIO.24 = wPi 24 = BCM 19
36 = GPIO.27 = wPi 27 = BCM 16
37 = GPIO.25 = wPi 25 = BCM 26
38 = GPIO.28 = wPi 28 = BCM 20
39 = 0v
40 = GPIO.29 = wPi 29 = BCM 21
B4J Setup
jPi4J
A wrapper, jPi4J, is available for the Pi4J Library:
Find here: https://www.b4x.com/android/forum/threads/iot-jpi4j-raspberry-pi-gpio-controller.37493/
Hint: Regular review the previous link for updates.
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
IMPORTANT ON UPDATES
As the Raspberry Pi is being developed further, ensure to use the latest Pi4J Library files.
Download the latest jar files from http://pi4j.com/download.html#Direct_Download 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 jpi4j.zip (see link above).
Ensure the following files:
• jPi4J.jar and jPi4J.xml are in the B4J additional library folder.
• jPi4J.jar and jPi4J.xml do NOT store in the B4J internal library folder!
• jPi4J library is selected in the B4J IDE Library Manager.
Information about the Pin Numbering using wiringPi can be found here:
http://pi4j.com/pins/model-2b-rev1.html
B4J Usage with the Raspberry Pi
The B4J usage of the jPi4J wrapper is easy.
In summary, define a GPIO Controller and GPIO Pin to control.
Example
See this example, which changes the state of the GPIO Pin 1.
A LED is connected to the Raspberry Pi GPIO Physical Pin Number 12 = BCM 18 = wPi 1 = GPIO.1.
In B4J the the wiringPi pin numbering is used, so for this example 1.
Note: To stop this app, use [Kill Process] in B4J IDE else the process will continue.
`Sub Process_Globals
Private controller As GpioController
'Define the Physical Pin12 = wPi 1 as Output
Private Pin1 As GpioPinDigitalOutput
'Timer to change the state
Private Timer1 As Timer
End Sub`
`Sub AppStart (Args() As String)
Try
controller.Initialize
Catch
Log("Controller Initialize Error:" & CRLF & LastException.Message)
Return
End Try
Pin1.Initialize(1, True)
Timer1.Initialize("Timer1", 5000)
Timer1.Enabled = True
StartMessageLoop
End Sub`
`Sub Timer1_Tick
Pin1.State = Not(Pin1.State)
Log("Switching Pin1 state. Pin1 state = " & Pin1.State)
End Sub`
Hints
Hint using the B4J Bridge on the Raspberry Pi
To run:
On the RPi start the B4J bridge:
sudo java -jar b4j-bridge.jar. Note the IP address.
On the PC start the B4J IDE, connect to B4J Bridge using the noted IP address. In the B4J IDE bottom left, connected is displayed.
Run the app by pressing F5 ... and the LED should flash every 5 seconds.
Set the Debounce Period for all Pins (requires the JavaObject library).
Reference: http://pi4j.com/apidocs/com/pi4j/io/gpio/GpioPinDigitalInput.html
Class:
com.pi4j.io.gpio
Method:
void setDebounce(int debounce)
Sets the debounce delay interval (in milliseconds) for all pin states.
Example set debounce period to 5 seconds
Private InputPin As GpioPinDigitalInput
Dim joPin As JavaObject = InputPin
Dim DebouncePeriod as Int = 5000
joPin.RunMethod("setDebounce", Array(DebouncePeriod))

Changed: 2018-03-26 13:09, Category: Raspberry Pi Libraries, File: T6C235M1723.htm, ID: 1723