Skip to content

Instantly share code, notes, and snippets.

@jimbolimbo3
Created May 30, 2016 19:20
Show Gist options
  • Save jimbolimbo3/4bddddd5094e92a4be9b875aee7cac7f to your computer and use it in GitHub Desktop.
Save jimbolimbo3/4bddddd5094e92a4be9b875aee7cac7f to your computer and use it in GitHub Desktop.
init.lua for 5v relay to 3v3 in NodeMCU
status=0
pin=4
gpio.mode(pin,gpio.OUTPUT)
--gpio.write(pin,gpio.HIGH) --useless
--create a webserver
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>";
--the button(s)
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 == "OFF1")then
gpio.write(pin, gpio.HIGH);
elseif(_GET.pin == "ON1")then
gpio.write(pin, gpio.LOW);
--I keep the code for the second button for two relay rack
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment