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

User avatar
By ab88
#69830 hi
im sorry for my poor english

i flashed nodemcu 2.1 firmware on my device and change code like below for new api and nodemcu connect to my modem but when i write ip address html page did not loaded.

0.9.5 firmware not have problem.
changed block for new api:
Code: Select allcfg={}
cfg.ssid="my ssid"
cfg.pwd="my password"
wifi.sta.config(cfg)
  wifi.sta.connect()


main code:
Code: Select allwifi.setmode(wifi.STATION)   
--wifi.sta.config("my ssid","my password) removed this line

cfg={}
cfg.ssid="my ssid"
cfg.pwd="my password"
wifi.sta.config(cfg) 

print(wifi.sta.getip())
led1 = 3
led2 = 4
gpio.mode(led1, gpio.OUTPUT)
gpio.mode(led2, 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> ESP8266 WeServer</h1>";
        buf = buf.."<p>GPIO0 <a href=\"?pin=ON1\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF1\"><button>OFF</button></a></p>";
        buf = buf.."<p>GPIO2 <a href=\"?pin=ON2\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF2\"><button>OFF</button></a></p>";
        local _on,_off = "",""
        if(_GET.pin == "ON1")then
              gpio.write(led1, gpio.HIGH);
        elseif(_GET.pin == "OFF1")then
              gpio.write(led1, gpio.LOW);
        elseif(_GET.pin == "ON2")then
              gpio.write(led2, gpio.HIGH);
        elseif(_GET.pin == "OFF2")then
              gpio.write(led2, gpio.LOW);
        end
        client:send(buf);
        client:close();
        collectgarbage();
    end)
end)


source:
http://randomnerdtutorials.com/esp8266-web-server/