2016-06-21 97 views
-1

我用lua使用一个Esp8266做了服务器 - 客户端应用程序。我想用两个Esp8266做这个。我想使用其中一个Esp8266是服务器,另一个是客户端。您可以在下面看到第一个代码,用于从一个AP获取RSSI,第二个代码用于将这些RSSI写入服务器。我如何将这两个代码放在两个Esp8266中?一个Esp8266客户端一个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) 
+0

您可以尝试micropython fireware。它有一个webrepl(read-eval-print循环),你可以通过webrepl发送和接收来自esp8266的数据。 –

+0

你有两个esp8266,并希望在他们每个人上执行一个不同的程序。这应该是微不足道的,对吧?只需将每个程序上传到相应的esp8266即可。你不会写出你所面临的妨碍你这样做的障碍,或者为什么有人想要阅读你的代码。 –

回答

0

将每个代码放到一个Lua文件。包括从init.lua这两个打字

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

为了使事情更容易,写入方法。

祝你好运。

相关问题