TinkerForge openHAB Experiment IO-16 Switch LED

Introduction
TinkerForge Experiment switching LEDs On / OFF via openHAB.

Hardware setup: The TinkerForge IO-16 Bricklet is connected to a TinkerForge Master Brick which is plugged on a TinkerForge RED Brick.
The LEDs with Resistors are connected to the Ports A0 and A1 of the IO-16 Bricklet.

Software setup: The openHAB configuration is managed via the TinkerForge Brick Viewer running on the RED Brick.
Versions used for this experiment: RED Brick Image 1.8, Brick Viewer 2.3.3.

The IO-16 Port state can also be changed by using the openHAB REST-API. A B4J Example is included to set or get the state of IO-16 Port A0 state.

Functionality
  • Switching LEDs ON / OFF via TinkerForge openHAB Webinterface.
  • Configure openHAB via Brick Viewer running on the REd Brick. The files used are openhab.cfg, io16test.items, io16test.rules,io16test.sitemap
  • Accessing the Experiment Webinterface via http://red-brick-ip-address/openhab.app?sitemap=io16test
  • Changing the state of a IO-16 Port via a B4J application using the openHAB REST API
Content of the openHAB configuration files stored in /etc/openhab/configurations.
Items
//Define Text Item which is updated when a switch changes
String labelinfo "Label Info" { }
//Define the IO-16 switches
Switch outa0 "LED A0" { tinkerforge="uid=gkM, subid=outa0" }
Switch outa1 "LED A1" { tinkerforge="uid=gkM, subid=outa1" } 
Sitemap
sitemap io16test label="IO-16 Test"
{
	Frame
	{
		Text item=labelinfo label="LED Last Change [%s]" icon="switchinfo"
		Switch item=outa0
		Switch item=outa1
	}
}
Rules
import org.openhab.core.library.types.*
import java.util.Date
import java.text.SimpleDateFormat

rule "LED A0 Switch"
when
	Item outa0 changed
then
	var SimpleDateFormat df = new SimpleDateFormat( "YYY-MM-dd HH:mm:ss" )
	var String Timestamp = df.format ( new Date() )

	if (outa0.state == ON) {
		logInfo( "FILE", Timestamp + " - State " + "ON")
		labelinfo.postUpdate("A0 ON " + Timestamp)
	}
	else {
		logInfo( "FILE", Timestamp + " - State " + "OFF" )
		labelinfo.postUpdate("A0 OFF " + Timestamp)
	}
end

rule "LED A1 Switch"
when
	Item outa1 changed
then
	var SimpleDateFormat df = new SimpleDateFormat( "YYY-MM-dd HH:mm:ss" )
	var String Timestamp = df.format ( new Date() )

	if (outa1.state == ON) {
		logInfo( "FILE", Timestamp + " - State " + "ON")
		labelinfo.postUpdate("A1 ON " + Timestamp)
	}
	else {
		logInfo( "FILE", Timestamp + " - State " + "OFF" )
		labelinfo.postUpdate("A1 OFF " + Timestamp)
	}
end 
Download
  • The project : openHAB configuration files, B4J REST-API example.
Notes

  • Unpack and copy the configuration files to the RED Brick openHAB configuration folder "/etc/openhab/configurations".
  • The RED Brick Viewer lists the files in Tab RED Brick > Settings > openHAB.
  • The files are owned by user openhab. It is recommended that all changes to the configuration files are handled via the RED brick Interface.

ChangeLog
  • Feb 24, 2016: Added B4J Example using the openHAB REST API
  • Feb 21, 2016: First Version
ToDo
Additional Information
openHAB Various
  • Learn more about openHAB.
  • Log Files Folder: /var/log/openhab.
  • Events Log: /var/log/openhab/events.log.
  • Log File /var/log/openhab/openhab.log is useful to check for erros, warnings and info.
  • Configuration File openhab.cfg: When making changes to the openHAB configuration file /etc/openhab/configurations/openhab.cfg, a reboot of the RED Brick is required to apply the changes. Note that rebooting takes a few minutes.
  • Configuration Files are created with the Brick Viewer: Select RED Brick > Settings > openHAB.
  • Icons Folder: /usr/share/openhab/webapps/images with Owner root.
  • TinkerForge Example: here
Use the openHAB REST-API (Example TinkerForge Ambient Light Bricklet)
Get or set the state of an openHAB item as defined in the configuration items file.
#Items file //AmbientLight as Number Number TF_AmbientLight "Luminance [%.0f lx]" { tinkerforge="uid=mdh" } //IO-16 Port Switch outa0 "LED A0" { tinkerforge="uid=gkM, subid=outa0" } #REST-API URL Commands AmbientLight get actual measured value = http://192.168.0.30:8080/rest/items/TF_AmbientLight/state IO-16 Port A0 get state ON or OFF = http://192.168.0.30:8080/rest/items/outa0/state IO-16 Port A0 set state ON = http://192.168.0.30:8080/rest/items/outa0/ON IO-16 Port A0 set state OFF = http://192.168.0.30:8080/rest/items/outa0/OFF
Setup & Webinterface
B4J openHAB REST-API Application