1.wifi自动重连,led指示连接状态(接通电源led常亮,连接wifi后每隔两秒led闪烁,给弹后每隔0.5秒led闪烁)
pin1 = 1--GPIO5 gpio.mode(pin1,gpio.INT,gpio.PULLUP) pin5 = 5 gpio.mode(pin5, gpio.OUTPUT) function pin11cb() print("up") gpio.write(pin5, gpio.HIGH); gpio.write(pin5, gpio.LOW); end gpio.trig(pin1, "up",pin11cb) status = gpio.LOW; function pin1Shoot() gpio.write(pin1, gpio.HIGH); gpio.write(pin1, gpio.LOW); end --wifi状态更新计时器 --tmr.alarm(2, 2000, tmr.ALARM_AUTO, ChangeD1Status) --LED针脚 D0=0--GPIO16 D1=1--GPIO5 D2=2--GPIO4 D3=3--GPIO0 D4=4--GPIO2 D5=5--GPIO14 D6=6--GPIO12 D7=7--GPIO13 D8=8--GPIO15 D9=9--GPIO3 D10=10--GPIO1 D11=11--GPIO9 D12=12--GPIO10 --LED led2 = D4;--LED针脚 --常量定义 timer = 2000;--LED闪烁间隔 timer_wifi = 2000 timer_shoot = 500 bullets = 0;--子弹数 --先让LED灯高亮 gpio.mode(led2, gpio.OUTPUT) gpio.write(led2, gpio.LOW); --LED灯闪烁变量 ledflash = gpio.LOW; --wifi状态更新函数,如果wifi连接,LED每隔2秒闪烁;如果wifi断开,LED常亮,wifi自动重连 function updateWifiStatus() ip = wifi.sta.getip() if ip == nil then gpio.write(led2, gpio.LOW); print('Setting up WIFI...') wifi.setmode(wifi.STATION) wifi.sta.config('Netcore_09D8C7', '83315769') wifi.sta.autoconnect(1) else timer = timer_wifi gpio.write(led2, ledflash); if ledflash == gpio.HIGH then ledflash = gpio.LOW print(ip..', gpio.LOW') else ledflash = gpio.HIGH print(ip..', gpio.HIGH') end end end --wifi状态更新计时器 tmr.alarm(1,timer,tmr.ALARM_AUTO,updateWifiStatus) srv=net.createServer(net.TCP, 30) srv:listen(80,function(conn) conn:on("receive", function(client,request) local buf = ""; local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP"); if(method == nil)then _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP"); end local _GET = {} if (vars ~= nil)then for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do _GET[k] = v end end buf = buf.."<h1> ESP8266 Web Server</h1>"; buf = buf.."<p>GPIO2 <a href=\"?pin1=ON2\">OFF</a> <a href=\"?pin1=OFF2\">ON</a></p>"; buf = buf.."<p>GPIO2 <a href=\"?pin1=BTN1\"><button>Distributive bullets</button></a> <a href=\"?pin1=BTN2\"><button>shoot</button></a></p>"; local _on,_off = "","" if(_GET.pin1 == "BTN1")then print('Distributive bullets') tmr.stop(1) tmr.interval(1,timer_shoot) tmr.start(1) bullets = 10 elseif(_GET.pin1 == "BTN2")then print('Shoot'..bullets) bullets = bullets - 1 pin1Shoot() if(bullets <= 0) then bullets = 0 tmr.stop(1) tmr.interval(1,timer_wifi) tmr.start(1) end end buf = buf.."<p>bullets "..bullets.."</p>"; client:send(buf); client:close(); collectgarbage(); end) end)2.按钮状态监测
pin = 1--D1 gpio.mode(pin,gpio.INT,gpio.PULLUP) gpio.write(pin, 0) function pin1cb() print("up") end gpio.trig(pin, "up",pin1cb)3.读写配置文件
filename = "config.txt" ip = "192.168.0.2"; port = 6000; function readConfigFile() local files = file.list() if file.open(filename, "r") then ip = file.readline(); port = file.readline(); print("ip "..ip) print("port "..port) file.close() end end function writeConfigFile(ip, port) local files = file.list() if file.open(filename, "w") then -- write 'foo bar' to the end of the file file.writeline(ip) file.writeline(port) file.close() end end4.可模拟给弹、射击
filename = "config.txt" ip = "192.168.0.5" port="6000" connected=false; bullets = 10 function readConfigFile() local files = file.list() if file.open(filename, "r") then ip = file.readline(); port = file.readline(); print("ip "..ip) print("port "..port) file.close() end end function writeConfigFile(ip, port) local files = file.list() if file.open(filename, "w") then -- write 'foo bar' to the end of the file file.writeline(ip) file.writeline(port) file.close() end end --字符串分割 function stringsplit(input, delimiter) input = tostring(input) delimiter = tostring(delimiter) if (delimiter=='') then return false end local pos,arr = 0, {} for st,sp in function() return string.find(input, delimiter, pos, true) end do table.insert(arr, string.sub(input, pos, st - 1)) pos = sp + 1 end table.insert(arr, string.sub(input, pos)) return arr end --作为服务器收到消息后设备TCP客户端要连接的IP function receiveClientMsg(sck, data) print(data) local str = data --"192.168.1.117:60" local str_Arr =stringsplit(str,":") if(#str_Arr == 2)then sck:send("set ip") print(str_Arr[1]) print(str_Arr[2]) writeConfigFile(str_Arr[1],str_Arr[2]) if(connected == true)then cl:close() connected = false end sck:close() else--射击 print('Shoot'..bullets) bullets = bullets - 1 if(bullets <= 0) then bullets = 0 tmr.stop(1) tmr.interval(1,2000) tmr.start(1) end sck:send("bullets "..bullets) end end --作为服务器收到消息后设备TCP客户端要连接的IP function creatTcpClient(newIp, newPort) cl = net.createConnection(net.TCP, 0) cl:connect(newPort, newIp) cl:on("receive", function(sck, c) print(c) bullets = 10 sck:send("set bullets success") end) cl:on("connection", function(sck, c) print("connection!") connected = true end) cl:on("disconnection", function(sck, c) connected = false print("disconnection!") end) end --writeConfigFile("192.168.0.5","6000") --先让LED灯高亮 led2=4 gpio.mode(led2, gpio.OUTPUT) gpio.write(led2, gpio.LOW); --LED灯闪烁变量 ledflash = gpio.LOW; --wifi状态更新函数,如果wifi连接,LED每隔2秒闪烁;如果wifi断开,LED常亮,wifi自动重连 function updateWifiStatus() ip = wifi.sta.getip() if ip == nil then gpio.write(led2, gpio.LOW); print('Setting up WIFI...') wifi.setmode(wifi.STATION) wifi.sta.config('Netcore_09D8C7', '83315769') wifi.sta.autoconnect(1) else timer = timer_wifi if ledflash == gpio.HIGH then gpio.write(led2, gpio.LOW); ledflash = gpio.LOW print(ip..', gpio.LOW') else gpio.write(led2, gpio.HIGH); ledflash = gpio.HIGH print(ip..', gpio.HIGH') end --自动连接TCP server if connected==false then readConfigFile() creatTcpClient(ip, port) end end end --wifi状态更新计时器 tmr.alarm(1,2000,tmr.ALARM_AUTO,updateWifiStatus) --创建TCP服务端 sv = net.createServer(net.TCP, 30) if sv then sv:listen(80, function(conn) conn:on("receive", receiveClientMsg) end) end