OpenWrt Forum Archive

Topic: luci as a general web interface to GPIO

The content of this topic has been archived on 16 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hi all,
does anyone have links for creating a luci page that will allow me to see and/or change GPIO values?
I'd rather I could leverage luci, than have to install php/mysql or some other space hogs.
Provisionally, I am thinking visual status (1/0) of GPIOs, possible switch for output GPIOs
TIA
Rob.

(Last edited by robthebrew on 1 Nov 2012, 15:11)

Yes, I agree that LuCI is a much lighter solution than all the php/mysql stuff!

I am currently digging into LuCI code, as the given tutorial and examples are not very meaningful sad

First, I changed the default theme to the much nicer 'Bootstrap' (a subset of Twitter's framework):
http://nut-bolt.nl/2012/openwrt-bootstr … -for-luci/

Bootstrap

It should already be into the AA branch, and if it were just me, I would make it the default theme, rather than the Windows-95-looking default OpenWrt theme smile.

LuCI is using the model/view/controller paradigm, so the model contains the internal representation of things, the view is what is displayed to the user, and the controller is in charge of transferring information between the 2 others.

It is of course a good practice, but it makes things difficult to follow if you don't know where to look as it is basically non-linear code.

I am trying to achieve the same goal as you, so let's share our knowledge!

I will try to make a button to toggle the USB power ("echo [0,1] > /sys/classes/gpio/gpio8/value") as a first example. It is useful as is to control an Arduino board power (so when the USB communication over USB hangs), or a solid state relay to control AC-powered devices over Wifi.

I had a dig around. Bear in mind I have no experience with lua or luci!
If you set up gpio8 as an LED in tl-wr703n.c, you can then use the LED tab in luci to control the value of gpio8.
It ought to be possible to do similar for inputs via the buttons tab, except that the source does not seem to be included in luci-admin-full (ie there is no button tab).
Need to hunt down why.

Using the LED tab to control the USB power is not very elegant smile

However, you are right, this may be what is the closest to what we want to do, so I will start from there.

Actually, the button tab is included into luci-admin-full, but it is severely broken...

The CBI LuCI <=> uci config binding file is "/modules/admin-full/luasrc/model/cbi/admin_system/buttons.c" in the build_dir, and placed into "/usr/lib/lua/luci/model/cbi/admin_system" on the router, but it lacks the mandatory final "return m" statement!

Then, it should be called by the parent index() function in the "/modules/admin-full/luasrc/controller/admin/system.lua" controller, but it is not...

Here is the patch file:

diff -Naur luci-0.11+svn9413.orig/modules/admin-full/luasrc/controller/admin/system.lua luci-0.11+svn9413/modules/admin-full/luasrc/controller/admin/system.lua
--- luci-0.11+svn9413.orig/modules/admin-full/luasrc/controller/admin/system.lua    2012-08-08 12:11:00.000000000 +0200
+++ luci-0.11+svn9413/modules/admin-full/luasrc/controller/admin/system.lua    2012-11-02 18:34:39.000000000 +0100
@@ -40,6 +40,8 @@
         entry({"admin", "system", "leds"}, cbi("admin_system/leds"), _("<abbr title=\"Light Emitting Diode\">LED</abbr> Configuration"), 60)
     end
 
+    entry({"admin", "system", "buttons"}, cbi("admin_system/buttons"), _("Buttons"), 65)
+
     entry({"admin", "system", "flashops"}, call("action_flashops"), _("Backup / Flash Firmware"), 70)
     entry({"admin", "system", "flashops", "backupfiles"}, form("admin_system/backupfiles"))
 
diff -Naur luci-0.11+svn9413.orig/modules/admin-full/luasrc/model/cbi/admin_system/buttons.lua luci-0.11+svn9413/modules/admin-full/luasrc/model/cbi/admin_system/buttons.lua
--- luci-0.11+svn9413.orig/modules/admin-full/luasrc/model/cbi/admin_system/buttons.lua    2009-11-01 02:37:03.000000000 +0100
+++ luci-0.11+svn9413/modules/admin-full/luasrc/model/cbi/admin_system/buttons.lua    2012-11-02 18:35:13.000000000 +0100
@@ -36,3 +36,4 @@
 
 max = s:option(Value, "max", translate("Maximum hold time"))
 max.rmempty = true
+return m

Here is the result using the beautiful "Bootstrap" theme:

http://img442.imageshack.us/img442/9907/lucibuttons.png

What this page basically do is to modify the corresponding uci config entries in /etc/config/system like this:

config button
        option button 'reset'
        option action 'released'
        option handler 'logger reset button pressed'
        option min 0
        option max 3

In order to do something actually useful, you must follow the tutorial in the wiki about buttons:
http://wiki.openwrt.org/doc/howto/hardware.button

In this tutorial:

  • the "button" events are enabled in the "/etc/hotplug2.rules" config file

  • a button handler script is created. In the original example, the first "#!/bin/sh" dashbang line was missing, resulting in nothing happening at all!

Eventually now, when I press the reset button on my TL-WR703N router for less than 3 s, I get a message in the log "reset button pressed", that I can follow using "logread -f" cool

I vote also for the bootstrap theme. The default theme is really horrifying. I suspect developers don't want we use the web interface, since it's always better idea to configure things via CLI big_smile.

The GPIO section is a good idea, since GPIOs are one of the most useful things a router has. And could be also used to  easily locate the gpio number of a led or buttons for new boards, or boards already with partial Openwrt support with unidentified leds among other things.

The discussion might have continued from here.