一台Esp8266客户端一台Esp8266服务器

时间:2016-06-21 13:11:09

标签: tcp server client esp8266 nodemcu

我使用一个Esp8266用lua做了服务器客户端应用程序。我想用两个Esp8266做到这一点。我想使用其中一个Esp8266是Server,另一个是Client。您可以在下面看到第一个代码用于从一个AP获取RSSI,第二个代码用于在服务器中写入这些RSSI。我怎样才能将这两个代码放在两个Esp8266中?

i=5
tmr.alarm(1,10000,1, function()
print(wifi.sta.getap(scan_cfg, 1, listap))
if i>1 then
print(i)
i=i-1
else
tmr.stop(1)
print("Timer Durdu")
end
end
)
function listap(t)
for bssid,v in pairs(t) do
    local ssid = string.match(v, "([^,]+)")
    l=string.format("%-10s",ssid)
    stringtoarray = {}
    index = 1
         for value in string.gmatch(v,"%w+") do
            stringtoarray [index] = value
            index = index + 1
           end
            print(l)
    print(stringtoarray[2])
           end
end
scan_cfg = {}
scan_cfg.ssid = "VSP250s"
scan_cfg.bssid = "00:09:df:8e:03:b4"
scan_cfg.channel = 0
scan_cfg.show_hidden = 1

第二段代码:

srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive", function(client,request)
    local buf = "";
    local _, _, method, path, vars = string.find(request, "([A-Z]+)  (.+)?(.+) HTTP");
    if(method == nil)then
        _, _, method, path = string.find(request, "([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

    buf = buf.."<!DOCTYPE html><html><div id='container'><font size='5'>"
    buf = buf..'<style>body{width:auto;height:auto;background-color:#ffffff;}'
    buf = buf..'.button {font-size: 20px;}</style>'

    buf = buf.."<head> <meta http-equiv='refresh' content=3> "
    buf = buf.."<p><h1>RSSI  meter<br> ESP8266</h1>";

    --buf = buf.."<p>Refresh           : <a href=\"?pin=REFRESH1\"><button class='button'>ON</button></a>&nbsp</p>";
    --buf = buf.."<p>Relay Switch      : <a href=\"?pin=ON1\"><button class='button'>ON</button></a>&emsp;&emsp;"
    --buf = buf.."<a href=\"?pin=OFF1\"><button class='button'>OFF</button></a><br>"


    buf = buf..'<B>Voltage   :<font color=red>'..string.format('%s',l)..' V</font></b><br>'
    buf = buf..'<B>Current           :<B><font color=blue>'..string.format('%g',stringtoarray[2])..' A</font></b><br>'
    --buf = buf..'<B>Power Consumption :<B><font color=DeepSkyBlue>'..'Not Available'..'</font></b><br><BR>'
    -- buf = buf..'<p>Function Button   :<B><font color=BlueViolet>'..button_status..'</font></b><br></p>';

    buf = buf..'</head>'

    buf = buf..'<br><br><details><summary><font color=red>BURAK IPEK</font><p>'
    buf = buf..'<summary><p>Vestel Electronics </p></details>'


    buf = buf.."</body></font></div></html>"

    client:send(buf);
    client:close();
    collectgarbage();
end)
end)

1 个答案:

答案 0 :(得分:0)

将每个代码放入lua文件中。包含init.lua和输入

dofile("client.lua");
dofile("server.lua");

为了方便起见,请编写方法。

祝你好运。