Intro
LUA Source Code
Read moreSome time ago I lost my gate's remote control. Didn't have the time or inclination to buy a new remote control, I decided to replace it with an simple and low cost electronic system based on a ESP8266 and a relay.
General principlesESP is connected to my wifi network with a fixed IP address and executes an http server. For each specific http request, ESP emits a 500 ms pulse to activate the relay. This relay is connected to the appropriate inputs on my gate automation.
ESP has been flashed with nodemcu firmwares modules :
- GPIO
- Net
- Node
- TMR
- WiFi
1 / 4
-------------------------------
-- wifi client config
wifi.setmode(wifi.STATION);
cfg={}
cfg.ssid="SSID"
cfg.pwd="PASSWORD"
cfg.ip="192.168.1.40"
cfg.netmask="255.255.255.0"
cfg.gateway="192.168.1.0"
wifi.sta.setip(cfg)
wifi.sta.connect()
-------------------------------
-------------------------------
-- GPIO config
relay_pin = 1
gpio.mode(relay_pin, gpio.OUTPUT)
-------------------------------
-------------------------------
-- TCP Server initialisation
srv=net.createServer(net.TCP)
--print("Server HTTP started")
srv:listen(80,function(conn)
conn:on("receive", function(client,request)
--print(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.."<a style='text-decoration: none;' href=\"?relay=ACTION\"><table width=100% height=50% border=1>"
buf = buf.."<tr>"
buf = buf.."<td style='text-align: center;'>"
buf = buf.."<h1>ACTION</h1>"
buf = buf.."</td>"
buf = buf.."</tr>"
buf = buf.."</table></a>"
local _on,_off = "",""
if(_GET.relay == "ACTION")then
gpio.write(relay_pin, gpio.LOW);
tmr.delay(500000) -- 500 ms
gpio.write(relay_pin, gpio.HIGH);
end
client:send(buf);
client:close();
collectgarbage();
end)
end)
Test- Take your smartphone and verify that you are connected to your wifi network
- Go to the URL : http://192.168.1.40
- Click on the action button
- Verify that your relay is activated
Comments
Please log in or sign up to comment.