ESP_B too, at 192.168.1.237.
Both act as servers and execute a init.lua implementing two buttons (on/off) to switch their own blue little LED.
I'd like that switching ESP_A led from a webserver could automatically cause ESP_B led to switch too.
I've tried to init on ESP_A a server and a client at the same time: as a server it will respond as usual to browser commands, but as a client it would pass to ESP_B the corresponding switch request. But this doesn't work.
ESP_A CODE
-- MAC 18:fe:34:f2:02:d3
-- IP 192.168.1.236
-- ESP_A
wifi.setmode(wifi.STATION)
wifi.sta.config("xxxx","xxxx")
print(wifi.sta.getip())
led1 = 4
led2 = 8
gpio.mode(led1, gpio.OUTPUT)
gpio.mode(led2, gpio.OUTPUT)
cl = net.createConnection(net.TCP, false)
cl:connect(8889,"192.168.1.237")
srv=net.createServer(net.TCP)
srv:listen(8888,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
if(_GET.pin == "ON1")then
gpio.write(led1, gpio.LOW);
elseif(_GET.pin == "OFF1")then
gpio.write(led1, gpio.HIGH);
end
buf = buf..'HTTP/1.1 200 OK\n\n';
buf = buf..'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
buf = buf..'<html xmlns="http://www.w3.org/1999/xhtml">';
buf = buf..'<head><meta http-equiv="content-type" content="text/html; charset=utf-8"/>';
buf = buf..'<title>ESP_A | STS</title></head>';
buf = buf..'<body><h1>236 ESP_A Web Server</h1>';
buf = buf..'<p>GPIO 02 <a href="?pin=ON1"><button style="color:green;width:200px;height:200px">ON</button></a> <a href="?pin=OFF1"><button style="width:200px;height:200px">OFF</button></a>';
if(gpio.read(led1) == 0) then
buf = buf..' Blue LED on</p>';
cl:on("connection", function(cl)
cl:send("GET /?pin=ON1")
end)
else
buf = buf..' Blue LED off</p>';
cl:on("connection", function(cl)
cl:send("GET /?pin=OFF1")
end)
end
buf = buf..'<p>End of file</p></body></html>';
local _on,_off = "",""
client:send(buf);
cl:close();
client:close();
print('\nLED switched!');
collectgarbage();
end)
end)
ESP_B CODE:
-- MAC 18:fe:34:f2:04:46
-- IP 192.168.1.237
-- ESP_B
wifi.setmode(wifi.STATION)
wifi.sta.config("xxxx","xxxx")
print(wifi.sta.getip())
led1 = 4
led2 = 8
gpio.mode(led1, gpio.OUTPUT)
gpio.mode(led2, gpio.OUTPUT)
srv=net.createServer(net.TCP)
srv:listen(8889,function(conn)
conn:on("receive", function(client,request)
print(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
if(_GET.pin == "ON1")then
gpio.write(led1, gpio.LOW);
elseif(_GET.pin == "OFF1")then
gpio.write(led1, gpio.HIGH);
end
buf = buf..'HTTP/1.1 200 OK\n\n';
buf = buf..'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
buf = buf..'<html xmlns="http://www.w3.org/1999/xhtml">';
buf = buf..'<head><meta http-equiv="content-type" content="text/html; charset=utf-8"/>';
buf = buf..'<title>ESP_B | STS</title></head>';
buf = buf..'<body><h1>237 ESP_B Web Server</h1>';
buf = buf..'<p>GPIO 02 <a href="?pin=ON1"><button style="color:green;width:200px;height:200px">ON</button></a> <a href="?pin=OFF1"><button style="width:200px;height:200px">OFF</button></a>';
if(gpio.read(led1) == 0) then
buf = buf..' LED on</p>';
else buf = buf..' LED off</p>';
end
buf = buf..'<p>End of file</p></body></html>';
local _on,_off = "",""
client:send(buf);
client:close();
print('\nLED switched!')
collectgarbage();
end)
end)
Any help would be appreciated. Thank you very much.