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

User avatar
By dnc40085
#17702 If they are all on the same AP you should be able to communicate between stations with no problem, provided you know which station has what IP.
As for a method of communication, you can use net.createserver(receive data) and net.createconnection(transmit data) for communication between stations.
User avatar
By ucy74
#17729
dnc40085 wrote:If they are all on the same AP ... you can use net.createserver(receive data) and net.createconnection(transmit data) for communication between stations.

It works when client is on AP and serwer is on STATION.
Not if both are only on STATIONs, and AP is pure.

AP: client
Code: Select allport=5000

gpio.mode(3,gpio.INPUT)

sk=net.createConnection(net.UDP,0)
sk:on("receive",function(sk,pl) print('INPT:',pl) end)
sk:connect(port,"192.168.4.66")

tmr.alarm(0,5000,1,function()

    if gpio.read(3)==1 then
      pl="on"
    else
      pl="off"
    end

    sk:send(pl)
    print('OUTPUT:',pl)

end)

STATION: server
Code: Select allsrv=net.createServer(net.UDP)
srv:on("receive", function(srv, pl)

     print(' INPUT:', pl)

end)

srv:listen(port)
User avatar
By dnc40085
#17813 Initially, I was suggesting that you run client/server on each station (after some experimentation I found that concurrently running client and server is impossible, but that issue needs it's own thread)

After some testing I found that, when connecting two ESPs to my xfinity modem/router, the first communication from ESP #1(Station Mode) to ESP #2(Station Mode) was quick then the second one started getting lag, the third was generally hit-and-miss.

Next I tried using a third esp as the access point and it would say that it connected but no data was transferred from client to server, eventually the connection would time out and disconnect.

Maybe this is a bug, it might be in the NodeMCU source or the SDK, I have no idea.

Here is the code I tested with:

ESP #1:
Code: Select allprint("init will begin in 3 seconds")

function client()
    Client9001=net.createConnection(net.TCP, 0)
    Client9001:on("connection", function()
        print("connected")
        Client9001:send("HELLO SERVER!")
         
    end)
       
    Client9001:on("receive", function(sck, c)   
        tmr.stop(3)
        print(c)
        if string.find(c, "Hello Client") then
            Client9001:send("I'm going to disconnect now")
        end
        if string.find(c, "disconnect_ack") then
            Client9001:close()
        end
    end)
    Client9001:on("disconnection", function()
        print("disconnected")
        Client9001=nil
    end)
    Client9001:connect(9000,"10.0.0.200")
end


tmr.alarm(0, 3000, 0, function()
    print("init.lua START")
    wifi.setmode(1)
    wifi.sta.config ("MYSSID","MYPASSWORD")
    tmr.alarm(0, 10, 1, function()
        local Conn_status=wifi.sta.status()
        if (Conn_status==5) then
            cfg={ip="10.0.0.100", netmask="255.255.255.0", gateway="10.0.0.1"}
            wifi.sta.setip(cfg)
            tmr.stop(0)
            print("CONNECTED TO AP")
            print("Starting tcp server on port 9001")
            server = net.createServer(net.TCP,15)
            server:listen(8000, function(conn)
                conn:on("receive",function(conn,data)
                    print(data)
                    if string.find(data, "HELLO SERVER") then
                        conn:send("Hello Client")
                    end
                    if string.find(data, "I'm going to disconnect now") then
                        conn:send("disconnect_ack")
                    end
                end)
                conn:on("disconnection", function() print("disconnected"); end)
               
            end)
            print("Server started")


        end
    end)
    print("init.lua END")
end)
if false then
    client()
    server:close()
end



ESP #2:
Code: Select allprint("init will begin in 3 seconds")

function client()
    Client9001=net.createConnection(net.TCP, 0)
    Client9001:on("connection", function()
        print("connected")
        Client9001:send("HELLO SERVER!")
         
    end)
       
    Client9001:on("receive", function(sck, c)   
        tmr.stop(3)
        print(c)
        if string.find(c, "Hello Client") then
            Client9001:send("I'm going to disconnect now")
        end
        if string.find(c, "disconnect_ack") then
            Client9001:close()
        end
    end)
    Client9001:on("disconnection", function()
        print("disconnected")
        Client9001=nil
    end)
    Client9001:connect(8000,"10.0.0.100")
end


tmr.alarm(0, 3000, 0, function()
    print("init.lua START")
    wifi.setmode(1)
    wifi.sta.config ("MYSSID","MYPASSWORD")
    tmr.alarm(0, 10, 1, function()
        local Conn_status=wifi.sta.status()
        if (Conn_status==5) then
            cfg={ip="10.0.0.200", netmask="255.255.255.0", gateway="10.0.0.1"}
            wifi.sta.setip(cfg)
            print("CONNECTED TO AP")
            print("Starting tcp server on port 9001")
            server = net.createServer(net.TCP,15)
            server:listen(9000, function(conn)
                conn:on("receive",function(conn,data)
                    print(data)
                    if string.find(data, "HELLO SERVER") then
                        conn:send("Hello Client")
                    end
                    if string.find(data, "I'm going to disconnect now") then
                        conn:send("disconnect_ack")
                    end
                end)
                conn:on("disconnection", function() print("disconnected"); end)
               
            end)
            tmr.stop(0)
            print("Server started")
        end
    end)
    print("init.lua END")
end)
if false then
    tmr.alarm(0, 5000, 1, client)
    client()
    server:close()
end