-
Notifications
You must be signed in to change notification settings - Fork 14
Control an output with a Button in PellmonWeb
This recipe shows how to create a button in the webinterface to toggle the state of a raspberry pi output, with the output state persisting over reboot.
Any write-only item will show up as a button in the webinterface. The calculate plugin allows creating write-only items that causes the the connected script to be executed when something is written to them. Pressing the button will write a 0 to the script, but the value is not important here.
The state has to be stored somewhere to persist over reboot, to accomplish this it can be written to a read-write calculate script. Read-write scripts can be edited in the webinterface, but they can also be read and written by other scripts, which here is taken advantage of to hold the value.
Since the saved button state has to be written to the output after reboot and not only when the button is pressed, the most simple way to control the output is to add a periodic script that writes the button state to the output regardless of it having changed or not.
The output state is available as a 0 or 1 by reading the output item. To get a fancier description of the output state another script can be added that converts the output value to a text, for instance 'Open' or 'Closed', which can then be added to the system image to express the state of the controlled gadget.
[plugin_Calculate]
# The 'btn1state' read/write script is used to remember the button state over reboot
btn1state_prog = 0
btn1state_progtype = R/W
# The 'btn1' script reads the btn1state_prog value and writes back the inverse every time
# it is executed
btn1_prog =
if btn1state_prog get 0 == then
1 btn1state_prog set
else
0 btn1state_prog set
end
# Connect a writeonly item named 'Button1' to the btn1 script. It will be exececuted once
# every time something is written to the 'Button1' item.
btn1_writeitem = Button1
# This 1 second periodic script writes the button state to the 'button1output' item, which
# can be an output configured in the RaspberryGPIO plugin or the OWFS plugin.
btn1out_prog = btn1state_prog get button1output set
btn1out_taskcycle = 1
# Add a script that returns the text 'Open' or 'Closed' depending on the 'button1output' state.
btn1text_prog =
if btn1state_prog get 0 == then
Closed
else
Open
end
# The 'btn1text' script is executed when the item 'btn1_text' is read
btn1text_readitem = btn1_text
The item button1output is created and connected to pin 7 in the raspberryGPIO plugin, but it could also be eg a onewire output.
[plugin_RaspberryGPIO]
btn1gpio_function = output
btn1gpio_item = button1output
# Use GPIO4, pin 7
btn1gpio_pin = 7