本帖最后由 沙漠之痕 于 2018-11-7 12:45 编辑
1. 设置模块连接路由器,并打印路由器分配的ip: Init.lua文件: tmr.alarm(0,4000,0,function() dofile("tcpclient.lua") end) Init.lua文件结束 tcpclient.lua文件: wifi.setmode(wifi.STATION) cfg={} cfg.ssid="qqqqq" cfg.pwd="11223344" wifi.sta.config(cfg) printip = 0 wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T) printip = 0 end) wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T) if printip == 0 then print("+IP-> "..T.IP) end printip = 1 end) tcpclient.lua文件结束 2. 参照官方手册,创建客户端连接服务器;看见模块获取了ip但是连接不上服务器 Init.lua文件: tmr.alarm(0,4000,0,function() dofile("tcpclient.lua") end) Init.lua文件结束
tcpclient.lua文件: wifi.setmode(wifi.STATION) local stacfg={ ssid="qqqqq", pwd="11223344" } wifi.sta.config(stacfg) wifi.sta.autoconnect(1) TCPClient = net.createConnection(net.TCP,0) TCPClient:connect(8080,"192.168.0.105") TCPClient:on("receive", function(sck, data) uart.write(0,data) end) TCPClient:on("disconnection", function(sck, c) print("Disconnection") end) TCPClient:on("connection", function(sck, c) print("Connection") end) ----------Got IP Style:+IP-> xxx.xxx.xxx.xxx---------------------------- printip = 0 wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T) printip = 0 end) wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T) if printip == 0 then print("+IP-> "..T.IP) end printip = 1 end) ----------------------------END Got IP---------------------------------- tcpclient.lua文件结束 3. 是不是,没有获取到ip就去连接了呢?添加一个定时器延时10秒后再去创建TCP客户端连接服务器,这下可以看到能连接上服务器 Init.lua文件: tmr.alarm(0,4000,0,function() dofile("tcpclient.lua") end) Init.lua文件结束 tcpclient.lua文件: wifi.setmode(wifi.STATION) local stacfg={ ssid="qqqqq", pwd="11223344" } wifi.sta.config(stacfg) wifi.sta.autoconnect(1) ----------Got IP Style:+IP-> xxx.xxx.xxx.xxx---------------------------- printip = 0 wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T) printip = 0 end) wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T) if printip == 0 then print("+IP-> "..T.IP) end printip = 1 end) ----------------------------END Got IP---------------------------------- tmr.alarm(3,10000,0,function() TCPClient = net.createConnection(net.TCP,0) TCPClient:connect(8080,"192.168.0.105") TCPClient:on("receive", function(sck, data) uart.write(0,data) end) TCPClient:on("disconnection", function(sck, c) print("Disconnection") end) TCPClient:on("connection", function(sck, c) print("Connection") end) end) tcpclient.lua文件结束 那断开服务器,再重新打开服务器,客户端又不连接了。。。 4. 如何让模块断开连接后一直自动重连服务器呢? Init.lua文件: tmr.alarm(0,4000,0,function() dofile("tcpclient.lua") end) Init.lua文件结束 tcpclient.lua文件: wifi.setmode(wifi.STATION) local stacfg={ ssid="qqqqq", pwd="11223344" } wifi.sta.config(stacfg) wifi.sta.autoconnect(1) tmr.alarm(3,1000,1,function() TCPClient = net.createConnection(net.TCP,0) TCPClient:connect(8080,"192.168.0.105") TCPClient:on("receive", function(sck, data) uart.write(0,data) end) TCPClient:on("disconnection", function(sck, c) print("Disconnection") tmr.start(3) end) TCPClient:on("connection", function(sck, c) print("Connection") tmr.stop(3) end) end) ----------Got IP Style:+IP-> xxx.xxx.xxx.xxx---------------------------- printip = 0 wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T) printip = 0 end) wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T) if printip == 0 then print("+IP-> "..T.IP) end printip = 1 end) ----------------------------END Got IP---------------------------------- tcpclient.lua文件结束 5. 通过接收TCP服务器的控制指令“open”和“close”控制继电器动作
Init.lua文件: RELAY_Pin = 1 gpio.mode(RELAY_Pin, gpio.OUTPUT) gpio.write(RELAY_Pin,0) tmr.alarm(0,4000,0,function() dofile("tcpclient.lua") end) Init.lua文件结束 tcpclient.lua文件: LinkStat = 0 TCPClient = nil ClientReceData = "" ClientReceCnt = 0 ClientReceDataStat = true wifi.setmode(wifi.STATION) local stacfg={ ssid="qqqqq", pwd="11223344" } wifi.sta.config(stacfg) wifi.sta.autoconnect(1) tmr.alarm(1,1000,1,function() if LinkStat == 0 then TCPClient = net.createConnection(net.TCP,0) TCPClient:connect(8080,"192.168.0.105") TCPClient:on("receive", function(sck, data) ClientReceData = ClientReceData..data ClientReceCnt = 0 ClientReceDataStat = true end) TCPClient:on("connection", function(sck, c) LinkStat = 1 print("Connection") tmr.stop(1) end) TCPClient:on("disconnection", function(sck, c) LinkStat = 0 print("Disconnection") tmr.start(1) end) if LinkStat == 0 then print("LinkError") end end end) tmr.alarm(2,10,1,function() if ClientReceDataStat==true then ClientReceCnt=ClientReceCnt+1 if ClientReceCnt>=10 then --receive finsion if ClientReceData=="open" then gpio.write(RELAY_Pin,1) end if ClientReceData=="close" then gpio.write(RELAY_Pin,0) end uart.write(0,ClientReceData) ClientReceData = "" ClientReceCnt = 0 ClientReceDataStat = false end end end) ----------Got IP Style:+IP-> xxx.xxx.xxx.xxx---------------------------- printip = 0 wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T) printip = 0 end) wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T) if printip == 0 then print("+IP-> "..T.IP) end printip = 1 end) ----------------------------END Got IP---------------------------------- tcpclient.lua文件结束 6. 我想使用串口助手给模块发送数据,然后让模块吧数据发送给TCP服务器: TCP服务器发送的数据我们通过模块的串口发送出来了,但是服务器并没有接收到串口发送过来的数据 通过串口助手发送数据给服务器: Init.lua文件 RELAY_Pin = 1 gpio.mode(RELAY_Pin, gpio.OUTPUT) gpio.write(RELAY_Pin,0) tmr.alarm(0,4000,0,function() dofile("tcpclient.lua") end) Init.lua文件结束 tcpclient.lua文件 LinkStat = 0 TCPClient = nil ClientReceData = "" ClientReceCnt = 0 ClientReceDataStat = true wifi.setmode(wifi.STATION) local stacfg={ ssid="qqqqq", pwd="11223344" } wifi.sta.config(stacfg) wifi.sta.autoconnect(1) tmr.alarm(1,1000,1,function() if LinkStat == 0 then TCPClient = net.createConnection(net.TCP,0) TCPClient:connect(8080,"192.168.0.105") TCPClient:on("receive", function(sck, data) ClientReceData = ClientReceData..data ClientReceCnt = 0 ClientReceDataStat = true end) TCPClient:on("connection", function(sck, c) LinkStat = 1 print("Connection") tmr.stop(1) end) TCPClient:on("disconnection", function(sck, c) LinkStat = 0 print("Disconnection") tmr.start(1) end) if LinkStat == 0 then print("LinkError") end end end) tmr.alarm(2,10,1,function() if ClientReceDataStat==true then ClientReceCnt=ClientReceCnt+1 if ClientReceCnt>=10 then --receive finsion if ClientReceData=="open" then gpio.write(RELAY_Pin,1) end if ClientReceData=="close" then gpio.write(RELAY_Pin,0) end uart.write(0,ClientReceData) ClientReceData = "" ClientReceCnt = 0 ClientReceDataStat = false end end end) ----------Got IP Style:+IP-> xxx.xxx.xxx.xxx---------------------------- printip = 0 wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T) printip = 0 end) wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T) if printip == 0 then print("+IP-> "..T.IP) end printip = 1 end) ----------------------------END Got IP---------------------------------- ------------------uart send data to server ---------------- uart.on("data",0,function(data) if TCPClient ~= nil then TCPClient:send(data,function() end) end end,0) --------------------end uart send data to server------------------ tcpclient.lua文件结束
|