### Sample GPIO LED switch with Mail Action

GPIO Example to switch a LED On / Off and sent an email. Project name used is mailevent.
Purpose to switch a LED connected to PIN 12 (named GPIO18) via Webbrowser and sent an email via a GoogleMail account with the status of the LED.
This sample has the same basic setup as the B4J HowTo Sample GPIO LED Switch with the addition of using a Mail Action.
References
https://github.com/openhab/openhab/wiki/GPIO-Binding
https://github.com/openhab/openhab/wiki/Actions goto Mail Actions
Define the Mail Configuration in openhab.cfg
mail:hostname=smtp.gmail.com
mail:port=587
mail:username=@googlemail.com
mail:password=
mail:from=@googlemail.com
mail:tls=true
Define the Items
Folder: /opt/openhab/configurations/items
File: mailevent.items
//openHAB Project MAILEVENT by Robert W.B. Linn
//https://github.com/openhab/openhab/wiki/GPIO-Binding
//Changed: 20160318
//Define the LED which is connected to pin number 12 which is named GPIO18 and therefore bind to PIN 18
Switch LED1 { gpio="pin:18" }
//Define Text Item which is updated when the LED state has changed
String LabelInfo
//Counter how often the LED has been switched. Value is updated via a rule
Number SwitchCounter
Define the Sitemap
Folder: /opt/openhab/configurations/sitemaps
File: mailevent.sitemap
//openHAB Project MAILEVENT by Robert W.B. Linn
//https://github.com/openhab/openhab/wiki/GPIO-Binding
//Access Sitemap: 192.168.0.58:8080/openhab.app?sitemap=mailevent
//Changed: 20160318
sitemap gpioled label="Switch LED On / Off with Mail Event"
{
Frame label="An openHAB Raspberry Pi GPIO Experiment by Robert W.B. Linn"
{
Switch item=LED1 label="LED"
Text item=LabelInfo label="Switched [%s]" icon="info"
Text item=SwitchCounter label="Times [%.0f]" icon="_up"
// Text label="2016 by Robert W.B. Linn" icon="rwblinns"
}
}
Define the Rules
Folder: /opt/openhab/configurations/rules
File: mailevent.rules
//openHAB Project MAILEVENT by Robert W.B. Linn
//https://github.com/openhab/openhab/wiki/Rules
//Changed: 20160315
import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*
import java.lang.String
import java.util.Date
import java.text.SimpleDateFormat
//Switch counter. Define as var Number SwitchCounter and not as var SwitchCounter
var Number mSwitchCounter = 0
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("ON at " + Timestamp )
}
else {
LabelInfo.postUpdate("OFF at " + Timestamp )
}
//Update the counter
mSwitchCounter = mSwitchCounter + 1
logInfo("LED1", "State " + LED1.state + ", Counter = " + mSwitchCounter)
SwitchCounter.postUpdate(mSwitchCounter)
//Send Mail
sendMail("name@mysite", "LED1 Switched", "State " + LED1.state + ", Counter = " + mSwitchCounter)
end
Access the Sitemap via Browser
URL is (using the Raspberry Pi IP Address):
http://192.168.0.58:8080/openhab.app?sitemap=mailevent

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