### Sample GPIO LED switch

GPIO Example to switch a LED On / Off. Project name used is GPIOLED.
Purpose to switch a LED connected to PIN 12 (named GPIO18) via Webbrowser.
References
https://github.com/openhab/openhab/wiki/GPIO-Binding
Define the Items
Folder: /opt/openhab/configurations/items
File: gpioled.items
//openHAB Project GPIOLED by Robert W.B. Linn
//https://github.com/openhab/openhab/wiki/GPIO-Binding
//Changed: 20160315
//Define the LED which is connected to pin number 12 which is named GPIO18 and therefor bind to PIN 18
Switch LED1 "LED" { gpio="pin:18" }
//Define Text Item which is updated when a value has changed
String labelinfo "Label Info" { }
Define the Sitemap
Folder: /opt/openhab/configurations/sitemaps
File: gpioled.sitemap
//openHAB Project GPIOLED by Robert W.B. Linn
//https://github.com/openhab/openhab/wiki/GPIO-Binding
//Access Sitemap: 192.168.0.58:8080/openhab.app?sitemap=gpioled
//Changed: 20160315
sitemap gpioled label="LED On / Off"
{
Frame label="openHAB Raspberry Pi Experiment"
{
Switch item=LED1
Text item=labelinfo label="Last Change [%s]" icon="info"
}
}
Define the Rules
Folder: /opt/openhab/configurations/rules
File: gpioled.rules
//openHAB Project GPIOLED by Robert W.B. Linn
//https://github.com/openhab/openhab/wiki/Rules
//Changed: 20160315
import org.openhab.core.library.types.*
import java.util.Date
import java.text.SimpleDateFormat
rule "LED Status Changed"
when
Item LED1 changed
then
//Set the time format. This could also be like YYYY:MM:DD HH:mm:ss
var SimpleDateFormat df = new SimpleDateFormat( "HH:mm:ss" )
var String Timestamp = df.format ( new Date() )
//Update the sitemap item - the label has a text "Last Change"
if (LED1.state == ON)
labelinfo.postUpdate("LED turned ON at " + Timestamp )
else
labelinfo.postUpdate("LED turned OFF at " + Timestamp )
end
Access the Sitemap via Browser
URL is (using the Raspberry Pi IP Address):
http://192.168.0.58:8080/openhab.app?sitemap=gpioled

Changed: 2018-06-07 09:31, Category: WebApps openHAB, File: T6C228M1682.htm, ID: 1682