--http://deneb21.tistory.com/
--Configure relay ouutput pins, pins are floating and relay opto needs ground to be activated. So pins are kept high on startup.
Relay1 = 1
Relay2 = 2
Relay3 = 3
Relay4 = 4
gpio.mode(Relay1, gpio.OUTPUT)
gpio.write(Relay1, gpio.HIGH);
gpio.mode(Relay2, gpio.OUTPUT)
gpio.write(Relay2, gpio.HIGH);
gpio.mode(Relay3, gpio.OUTPUT)
gpio.write(Relay3, gpio.HIGH);
gpio.mode(Relay4, gpio.OUTPUT)
gpio.write(Relay4, gpio.HIGH);
print("internet relay standby...")
--Create server and send html data, process request from html for relay on/off.
srv=net.createServer(net.TCP)
srv:listen(8080,function(conn) --change port number if required. Provides flexibility when controlling through internet.
conn:on("receive", function(client,request)
local html_buffer = "";
local html_buffer1 = "";
local html_buffer2 = "";
local gpio1, gpio2, gpio3, gpio4
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
html_buffer = html_buffer.."<html><head><meta http-equiv=\"Content-Language\" content=\"en-us\"><meta http-equiv=\"Content-Type\" content=\"text/html;\">";
html_buffer = html_buffer.."</head><body><font size=5>Internet Relay1 Control (NodeMCU/Lua, ESP8266)</font></br>";
html_buffer1 = html_buffer1.."<a href=\"?pin=ON1\"><button style=\"width:300\">Relay1 ON</button></a><a href=\"?pin=OFF1\"><button style=\"width:300\">Relay1 OFF</button></a><br/>";
html_buffer1 = html_buffer1.."<a href=\"?pin=ON2\"><button style=\"width:300\">Relay2 ON</button></a><a href=\"?pin=OFF2\"><button style=\"width:300\">Relay2 OFF</button></a><br/>";
html_buffer1 = html_buffer1.."<a href=\"?pin=ON3\"><button style=\"width:300\">Relay3 ON</button></a><a href=\"?pin=OFF3\"><button style=\"width:300\">Relay3 OFF</button></a><br/>";
html_buffer1 = html_buffer1.."<a href=\"?pin=ON4\"><button style=\"width:300\">Relay4 ON</button></a><a href=\"?pin=OFF4\"><button style=\"width:300\">Relay4 OFF</button></a><br/>";
local _on,_off = "",""
if(_GET.pin == "ON1")then
gpio.write(Relay1, gpio.LOW);
elseif(_GET.pin == "OFF1")then
gpio.write(Relay1, gpio.HIGH);
elseif(_GET.pin == "ON2")then
gpio.write(Relay2, gpio.LOW);
elseif(_GET.pin == "OFF2")then
gpio.write(Relay2, gpio.HIGH);
elseif(_GET.pin == "ON3")then
gpio.write(Relay3, gpio.LOW);
elseif(_GET.pin == "OFF3")then
gpio.write(Relay3, gpio.HIGH);
elseif(_GET.pin == "ON4")then
gpio.write(Relay4, gpio.LOW);
elseif(_GET.pin == "OFF4")then
gpio.write(Relay4, gpio.HIGH);
end
-- Relay Status Read
if(gpio.read(1)==0) then gpio1 = "ON" else gpio1 = "OFF" end
if(gpio.read(2)==0) then gpio2 = "ON" else gpio2 = "OFF" end
if(gpio.read(3)==0) then gpio3 = "ON" else gpio3 = "OFF" end
if(gpio.read(4)==0) then gpio4 = "ON" else gpio4 = "OFF" end
-- Relay Status print
print("gpio1:"..gpio1)
print("gpio2:"..gpio2)
print("gpio3:"..gpio3)
print("gpio4:"..gpio4)
-- Relay Status print html
html_buffer2 = html_buffer2.."<br/>Relay1 : "..gpio1;
html_buffer2 = html_buffer2.."<br/>Relay2 : "..gpio2;
html_buffer2 = html_buffer2.."<br/>Relay3 : "..gpio3;
html_buffer2 = html_buffer2.."<br/>Relay4 : "..gpio4;
html_buffer2 = html_buffer2.."</body></html>";
--Buffer is sent in smaller chunks as due to limited memory ESP8266 cannot handle more than 1460 bytes of data.
client:send(html_buffer);
client:send(html_buffer1);
client:send(html_buffer2);
client:close();
collectgarbage();
end)
end)