### Tinkerforge Data send to Domoticz B4J Application

Solution to send Data from a TinkerForge Bricklet to a Domoticz Device.
The TinkerForge Bricklet is connected to a Raspberry Pi.
For this example Barometer Data from the TinkerForge Barometer Bricklet is send to a Domoticz Virtual Barometer Sensor.
Setup Servers
The Raspberry Pi is setup as a Domoticz Server and in addition the TinkerForge Brick Daemon is installed.
References:
Domoticz Manual: http://www.domoticz.com/DomoticzManual.pdf
TinkerForge Raspberry Pi: www.tinkerforge.com/en/doc/Embedded/Raspberry_Pi.html
Domoticz
Start the server, goto Setup > Hardware > Add a Dummy Device > give a name like Virtual Devices > Add.
In the Hardware list, select the new Virtual Device, select Create Virtual Sensors and add a Barometer.
Goto Setup > Devices and select the Barometer, note down the Idx, click on the blue arrow to add the device, give name Barometer.
The Barometer is shown under the tab Weather.
B4J
Create a Non-UI App which has an additional class to manage the TinkerForge connection and data requests.
This is being done using Inline Java. TinkerForge has example code, which can be used straight in the B4J Class Inline Java Section.
TinkerForge Reference Barometer Bricklet:
http://www.tinkerforge.com/de/doc/Software/Bricklets/Barometer_Bricklet_Java.html#barometer-bricklet-java-examples
Libraries used: Special non FX based HTTPUtils2, jStringUtils
External Library: Tinkerforge.jar (to be stored in the additional B4J Library Folder, download from the TinkerForge website).
Start the B4J App
On the Raspberry Pi goto the folder where the B4J app should run from.
Start the tfbarotodomoticz.jar as a process by running from the command line:
java -jar tfbarotodomoticz.jar&
See Example
pi@DoDev ~/domoticz/scripts $ java -jar tfbarotodomoticz.jar&
[1] 7834
pi@DoDev ~/domoticz/scripts $ TF Baro to Domoticz v20150723
Send data from Tinkerforge Barometer Bricklet to Domoticz Server Virtual Baro Sensor
07/23/2015 20:36:50
Starting Barometer Data Collection and send to Domoticz
Updating AirPressure in Domoticz: 1015.765
B4J Projectfile tfbarotodomoticz.b4j
Version=3.02
AppType=StandardJava
NumberOfModules=3
Module1=HttpJob
Module2=HttpUtils2Service
Module3=tfbarotodomoticzclass
Build1=Default,b4j.example
NumberOfFiles=0
NumberOfLibraries=4
Library1=jcore
Library2=jstringutils
Library3=JavaObject
Library4=jhttp
@EndOfDesignText@
'Non-UI application (console / server application)
#Region Project Attributes
#AdditionalJar: Tinkerforge
#End Region
`Sub Process_Globals
'Define the barometer classs as public
Public tfbarotodomoticz As tfbarotodomoticzclass
End Sub`
`Sub AppStart (Args() As String)
Log(DateTime.Date(DateTime.Now) & " " & DateTime.Time(DateTime.Now))
tfbarotodomoticz.Initialize
StartMessageLoop
End Sub`
B4J Class tfbarotodomoticzclass.bas
Type=Class
Version=3.02
ModulesStructureVersion=1
B4J=true
@EndOfDesignText@
'Class module tfbarotodomoticzclass
`Sub Class_Globals
Dim tmr As Timer
Dim joInline As JavaObject = Me
End Sub`
'Initializes the object.
`Public Sub Initialize
Log("Starting Barometer Data Collection and send to Domoticz")
tmr.Initialize("Timer1", 60000)
tmr.Enabled = True
Timer1_Tick
End Sub`
`Sub Timer1_Tick
joInline.RunMethod("getdataandupdate", Null)
End Sub`
`Sub UpdateAirPressure(ap As Int)
Log("Updating AirPressure in Domoticz: " & (ap / 1000))
Dim jobTF As HttpJob
jobTF.Initialize("jobTF", Me)
Dim mbar As String = ap / 1000
Dim tftodomiticzcommandurl As String = "http://192.168.0.21:8080/json.htm?type=command¶m=udevice&idx=9&nvalue=0&svalue=;;;" & mbar & ";0"
jobTF.Download(tftodomiticzcommandurl)
End Sub`
`Sub JobDone(Job As HttpJob)
If Job.Success Then
' Log("HTTP Job Success: " ) '& Job.GetString)
Else
Log("HTTP Job Error: " & Job.ErrorMessage)
End If
Job.Release
End Sub`
#If JAVA
import com.tinkerforge.BrickletBarometer;
import com.tinkerforge.IPConnection;
public void getdataandupdate() throws Exception {
String HOST = "192.168.0.21";
int PORT = 4223;
String UID = "jXZ";
// Create the IP connection first
IPConnection ipcon = new IPConnection();
// Create the device object connected to the ipconnection
BrickletBarometer b = new BrickletBarometer(UID, ipcon);
// Connect to the brick daemon
ipcon.connect(HOST, PORT);
// Don't use the device before ipcon is connected
// Get current air pressure (unit is mbar/1000)
// Can throw com.tinkerforge.TimeoutException
int airPressure = b.getAirPressure();
//System.out.println("Air Pressure: " + airPressure/1000.0 + " mbar");
ba.raiseEvent(this, "updateairpressure", airPressure);
ipcon.disconnect();
}
#End If

Changed: 2018-06-07 09:11, Category: Domoticz Home Automation, File: T6C186M1358.htm, ID: 1358