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

User avatar
By startimeahmet
#60940 Hi,
I am working on a little project where I am trying to control the built-in led of 2 esp8266 modules over the web server. There are lots of examples of controlling the led of the module via web server but they are for just 1 module. I want to add an extra client and control that as well.
Modules are Lolin Nodemcu v3.
Below is the code for server.
Code: Select allwifi.setmode(wifi.STATIONAP)
wifi.sta.config("DesirdTT","ddesirdsifree")  --router to connect to, needs to be changed accordingly
wifi.ap.config({ssid="test",pwd="12345678"});

tmr.alarm(0, 1000, 1, function()
    if wifi.sta.getip() == nil then
        print("Connecting to router...")
    else
        print('Connected to router :)')
        print('IP: ',wifi.sta.getip())
        tmr.stop(0)
    end
end)

led = 4
gpio.mode(led, gpio.OUTPUT)

if srv~=nil then
  srv:close()
end

srv=net.createServer(net.TCP)

srv:listen(80,function(conn)
    conn:on("receive", function(client,request)
        local buf = "";
        local client1 = "";
        local _GET = {}
        local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
        --print(method);
        --print(path);
        --print(vars);
        --print(client);
        --print(request);                       
             
        if (method == "CLIENT")then
            client:send("you are detected by server");
        end
        if(method == nil)then -- when we just enter 192.168.1.1 (normal entry)
            _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
        end       
        if (vars ~= nil)then
            for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
                _GET[k] = v
            end
        end
        buf = buf.."<h1 style='color:red'> ESP8266 Web Server</h1>";
        buf = buf.."<h2><i> This page is in very early stage of the development</h2></i>";
        buf = buf.."<p>1. DEVICE LED <a href=\"?pin=ON1\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF1\"><button>OFF</button></a></p>";
        buf = buf.."<p>2. DEVICE LED <a href=\"?pin=ON2\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF2\"><button>OFF</button></a></p>";
        buf = buf.."<p>3. DEVICE LED <a href=\"?pin=ON3\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF3\"><button>OFF</button></a></p>";
        if(_GET.pin == "ON1")then
            gpio.write(led, gpio.LOW);
        elseif(_GET.pin == "OFF1")then
            gpio.write(led, gpio.HIGH);
        end                                         
        client:send(buf);
        client:close();
        collectgarbage();
    end)
end)


wifi.eventmon.register(wifi.eventmon.AP_STACONNECTED, function(T)
    print("\n\tAP - STATION CONNECTED".."\n\tMAC: "..T.MAC)
end)


And below is the code for the client.

Code: Select allprint("ESP8266 Client")
wifi.sta.disconnect()
wifi.setmode(wifi.STATION)
wifi.sta.config("test","12345678") -- connecting to server
wifi.sta.connect()
print("Looking for a connection")

led = 4
gpio.mode(led, gpio.OUTPUT)

tmr.alarm(1, 2000, tmr.ALARM_AUTO, function()
    if(wifi.sta.getip()~=nil) then
        tmr.stop(1)         
        print("Connected to test!")
        print("Client IP Address:",wifi.sta.getip())
       
        cl=net.createConnection(net.TCP, 0)
        cl:connect(80,"192.168.4.1")

        tmr.alarm(2, 2000, 1, function()
            cl:send("CLIENT /?pin=ON2 HTTP/1.1")                             
        end)
       
        cl:on("receive", function(conn, receivedData)
            print("Received Data from server: " .. receivedData)
        end)1
               
     else
        print("Connecting...")
     end
end)


I know some parts of the codes are not complete. What I didn't understand is how to control the client's led via clicking on the 'ON' and 'OFF' buttons in the web page. Web page is a client as well and I think what I want to do is to control a client via another client.
Can you guys tell me if what I am trying to do is possible or not? Or is there another method to do this like using a UDP server ? Or maybe there is a specific method to send a message to connected clients via their IP or MAC adress, that would be much easier.

Thanks in advance.
User avatar
By marcelstoer
#61059
startimeahmet wrote:I think what I want to do is to control a client via another client.


For us trying to help it'd be essential if you made up your mind first ;) How should we help if we don't know what you're after.

Looks like each of your LoLin modules runs the same code?
Each of them has two LEDs attached?
All of them should be controlled from a browser independently from the others?
User avatar
By rudy
#61064
what I want to do is to control a client via another client.


A client can't connect to a client on the ESP8266. Another method must be used. I think UDP is a good option. But remember that UDP is not guaranteed delivery. Good to do 3 sends of the same data or to build in your own acknowledgment routines. I would not worry about reliability at first.

Another option is to have one as a server and the other the client. If you have a server set on one device to connect with a browser, that device can connect as a client to the other device that is set up as a server.

There are a number of possibilities.