### Bricklet Listener 3 Bricklets

This is an example of a Non-UI application with 3 Bricklets.
The Bricklet UID used for testing from WeatherCubeOne2
barometeruid = jXZ, temperatureuid = nfu, humidityuid = kcz
B4J Project Code
#Region Project Attributes
'Ensure to download the latest version from TinkerForge:
#AdditionalJar: Tinkerforge
#End Region
`Sub Process_Globals
'Define the listener class as public
Public tfListener As tflistenerclass
Public tfListenerInterval As Int = 10000
Public tfHOST As String = "192.168.0.21"
Public tfPORT As Int = 4223
Public tfTemperatureUID As String = "nfu"
Public tfHumidityUID As String = "kcz"
Public tfBarometerUID As String = "jXZ"
End Sub`
`Sub AppStart (Args() As String)
Log(CAPPTITLE & " " & CAPPVERSION)
Log(DateTime.Date(DateTime.Now) & " " & DateTime.Time(DateTime.Now))
tfListener.Initialize
StartMessageLoop
End Sub`
BAS Code tflistenerclass
'Class module tflistenerEx
`Sub Class_Globals
Dim joInline As JavaObject = Me
End Sub`
'Initializes the object.
`Public Sub Initialize
Log("Starting TinkerForge Listeners")
joInline.RunMethod("TemperatureListener", Array(Main.tfHOST, Main.tfPORT, Main.tfTemperatureUID, Main.tfListenerInterval))
joInline.RunMethod("HumidityListener", Array(Main.tfHOST, Main.tfPORT, Main.tfHumidityUID, Main.tfListenerInterval))
joInline.RunMethod("AirPressureListener", Array(Main.tfHOST, Main.tfPORT, Main.tfBarometerUID, Main.tfListenerInterval))
End Sub`
'Define the bricklet listener result events
`Sub Temperature(value As Int) 'ignore
Log($"${DateTime.time(DateTime.now)}: Temperature= ${value / 100.0}"$)
End Sub`
`Sub Humidity(value As Int) 'ignore
Log($"${DateTime.time(DateTime.now)}: Humidity= ${value / 10.0}"$)
End Sub`
`Sub AirPressure(value As Int) 'ignore
Log($"${DateTime.time(DateTime.now)}: AirPressure= ${value / 1000.0}"$)
End Sub`
'Define the listeners. Be exact in case - lookup the API reference with the examples
#If JAVA
import com.tinkerforge.IPConnection;
//Temperature
import com.tinkerforge.BrickletTemperature;
//Ref: www.tinkerforge.com/en/doc/Software/Bricklets/Temperature_Bricklet_Java.html#temperature-bricklet-java-examples
public void TemperatureListener(String HOST, int PORT, String UID, int interval) throws Exception {
IPConnection ipcon = new IPConnection();
BrickletTemperature t = new BrickletTemperature(UID, ipcon);
ipcon.connect(HOST, PORT);
t.setTemperatureCallbackPeriod(interval);
t.addTemperatureListener(new BrickletTemperature.TemperatureListener() {
public void temperature(short temperature) {
//System.out.println("Temperature: " + temperature/100.0 + " °C");
ba.raiseEvent(this, "temperature", temperature);
}
});
//ipcon.disconnect();
}
//Humidity
import com.tinkerforge.BrickletHumidity;
//Ref: http://www.tinkerforge.com/en/doc/Software/Bricklets/Humidity_Bricklet_Java.html#humidity-bricklet-java-examples
public void HumidityListener(String HOST, int PORT, String UID, int interval) throws Exception {
IPConnection ipcon = new IPConnection();
BrickletHumidity h = new BrickletHumidity(UID, ipcon);
ipcon.connect(HOST, PORT);
h.setHumidityCallbackPeriod(interval);
h.addHumidityListener(new BrickletHumidity.HumidityListener() {
public void humidity(int humidity) {
//System.out.println("Humidity: " + humidity/10.0 + " %RH");
ba.raiseEvent(this, "humidity", humidity);
}
});
//ipcon.disconnect();
}
//AirPressure
import com.tinkerforge.BrickletBarometer;
//Ref: www.tinkerforge.com/en/doc/Software/Bricklets/Barometer_Bricklet_Java.html#barometer-bricklet-java-examples
public void AirPressureListener(String HOST, int PORT, String UID, int interval) throws Exception {
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, "airpressure", airPressure);
}
});
//ipcon.disconnect();
}
#End If

Changed: , Category: TinkerForge, File: T6C264M1960.htm, ID: 1960