sudo vi /opt/openhab/configurations/sitemaps/dolphin.sitemap
and add it like:
Switch item=Toggle label="Toggle"
Next, we need to create a rule to manage this toggling action into HTTP requests:
sudo vi /opt/openhab/configurations/rules/httpreq.rules
and inside this file I add two rules, one to catch the ON and one to catch the OFF:
rule updateToggleOn
when
Item Toggle received command ON
then
sendHttpGetRequest("http://192.168.1.5/?pin=ON")
end
rule updateToggleOff
when
Item Toggle received command OFF
then
sendHttpGetRequest("http://192.168.1.5/?pin=OFF")
end
Now, in this case the device I am controlling is a simple ESP8266 WebApp from https://github.com/nodemcu/nodemcuirmware/blob/master/lua_examples/webap_toggle_pin.lua to demonstrate the idea to Michael. Since its this simple sketch, you can expect a cosmetic error to popup in openhab.log:
05:37:12.169 ERROR o.openhab.io.net.http.HttpUtil[:230]- Fatal protocol violation: org.apache.commons.httpclient.ProtocolException: The server 192.168.1.5 failed to respond with a valid HTTP response
The ESP8266 sketch doesnt return a valid HTTP header. No worries though, ignore the error – it still works!
So, the above example takes care of a device needing an “on” and “off” …
But what about a a “toggle” or “momentary” button (Garage door opener for example?)
Easy: One simple change in the Items file and one less rule:
Schematics, Board and Gerbers are released as open source hardware under a CC-BY-SA licence and you can find it on my GitHub repo at https://github.com/openhardwarecoza/esp8266-PROJECTS/tree/master/ESP8266-%204%20channel%20mosfet%20switch
New to what we’ve done so far, we need to add a Rule as well into a new file under /opt/openhab/configuration/rules/dolphin.rules for example.
This rule takes the inputs from the Item above and transforms it into RGB values to send over MQTT
import org.openhab.core.library.types.*
var HSBType hsbValue
var int redValue
var int greenValue
var int blueValue
var String RGBvalues
rule "Set RGB 2 value"
when
Item fWIFI_RGB_2 changed
then
hsbValue = fWIFI_RGB_2.state as HSBType
redValue = hsbValue.red.intValue
greenValue = hsbValue.green.intValue
blueValue = hsbValue.blue.intValue
RGBvalues= redValue.toString + ";" + greenValue.toString + ";" + blueValue.toString
sendCommand( WIFI_RGB_2_RGB, RGBvalues )
logInfo( "fWIFI_RGB_2", RGBvalues )
end
Note: Based off code written by Andreas Holldorfer (http://chaozlabs.blogspot.co.za/2015/08/esp8266-in-wild-wifi-led-controller-hack.html) but adapted to my own PCBs