OpenWRT 添加 WEB 配置界面实战记录

本篇是记录在 Openwrt 镜像中添加 自定义的 web 配置界面过程,编译进 openwrt 的系统镜像中。

第一步 建立项目文件目录

mkdir -p feeds/luci/applications/luci-app-Gateway
mkdir -p feeds/luci/applications/luci-app-Gateway/config 
mkdir -p feeds/luci/applications/luci-app-Gateway/luci 
mkdir -p feeds/luci/applications/luci-app-Gateway/module

第二步 编写 controller 的 lua 源码

wooya@hi-wooya:~/openwrt-hiwooya/feeds/luci/applications/luci-app-Gateway$ cat luasrc/controller/gateway.lua 
module("luci.controller.gateway", package.seeall) 
function index()
    entry({"admin", "services", "Gateway"}, cbi("gateway/gateway_conf"), translate("Gateway"), nil)
    entry({"admin", "services", "Service"}, template("gateway/helloworld"), _("Online help"), nil)
end

第三步 编写 model 模块内容

wooya@hi-wooya:~/openwrt-hiwooya/feeds/luci/applications/luci-app-Gateway$ cat luasrc/model/cbi/gateway/gateway_conf.lua 
m = Map("gatewayConf", translate("Gateway")) -- cbi_file is the config file in /etc/config

g = m:section(TypedSection, "gateway")  -- info is the section called info in cbi_file

sn = g:option(Value, "sn", translate("Gateway SN"),"BCD code Eg:abcdef202202110001000101");

addr = g:option(Value,"IPv4addr",translate("Server IPv4:Port"),"eg:172.168.10.211:1813");

heart= g:option(Value, "HeartBeat", translate("MQTT HeartBeat"), "d:second");

name = g:option(Value, "UserName", translate("MQTT UserName"));

pwd = g:option(Value, "Password", translate("MQTT Password"));

return m

第四步 编码在线帮助web 网页

wooya@hi-wooya:~/openwrt-hiwooya/feeds/luci/applications/luci-app-Gateway$ cat luasrc/view/gateway/helloworld.htm 
<%+header%>
<h1><%: HelloWorld %></h1>
<%+footer%>

第五步 创建配置参数确实值

wooya@hi-wooya:~/openwrt-hiwooya/feeds/luci/applications/luci-app-Gateway/root/etc/config$ cat gatewayConf
config gateway 'parament'
        option HeartBeat '60'
        option sn 'abcdef202202110001000101'
        option UserName 'admin'
        option Password '123456'
        option IPv4addr '81.70.211.95:1883'

此文件镜像打包后的系统路径: etc/config 网页中显示和配置内容。

第六步 验证文件路径

wooya@hi-wooya:~/openwrt-hiwooya/feeds/luci/applications/luci-app-Gateway$ tree -L 5
.
├── luasrc
│   ├── controller
│   │   └── gateway.lua
│   ├── model
│   │   └── cbi
│   │       └── gateway
│   │           └── gateway_conf.lua
│   └── view
│       └── gateway
│           └── helloworld.htm
├── Makefile
└── root
    └── etc
        └── config
            └── gatewayConf

至此我们把 luci 相关源码准备好,开始编译验证

第七步 编译 luci app 模块

建立软连接并安装 luci_app_gateway 模板

//> ln -s
wooya@hi-wooya:~/openwrt-hiwooya$ cd package/feeds/luci
wooya@hi-wooya:~/openwrt-hiwooya$ package/feeds/luci$ ln -s ../../../feeds/luci/applications/luci-app-Gateway/ .
wooya@hi-wooya:~/openwrt-hiwooya$ package/feeds/luci$ ls luci-app-Gateway
luasrc  Makefile  root
wooya@hi-wooya:~/openwrt-hiwooya$ package/feeds/luci$ ls luci-app-Gateway/luasrc/
controller  model  view
//> install -p luci
wooya@hi-wooya:~/openwrt-hiwooya$ ./scripts/feeds install -a -p luci
Installing all packages from feed luci.
Installing package 'luci-app-Gateway'

# make menuconfig 

Symbol: DEFAULT_luci-app-Gateway [=DEFAULT_luci-app-Gateway]                                                                     
  │ Type  : unknown                                                                                                                  
  │                                                                                                                                 
  │                                                                                                                                 
  │ Symbol: PACKAGE_luci-app-Gateway [=y]                                                                                            
  │ Type  : tristate                                                                                                                 
  │ Prompt: luci-app-Gateway........................... LuCI Support for Gateway-menu                                                 
  │   Location:-> LuCI                                                                                                                      
  │ (1)   -> 3. Applications                                                                                                         
  │   Defined at tmp/.config-package.in:27354                                                                                       
  │   Selects: PACKAGE_librt [=y] && PACKAGE_libc [=y] && PACKAGE_libpthread [=y] && PACKAGE_libssp [=n]   

烧写镜像后,可以在 services 菜单中,看到 Gateway 和 online help 子菜单内容。下一篇将分享如果在 C++ 程序通过 libuci.so 库
方式,获取web设置内容。

如果您对 Luci 相关语法有些陌生,可以移步至 openwrt.org 网站中wiki部分检索。

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、因手头有一个7620N芯片的大功率无线路由器(万兴达WR5508商用路由),由于要使用无线广告营销功能,但在网上搜寻了很久,都没找到合适的固件。 2、可能有人会说支持7620方案的广告营销固件多的是,但这款路由器是大功率的,500mW,刷了海蜘蛛、RippleOS以及其他有wifidog功能的固件,无线超过3-5米就没有信号了。只有刷了openwrt和PandoraBox固件,无线信号超级强大,达到26db。 3、因此,只能使用openwrt和PandoraBox固件,查询了多种方法,都是需要对固件进行编译的,由于小弟不懂编程,只有通过网友们的安装配置经验进行参考,在刷好的固件基础上安装wifidog和Luci界面配置,如果哪位高手能将Luci-wifidog源码打包成ipk安装包,小弟万分感谢!此方法仅供想玩wifidog的朋友参考,编程高手勿拍砖就行了! 方法: 1、安装wifidog(前提是路由器必须接入互联网) [root@PandoraBox:/root]#opkg update [root@PandoraBox:/root]#opkg install wifidog [root@PandoraBox:/root]#/etc/init.d/wifidog enable [root@PandoraBox:/root]#/etc/init.d/wifidog start 备注:安装wifidog可能需要依赖包 libc iptables-mod-extra iptables-mod-ipopt iptables-mod-nat-extra libpthread 2、安装配置luci-wifidog(luci-wifidog源码是在网上找的,别人已经设置好了的,如果不喜欢,自行到官网下) (1)使用winscp把源码controller文件夹下的内容复制到/usr/lib/lua/luci里目录的controller下,model放到model下 (2)源码root文件夹下的内容复制到root/etc下各自相应的目录下 (3)源码ipkg文件夹下的内容复制到/usr/lib/opkg目录下 (4)源码makefile文件复制到/etc下并给xxx权限 (5)最后重启路由器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值