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

User avatar
By Whirlynerd
#63869 I found (and modified slightly) a very simple web controlled relay script I'm using with a generic ESP8266-12E dev board.

http://www.ebay.com/itm/191607499973?_t ... EBIDX%3AIT

It was unflashed; I initially used NodeMCU Flasher and the default firmware (2015 something). I'm using ESPlorer as the IDE. The script worked although it took a few tries--don't know if the problem is the firmware or the IDE. Anyway I went through the NodeMCU official docs and found NodeMCU-build.com and the pyflasher. I requested a default module built and first tried to flash it with Flasher but that failed--it just spewed constant garbage to the serial port. Pyflasher worked with the new build (serial port is MUCH faster on new build) but the script doesn't work--not fully anyway. The ESP8266 is connecting to my network (I can ping it) but I can't pull up the web page (gives 'error connection reset'). No errors are reported when loading the script.

Code: Select allwifi.setmode(wifi.STATION)
wifi.sta.config("XXXXXXXXXX","XXXXXXXXXXX")
wifi.sta.autoconnect(1)
cfg = {
    ip="192.168.2.20",
    netmask="255.255.255.0",
    gateway="192.168.2.1"
  }
wifi.sta.setip(cfg)
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 Web Server</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)


Any ideas would be appreciated!