### Sample AmbientLight MQTT Listener

Example of publishing a MQTT Topic.
Application purpose:
To log via a listener, the TinkerForge AmbientLight Bricklet iluminance via Inline Java Code.
B4J Main Module (Non-UI application)
#Region Project Attributes
#CommandLineArgs:
#MergeLibraries: True
'TinkerForge Library tinkerforge.jar located in the B4J additional libraries folder
#AdditionalJar: Tinkerforge
#End Region
`Sub Process_Globals
'Define the listener class as public
Public allistener As TFAmbientLightListener
End Sub`
`Sub AppStart (Args() As String)
allistener.Initialize
StartMessageLoop
End Sub`
B4J Listener Class
Class Module TFAmbientLightListener (BAS)
Provides a TinkerForge AmbientLight listener
`Sub Class_Globals
Dim joInline As JavaObject = Me
End Sub`
`Public Sub Initialize
joInline.RunMethod("StartListener", Array("192.168.0.30", 4223, "mdh", 1000))
End Sub`
`Sub LogListener(al As Int)
If al <> 0 Then Log($"${DateTime.time(DateTime.Now)} Listener Value: ${(al/10.0)} Lux"$)
End Sub`
#If JAVA
import com.tinkerforge.IPConnection;
import com.tinkerforge.BrickletAmbientLight;
public void StartListener(String HOST, int PORT, String UID, int interval) throws Exception {
IPConnection ipcon = new IPConnection();
BrickletAmbientLight al = new BrickletAmbientLight(UID, ipcon);
ipcon.connect(HOST, PORT);
//Set the callback period
al.setIlluminanceCallbackPeriod(interval);
//Add illuminance listener (parameter has unit Lux/10)
//The value is fed back to the b4j sub via ba.raiseevent
al.addIlluminanceListener(new BrickletAmbientLight.IlluminanceListener() {
public void illuminance(int illuminance) {
ba.raiseEvent(this, "loglistener", illuminance);
//System.out.println("Illuminance: " + illuminance/10.0 + " Lux");
}
});
//ipcon.disconnect();
}
#End If

Changed: , Category: TinkerForge MQTT, File: T6C266M1979.htm, ID: 1979