As the title says... Chat on...

User avatar
By Norber
#39673 ESP_A is successfully connected to wifi at home at 192.168.1.236.
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

Code: Select all-- 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>&nbsp;<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:

Code: Select all-- 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>&nbsp;<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.
User avatar
By Norber
#39755 I've made some changes and now the code works this way:

ESP_A is connected to home wifi at 192.168.1.236:8888
ESP_B is connected to home wifi at 192.168.1.237:8889

Both implement a pair of buttons (ON, OFF) to switch their respective little blue LEDs, each its own, from any web browser (including iPhones). But when the user switchs ESP_A LED, the ESP_B mimics the action so its LED is switched also. That is: ESP_A is acting as a server when it receives the switch order from the web, but as a client when it sends the mimic order to ESP_B. Both, nevertheless, are configured as STA and are linked to the home wifi, from where they get their fixed IP address.

The key question was not to mix a server with a client in software. I mean: you should start a server connection, you can use it and close it, and then (and only then) open a client connection and do whatever is needed. Close it at the end too.

I'm not sure if these cautions are essential or not. But they made the main difference between yesterday's code and today's, which follows.


ESP_A:

Code: Select all-- MAC 18:fe:34:f2:02:d3
-- Needs 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)
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
        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>236 ESP_A | stsproyectos.com | STS</title></head>';
        buf = buf..'<body><h1>236  ESP_A Web Server</h1>';
        buf = buf..'<p>ESP_A 236  __  GPIO 02 <a href="?pin=ON1"><button style="width:200px;height:200px">ON</button></a>&nbsp;<a href="?pin=OFF1"><button style="width:200px;height:200px">OFF</button></a>';
        if(_GET.pin == "ON1")then
              gpio.write(led1, gpio.LOW);
              buf = buf..' LED on</p>'
        elseif(_GET.pin == "OFF1")then
              gpio.write(led1, gpio.HIGH);
              buf = buf..' LED off</p>'
        end   
        buf = buf..'<p>End of file</p></body></html>';
        local _on,_off = "",""
        client:send(buf);
        client:close();
        collectgarbage();
        cl = net.createConnection(net.TCP, 0);
        cl:connect(8889,"192.168.1.237");
        cl:on("connection", function(a,b)
        if(gpio.read(led1) == 0) then
            cl:send("GET /?pin=ON1 HTTP/1.1\r\nHost: 192.168.1.237:8889\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
            else cl:send("GET /?pin=OFF1 HTTP/1.1\r\nHost: 192.168.1.237:8889\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
        end
        cl:close();
        end);
    collectgarbage(); 
  end);
end);



ESP_B:

Code: Select all-- MAC 18:fe:34:f2:04:46
-- Needs 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 | stsproyectos.com | STS</title></head>';
        buf = buf..'<body><h1>237  ESP_B Web Server</h1>';
        buf = buf..'<p>ESP_A 237  __  GPIO 02 <a href="?pin=ON1"><button style="color:green;width:200px;height:200px">ON</button></a>&nbsp;<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>Campo de numeros: <input id="C1" type="text" size="10" value="255.38"></p>';
        buf = buf..'<p>End of file</p></body></html>';
        local _on,_off = "",""
        client:send(buf);
        client:close();
        collectgarbage();
    end)
end)