【ESP8266之LUA开发】三、建立TCP服务器,实现socket通信控制继电器,串口,服务器,客户端收发数据小感悟

举报
ReCclay 发表于 2022/02/22 01:42:24 2022/02/22
【摘要】 8266做服务器,实现TCP通信 注,实际烧录的时候会因为注释过多造成烧录不进去的情况,这个时候需要删除注释! 先来测试8266建立服务器,并分得IP 烧录时,先烧wifi.lua,...

8266做服务器,实现TCP通信

,实际烧录的时候会因为注释过多造成烧录不进去的情况,这个时候需要删除注释!

先来测试8266建立服务器,并分得IP

烧录时,先烧wifi.lua,然后再烧init.lua

init.lua

--小灯   gpio2 -> IO4
--继电器 gpio4 -> IO2

gpio.mode(4, gpio.OUTPUT) 
gpio.mode(2, gpio.OUTPUT) 

gpio.write(4, 1) 

tmr.alarm(0, 1000, 1, function()
    gpio.write(4, 1-gpio.read(4))
end)

tmr.alarm(1, 2000, 0, function()
    dofile("wifi.lua")
end)


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

wifi.lua

wifi.setmode(wifi.STATIONAP)

cfg = {} 
cfg.ssid = "Hello8266"
cfg.pwd = "11223344"
wifi.ap.config(cfg)

apcfg = {}
apcfg.ssid = "qqqqq"
apcfg.pwd = "11223344"
wifi.sta.config(apcfg)
wifi.sta.autoconnect(1) 

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 = 0
end)
      

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

具体的函数介绍,还是看LUAAPI<这里>即可!


建立服务器并分得相应IP没问题了,就可以建立socket通信了!

init.lua

同上


  
 
  • 1
  • 2

wifi.lua

wifi.setmode(wifi.STATIONAP)

cfg={}
cfg.ssid="Hellow8266"
cfg.pwd="11223344"
wifi.ap.config(cfg)

apcfg={}
apcfg.ssid="qqqqq"
apcfg.pwd="11223344"
wifi.sta.config(apcfg)
wifi.sta.autoconnect(1)

TCPSever=net.createServer(net.TCP,28800) 

TCPSever:listen(8080,function(socket) 
    socket:on("receive",function(socket,data) 
          if data == "++MD610" then
             gpio.write(2,1)
             socket:send("relay=1")
          end
          if data == "++MD600" then
             gpio.write(2,0)
             socket:send("relay=0")
          end
    end) 

    socket:on("disconnection",function(sck,c) 
          socket = nil
    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)

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

服务器和客户端进行发送接收数据的同时,让串口也参与进来。

8266建立服务器并分得IP,客户端连接相应IP,发送控制命令。

服务器收到命令后,先把收到的命令通过uart.write8266的串口发送到电脑端的串口调试助手,(同时拷贝此时的服务器客户端建立的socket)
然后执行相应的命令动作,并通过socket:send实现把命令再回显到客户端!

uart.on是串口数据接收监听函数,可以实现接收电脑端的串口调试发送的数据!
在这里面,利用服务器与客户端之间的socket,实现串口调试助手发送数据到8266串口,再利用socket:send发送到客户端!

具体实现还是看下面的wifi.lua.

init.lua

同上

  
 
  • 1

wifi.lua

wifi.setmode(wifi.STATIONAP)

cfg={}
cfg.ssid="Hellow8266"
cfg.pwd="11223344"
wifi.ap.config(cfg)

apcfg={}
apcfg.ssid="qqqqq"
apcfg.pwd="11223344"
wifi.sta.config(apcfg)
wifi.sta.autoconnect(1)

TCPSever=net.createServer(net.TCP,28800) 

TCPSever:listen(8080,function(socket) 
    socket:on("receive",function(socket,data) 
          uartsocket = socket
          uart.write(0,data) 
          
          if data == "++MD610" then
             gpio.write(2,1)
             socket:send("relay=1")
          end
          if data == "++MD600" then
             gpio.write(2,0)
             socket:send("relay=0")
          end
    end) 

    socket:on("disconnection",function(sck,c) 
          socket = nil
    end)
end)

uart.on("data",0,function(data) 

        if uartsocket ~= nil then
           uartsocket:send(data)
        end
        
end, 0))

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)

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
小感悟之串口,服务器,客户端之间的收发数据

看来,服务器向客户端发送消息就是通过socket:send
客户端向服务器则是直接发,只需要再服务器端进行接收监听即可
socket:on("receive",function(socket,data)...

同样,我们8266的串口和串口调试助手的通信也是如此呢!
8266的串口向串口调试助手发送消息通过print或者uart.write
串口调试助手向8266进行发送消息直接发即可,只需要注册串口数据监听即可
uart.on("data",0,function(data)

文章来源: recclay.blog.csdn.net,作者:ReCclay,版权归原作者所有,如需转载,请联系作者。

原文链接:recclay.blog.csdn.net/article/details/78172287

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200