### Bricklet Data Inline Java

This is an example of building a TinkerForge Application to get Bricklet Data using Inline Java with the Code provided by TinkerForge.
The B4J Inline Java feature makes it real easy to develop solutions using TinkerForge API.
Example based upon Barometer Bricklet.
http://www.tinkerforge.com/en/doc/Software/Bricklets/Barometer_Bricklet_Java.html#barometer-bricklet-java-examples
Note
This is a "Getter" solution which is not recommended by TinkerForge when often new data is retrieved
(which can be done by using a Timer).
It is better to use a Listener instead.
Get the TinkerForge Java Binding
Reference: http://www.tinkerforge.com/en/doc/Software/API_Bindings_Java.html
The Tinkerforge.jar, a precompiled Java library, is accessed via B4x JavaObjects.
The Jar file has to be copied to the B4J additional libraries folder and added to the project attributes.
Get the connection parameter
For this example the Barometer Bricklet is connected to a Master Brick which is connected to a Raspberry Pi 2.
Note the IP Address, Port and Bricklet UID.
The Port is default 4223 and the Bricklet UID can be obtained using the Brick Viewer.
B4J Project
'Non-UI application (console / server application)
#Region Project Attributes
#AdditionalJar: Tinkerforge
#End Region
`Sub Process_Globals
'Define the listener class as public
Public tfdata As tfdataEx
End Sub`
`Sub AppStart (Args() As String)
tfdata.Initialize
StartMessageLoop
End Sub`
BAS Class
'Class module tflistenerEx
`Sub Class_Globals
Dim joInline As JavaObject = Me
End Sub`
'Initializes the object.
`Public Sub Initialize
joInline.RunMethod("GetData", Null)
End Sub`
`Sub LogData(ap As Int)
Log("Data Value: " & (ap / 1000))
End Sub`
If JAVA
import com.tinkerforge.BrickletBarometer;
import com.tinkerforge.IPConnection;
public void GetData() throws Exception {
String HOST = "192.168.0.21";
int PORT = 4223;
String UID = "jXZ";
IPConnection ipcon = new IPConnection();
BrickletBarometer b = new BrickletBarometer(UID, ipcon);
ipcon.connect(HOST, PORT);
int airPressure = b.getAirPressure();
ba.raiseEvent(this, "logdata", airPressure);
ipcon.disconnect();
}
End If

Changed: , Category: TinkerForge, File: T6C264M1958.htm, ID: 1958