https://www.zhihu.com/question/31579325html
uhttpd + Fast-CGI的開發要求:web
以「讀取GET和POST數據」爲例,所有使用默認值的狀況下,如何具體實現:瀏覽器
第一步:使用「opkg update」更新「.ipk」包的源,而後使用「opkg install uhttpd」安裝;緩存
第二步:使用「vi /etc/config/uhttpd」,在「config uhttpd main」下添加一行curl
list interpreter ".lua=/usr/bin/lua」
第三步:創建目錄「/www/cgi-bin」,並增長「+x」屬性;函數
root@OpenWrt:~# mkdir -p /www/cgi-bin/
root@OpenWrt:~# chmod +x /www/cgi-bin/
創建響應文件,並增長「+x」屬性;測試
root@OpenWrt:~# touch /www/cgi-bin/webservice
root@OpenWrt:~# chmod +x /www/cgi-bin/webservice
將文件「/www/cgi-bin/webservice」內容修改成:ui
#!/usr/bin/lua
local WebService = require 'WebService'
WebService.Run()
第四步:創建Lua模塊文件(再也不要求「+x」屬性),並讀取參數,返回響應:lua
root@OpenWrt:~# touch /usr/lib/lua/WebService.lua
將其內容修改成以下內容:url
local WebService = {}
function WebService.Run()
local client = os.getenv("REMOTE_ADDR")
local GET = os.getenv("QUERY_STRING")
local POST = nil
local POSTLength = tonumber(os.getenv("CONTENT_LENGTH")) or 0
if (POSTLength > 0) then
POST = io.read(POSTLength)
--POST = io.read("*a")
end
-- Fast-CGI+HTTP require HEADER
-- enable cache
--io.write("Content-type: text/html\n\n")
-- disable cache, especially for Internet Explorer
io.write("Content-type: text/html\nPragma: no-cache\n\n")
local reply = string.format("Client %s said: url: [%s], data: [%s]\n", client or '-', GET or '-', POST or '-')
io.write(reply)
end
return WebService
第五步:重啓「uhttpd」服務(爲了讓更改的/etc/config/uhttpd生效):
root@OpenWrt:~# /etc/init.d/uhttpd restart
第六步:使用瀏覽器測試:
使用瀏覽器訪問以下地址:
http://<openwrt_ipaddr>/cgi-bin/webservice?author=qige
這裏假設OpenWrt開發板的IP爲192.168.1.24:
http://192.168.1.24/cgi-bin/webservice?author=qige
最終效果以下圖:
注意:
root@OpenWrt:~# opkg install coreutils
root@OpenWrt:~# opkg install coreutils-stty
再將如下內容添加到Lua模塊的適當位置,並調用此函數便可:
function WebService.uartWrite(msg)
local uartConfig = 'stty -F /dev/ttyS0 raw speed 9600\n'
os.execute(uartConfig)
local cmd = string.format("echo '%s' > /dev/ttyS0\n", msg or '')
os.execute(cmd)
end