NodeMCU之WIFI模式

关于NodeMCU 几种WIFI模式的一些尝试

主要实现了三个功能。

一、station模式(用NodeMCU连接WiFi并实现与设备的简单通信)

这个功能主要分为三部分来实现:
1)连接WiFi并打开uart串口;
2)通过监听函数监听端口信息并打印;
3)通过发送函数来发送串口命令行输入的字符。

station_cfg = {}
station_cfg.ssid = "iPhone"
station_cfg.pwd = "12345678"
station_cfg.save = true

pin=4

print(wifi.sta.getip())               --nil
      wifi.setmode(wifi.STATION)
      wifi.sta.config(station_cfg)    --wifi.sta.connect()
      tmr.alarm(1, 1000, 1, function()
            if wifi.sta.getip() == nil then
                 print("Connecting...")
            else
                 tmr.stop(1)
                 print("Connected, IP is "..wifi.sta.getip())
            end
      end)
      uart.setup(0,115200,8,uart.PARITY_NONE,uart.STOPBITS_1,0)
     
      sv=net.createServer(net.TCP, 2880)
      print("creat server")
      global_c = nil
      sv:listen(8080, function(c)
            if global_c~=nil then
                 global_c:close()
            end
            global_c=c
            c:on("receive",function(sck,pl) uart.write(0,pl) end)
      end)
   
      uart.on("data",4, function(data)
            if global_c~=nil then
                 global_c:send(data)
            end
      end, 0)

二、AP模式(以NodeMCU创建局域网,设备连接后通过网络调试助手控制小灯亮灭)

LinkStat = 0      
TCPClient = nil   
ClientReceData = ""
ClientReceCnt = 0
ClientReceDataStat = true

wifi.setmode(wifi.SOFTAP)
local apcfg={
    ssid="hanhan",      
    pwd="123456789"  
}
wifi.ap.config(apcfg)
print("IP Adress is:"..wifi.ap.getip())

tmr.alarm(1,1000,1,function()
    if LinkStat == 0 then
        TCPClient = net.createConnection(net.TCP,0)  
        TCPClient:connect(8080,"192.168.4.2") 
          
        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)
            TCPClient:on("disconnection", function(sck, c)
                LinkStat = 0
                print("Dwifi.ap.getip()wifi.ap.getip()isconnection")
                tmr.start(1)
            end)
       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(pin,gpio.HIGH)
           end
           
           if ClientReceData=="close" then
                gpio.write(pin,gpio.LOW)
           end
           
           uart.write(0,ClientReceData)
           ClientReceData = ""
           ClientReceCnt = 0
           ClientReceDataStat = false
       end
   end
end)

三、station模式应用(用网页控制单片机小灯亮灭)

station_cfg = {}
station_cfg.ssid = "jietijiaoshi1"
station_cfg.pwd = "12345678"
station_cfg.save = true

print(wifi.sta.getip())
    wifi.setmode(wifi.STATION)
    wifi.sta.config(station_cfg)

    tmr.alarm(1, 1000, 1, function()
        if wifi.sta.getip() == nil then
            print("Connecting...")
        else
            tmr.stop(1)
            print("Connected, IP is "..wifi.sta.getip())
        end
    end)

    srv=net.createServer(net.TCP,2880)
    srv:listen(80,function(conn)
        conn:on("receive",function(conn,payload)
            print("Heap = "..node.heap().." Bytes")
            print("Print payload:\n"..payload)

            local _, _, method, path, vars = string.find(payload, "([A-Z]+) (.+)?(.+) HTTP");
            if(method == nil)then
                _, _, method, path = string.find(payload, "([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

            para = ""
            if(_GET.pin == "ON4")then
                gpio.write(pin, gpio.HIGH)
                para=para.."

open

" elseif(_GET.pin == "OFF4")then gpio.write(pin, gpio.LOW) para=para.."

off

" end head = "ESP8266 Webserver" body = "" ending = "" reply1 = head..body reply2 = para..ending payloadLen = string.len(reply1) + string.len(reply2) conn:send("HTTP/1.1 200 OK\r\n") conn:send("Content-Length:" .. tostring(payloadLen) .. "\r\n") conn:send("Connection:close\r\n\r\n") conn:send(reply1) conn:send(reply2) collectgarbage() end) conn:on("sent",function(conn) conn:close() end) end)

时间仓促,暂时先把代码贴上,具体的抽空再码上把。。。

学摄像头去也。

你可能感兴趣的:(NodeMCU之WIFI模式)