So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By Raihaan
#72689 --I am working on NodeMCU--

Hi guys,
On every online example I've seen, I have only seen people manage to setup one ESP as STATION and AP and creating a server on it, then making the other ESP just a STATION listening for the server, then posting data to the server on 192.168.4.1. I have this working.

The problem is, I want to create a HUB that is configured to STATIONAP and has the web server. I want the SERVER to post the incoming data to the client ESP listening in. So far, the only idea I've had is setting up a second server on the listening ESP with the IP being 192.168.4.2, and that has some partial success except my code is a little badly behaved:

Server Code, Works as Expected

Code: Select allprint("Server!!!")
wifi.setmode(wifi.STATIONAP)
wifi.ap.config({ssid="test",pwd="12345678"})
gpio.mode(4, gpio.OUTPUT)
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.."<h1> Bonjouurrrr evvverybodyyyy</h1><form src=\"/\">Turn LED <select name=\"pin\" onchange=\"form.submit()\">"
        local _on,_off = "",""
        print("Good...")
        if(_GET.pin == "ON")then
          _on = " selected=true"       
          cl=net.createConnection(net.TCP, 0)
          cl:connect(80,"192.168.4.2")
          tmr.alarm(2, 1000, 1, function()
            cl:send("HIGH!")
            print("HIGH!")
          end)
              gpio.write(4, gpio.HIGH)
       
        elseif(_GET.pin == "OFF")then
          _off = " selected=\"true\""       
          cl=net.createConnection(net.TCP, 0)
          cl:connect(80,"192.168.4.2")
          tmr.alarm(2, 5000, 1, function()
            cl:send("LOW!")
            print("LOW!")
          end)
              gpio.write(4, gpio.LOW)
        end
        buf = buf.."<option".._on..">ON</opton><option".._off..">OFF</option></select></form>"
        client:send(buf)
    end)
    conn:on("sent", function (c) c:close() end)
end)



As you can see, I am just trying to activate an LED. Both gpio2 pins on both ESPs need to turn on, and so far only the server works perfectly. Here is the faulty client script:

Client Code

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")
gpio.mode(4,gpio.OUTPUT)

tmr.alarm(0, 2000, 1, function()
     if(wifi.sta.getip()~=nil) then
          tmr.stop(1)
          print("Connected!")
          hi()
          print("Client IP Address:",wifi.sta.getip())         
         
      else
         print("Connecting...")
      end
end)

function hi()
       sv = net.createServer(net.TCP)
       x = 1 

       tmr.alarm(1, 500, 1, function ()
          print("Yay!")
          sv:listen(80, function(conn)
            conn:on("receive", function(conn, receivedData)
                print("Received Data: " .. receivedData)
                if(receivedData == "HIGH!") then
                    gpio.write(4,gpio.HIGH)
                    print("high")
         
                elseif(receivedData == "LOW!") then
                    gpio.write(4,gpio.LOW)
                    print("low")
                end         
            end)
            conn:on("sent", function(conn)
                collectgarbage()
            end)
          end)
          tmr.delay(5000)
       end)
     
       end     


And as you can tell, it successfully starts a server on the first ESPs network, but goes into a loop that crashes after about 3 seconds... I inserted a tmr.delay(1000) at the end of hi() but it seems to have no effect. If I add a second timer, then the code doesn't seem to progress past the first line... Can you take a look at both and see where I've gone wrong? My eventual goal is to connect many ESPs to the network made by the Hub ESP that talks to the internet (access it at http://hydruino.duckdns.org).

Thanks a lot!! :mrgreen: