找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4005|回复: 0
打印 上一主题 下一主题
收起左侧

ESP8266 lua设置模块连接路由器开启TCP服务器的源代码与详解

[复制链接]
跳转到指定楼层
楼主
本帖最后由 沙漠之痕 于 2018-11-7 12:48 编辑

1. 连接路由器,创建服务器监听8080端口
init.lua文件:
tmr.alarm(0,5000,0,function()
    dofile("tcpserver.lua")
end)
init.lua文件结束
tcpserver.lua文件:
wifi.setmode(wifi.STATIONAP)
local stacfg = {
    ssid = "qqqqq",
    pwd = "11223344"
}
wifi.sta.config(stacfg)
wifi.sta.autoconnect(1)
local ClientSocket = nil   
Server = net.createServer(net.TCP,28800)
Server:listen(8080,function(socket)
        ClientSocket=socket
        ClientSocket:on("receive",function(sck,data)
           uart.write(0,data)
        end)
        ClientSocket:on("disconnection",function() ClientSocket=nil
            print("Disconnec")
        end)
end)
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)
tcpserver.lua文件结束

手机连接上路由器,并去连接模块开启的服务器,模块ip即为服务器ip通过串口已经打印出

通过手机客户端给模块服务器发送数据通过模块的串口输出

2. 通过手机发送”open”打开继电器;发送 “close”关闭继电器
init.lua文件
RELAY_Pin = 1
gpio.mode(RELAY_Pin,gpio.OUTPUT)
gpio.write(RELAY_Pin,0)
tmr.alarm(0,5000,0,function()
    dofile("tcpserver.lua")
end)
init.lua文件结束
tcpserver.lua文件:
ServerReceData = ""
ServerReceCnt = 0
ServerReceStat = false
wifi.setmode(wifi.STATIONAP)
local stacfg = {
    ssid = "qqqqq",
    pwd = "11223344"
}
wifi.sta.config(stacfg)
wifi.sta.autoconnect(1)
local ClientSocket = nil   
Server = net.createServer(net.TCP,28800)
Server:listen(8080,function(socket)
        ClientSocket=socket
        ClientSocket:on("receive",function(sck,data)
        ServerReceData = ServerReceData..data
        ServerReceStat = true  
        ServerReceCnt = 0
        end)
        ClientSocket:on("disconnection",function() ClientSocket=nil
            print("Disconnec")
        end)
    end
)
tmr.alarm(1,10,1,function()
    if ServerReceStat == true then
        ServerReceCnt = ServerReceCnt + 1
        if ServerReceCnt >= 10 then
            if ServerReceData ==  "open" then
                gpio.write(RELAY_Pin,1)
            end
            if ServerReceData ==  "close" then
                gpio.write(RELAY_Pin,0)
            end
            uart.write(0,ServerReceData)
            ServerReceData = ""
            ServerReceCnt = 0
            ServerReceStat = false
        end
    end
end)
---------------Got IP -------------------
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 ------------------
tcpserver.lua文件结束



手机连接路由器并连接服务器




ESP8266 连接路由器 tcp服务 lua源码.zip (2.49 KB, 下载次数: 19)


交流QQ:1813763867
QQ群:607064330






分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表