参考文献来源
链接:https://www.cnblogs.com/yangfengwu/p/7533302.html
链接:https://blog.youkuaiyun.com/loser_11/article/details/78552815
实现功能:同一局域网中8266向电脑发送数据。
esp8266写入lua程序如下
wifi.setmode(wifi.STATIONAP)
stationcfg={
ssid="xxx"; --无线网名称,有密码需加pwd
auto=true;
}
wifi.sta.config(stationcfg)
flag=0;
wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
print("\n\tSTA - GOT IP".."\n\tStation IP: "..T.IP.."\n\tSubnet mask: "..
T.netmask.."\n\tGateway IP: "..T.gateway)
--连接服务器
Client = net.createConnection(net.TCP, 0)
Client:connect(60000,'10.xxx.xxx.xxx')
tmr.alarm(1, 1000,tmr.ALARM_AUTO, function()
Client:on("receive", function(Client, c) --接收采集命令,开始采集
Client:send("ack")
end)
Client:on("connection", function(Client, c)
--tmr.stop(1)
tmr.alarm(0,1000,tmr.alarm_auto,function()
client:send("abced")
end)
--tmr.stop(1)
end)
Client:on("disconnection", function(Client, c)
rep_connect()
tmr.start(1)
end)
end)
end)
function rep_connect()
Client = net.createConnection(net.TCP, 0)
print("connect time"..flag)
Client:connect(60000,'10.xxx.xxx.xxx')
flag=flag+1
end
----------------------------------------------------
再打开网络调试助手,设置tcp服务器,监听60000端口,即可实现
tcp服务器发送任意命令
收到’ack‘
图片: