0x01.About
第一次尝试开发路由器,发现并不是想象中那么难,和普通嵌入式开发一样,也是一块ARM板刷上Linux系统。
OpenWrt有很多好用的软件,附带流量监测。
OpenWrt主要开发语言为Python、Lua、Shell,还可以做深入研究写ipk软件包。
写了几个脚本,主要实现了openwrt下面GPIO控制、系统信息获取、wifi扫描器、定时发送邮件系统报警等功能,下面会介绍。
代码已经在Github开源: https://github.com/grasses/OpenWRT-Util
0x02.About OpenWrt
刷OpenWrt先要去https://downloads.openwrt.org/下载你想要的版本,包含aa型和bb型。
然后用Linux烧入命令烧入系统。
早MAC下面,先现将U盘插入电脑格式化,然后运行命令查看U盘编号:
diskUtil list
注意查看U盘编号,选择你的U盘,解除挂载:
diskUtil unmountDisk /dev/disk2
然后烧入系统:
dd if=/path/to/openwrt.img of=/dev/disk2 bs=2m
等待几分钟后烧入成功。
关于痛点:
第一次是在树莓派上安装OpenWrt,装好后,用有线把连进上级路由器的Lan口
然后,上级路由的包开始乱了,上级路由把OpenWrt当成路由器,OpenWrt把路由器当成上级路由器,然后就GG了。
0x03.About WRTnode
WRTnode是OpenWrt系统一个硬件解决方案,预先安装了OpenWrt相关软件包,并且内置两块无线网卡。
关于WRTnode,官方wiki已经介绍的很详细了:http://wiki.wrtnode.com/index.php?title=Main_Page/zh-cn
解析来的代码基本上是在WRTnode环境上开发的,主要包含了:
luci(WRTnode自带,非WRTnode用opkg安装即可)
python(WRTnode自带,非WRTnode用opkg安装即可)
luasocket( http://see.sl088.com/wiki/Luasocket )
目前只能想起这3个,如果报错,该装什么再装好了。
0x04.WRTnode控制GPIO
GPIO控制可以很好地实现软件硬件之间的交互。
GPIO的控制也不难,wiki讲得很清晰了,就是文件输入输出http://wiki.wrtnode.com/index.php?title=The_user_space_gpio_calls/zh-cn
这里我写了一个Lua版的GPIO控制模块,文件保存为gpio.lua:
#!/usr/bin/lua
--[[
Copyright 2015 http://homeway.me
@author homeway
@version 15.04.29
@link http://homeway.me
@function OpenWRT gpio module
-- ]]--
local M = {}
M.id = ""
M.path = "/sys/class/gpio/gpio"
M.router = "/sys/class/gpio"
M.check = function(where)
print("check path => "..where)
local f=io.open(where, "r")
if f~=nil then io.close(f) return true else return false end
end
-- set mode && check type
M.mode = function(id, mtype)
M.id = id
where = M.path..M.id
-- if id not use
if false==M.check(M.path..id..'/direction') then
--M.writeFile(M.router.."/unexport",id)
M.writeFile(M.router.."/export", id)
end
-- if type different
if mtype ~= M.readFile(M.path..id..'/direction') then
print("type =>"..mtype.." direction=>"..M.readFile(M.path..id..'/direction').." different")
M.writeFile(M.path..id..'/direction', mtype)
end
end
-- file write
M.writeFile =