### Bricklet Listener

This is an example of building a TinkerForge Listener 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
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 AirPressureListener
Project File (PRJ)
'Non-UI application (console / server application)
#Region Project Attributes
#AdditionalJar: Tinkerforge
#End Region
`Sub Process_Globals
'Define the listener class as public
Public tflistener As tflistenerEx
End Sub`
`Sub AppStart (Args() As String)
tflistener.Initialize
StartMessageLoop
End Sub`
Class Module tflistenerEx (BAS)
`Sub Class_Globals
Dim joInline As JavaObject = Me
End Sub`
`Public Sub Initialize
joInline.RunMethod("StartListener", Array(1000))
End Sub`
`Sub LogListener(ap As Int)
Log("Listener Value: " & (ap / 1000))
End Sub`
If JAVA
import com.tinkerforge.BrickletBarometer;
import com.tinkerforge.IPConnection;
public void StartListener(int interval) 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);
b.setAirPressureCallbackPeriod(interval);
b.addAirPressureListener(new BrickletBarometer.AirPressureListener() {
public void airPressure(int airPressure) {
//System.out.println("Air Pressure: " + airPressure/1000.0 + " mbar");
ba.raiseEvent(this, "loglistener", airPressure);
}
});
//ipcon.disconnect();
}
End If
B4J Project RS232Listener
Project File (PRJ)
'Non-UI application (console / server application)
#Region Project Attributes
#AdditionalJar: Tinkerforge
#End Region
`Sub Process_Globals
'Define the listener class as public
Public tflistener As tfrs232listener
End Sub`
`Sub AppStart (Args() As String)
Log(DateTime.Date(DateTime.Now) & " " & DateTime.Time(DateTime.Now))
tflistener.Initialize
StartMessageLoop
End Sub`
Class Module tfrs232listener (BAS)
`Sub Class_Globals
'Define the JavaObject required for the inline java code
Dim joInline As JavaObject = Me
End Sub`
'Initializes the class and starts the listener
`Public Sub Initialize
Log("Starting TinkerForge Listener")
Dim HOST As String = "192.168.0.19"
Dim PORT As Int = 4223
Dim UID As String = "uV3"
joInline.RunMethod("StartListener", Array(HOST, PORT, UID))
End Sub`
'If data is received, the data is logged
`Sub LogListener(str As String)
Log(DateTime.Time(DateTime.Now) & " :: RS232 Listener String Received:" & CRLF & str)
End Sub`
If JAVA
import java.util.Arrays;
import com.tinkerforge.IPConnection;
import com.tinkerforge.BrickletRS232;
//Convert string to array of length 60 as needed by write
public static char[] stringToCharArray(String message) {
return Arrays.copyOf(message.toCharArray(), 60);
}
//Assume that the message consists of ASCII characters and
//convert it from an array of chars to a string
public static String charArrayToString(char[] message, short length) {
return new String(message, 0, length);
}
//Start the listener
//Example: String HOST = "192.168.0.19";int PORT = 4223;String UID = "uV3";
public void StartListener(String HOST, int PORT, String UID) throws Exception {
IPConnection ipcon = new IPConnection();
BrickletRS232 rs232 = new BrickletRS232(UID, ipcon); // Create device object
ipcon.connect(HOST, PORT);
// Add read listener
rs232.addReadCallbackListener(new BrickletRS232.ReadCallbackListener() {
public void readCallback(char[] message, short length) {
String msg = charArrayToString(message, length);
//System.out.println("Message (length: " + length + "): \"" + msg + "\"");
ba.raiseEvent(this, "loglistener", msg);
}
});
// Enable read callback
rs232.enableReadCallback();
//ipcon.disconnect();
}
End If

Changed: , Category: TinkerForge, File: T6C264M1959.htm, ID: 1959