-1

I did Server-Client Application with lua using one Esp8266. I wanna do this with two Esp8266. I wanna use one of these Esp8266 is Server and the other other one is Client. You can see below first code using for get RSSI from one AP and second code is using for writing these RSSI in a Server. How can i placed these two codes in two 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

Second code:

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)
ahmd14
  • 61
  • 2
  • 8
  • You can try micropython fireware. It has a webrepl(read-eval-print loop), you can send and receive data from esp8266 via webrepl. – Deng diliang Feb 04 '17 at 13:11
  • You have two esp8266 and want to execute a different program on each of them. This should be trivial, right? Just upload each program to the respective esp8266. You do not write what obstacles you face that prevent you from doing so, or why anyone would want to read your code. – Ludwig Schulze Feb 21 '17 at 19:46

1 Answers1

1

Put each code into a lua file. Include both from init.lua with typing

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

To make things easier, write methods.

Good luck.

Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
cagdas
  • 1,634
  • 2
  • 14
  • 27