Esp-8266 door lock code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
/* based on example code from Nodemcu -- changed by Roee Bloch to fit my needings -- GPIOS 1 -> Output to relay -- GPIO 2 -> Door status -- GPIO3 -> Relay feedback to check if command is ok */ wifi.setmode(wifi.STATION); wifi.sta.config("your-wifi-name","your-wifi-password"); wifi.sta.setip({ip="192.168.1.100",netmask="255.255.255.0",gateway="192.168.1.1"}); -- this is for Static fixed IP address gpio.mode(1, gpio.OUTPUT) gpio.mode(2, gpio.INPUT) gpio.mode(3, gpio.INPUT) srv=net.createServer(net.TCP) srv:listen(80,function(conn) conn:on("receive", function(client,request) local buf = ""; local state =""; local mystate =""; local mess =""; local feedback_message =""; local feedback; local feedback_mess2=""; local pass=""; local pass_message=""; local my_pass="1234"; -- put here a better password :) 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> Hello, Roee Bloch Door locker.</h1><form src=\"/\">Password: <input type=\"password\" name=\"password\"><br/>Lock the door <select name=\"pin\" onchange=\"form.submit()\">"; local _on,_off = "","" if (_GET.password ==my_pass) then pass_message="<br> Password is OK"; if(_GET.pin == "Locked") then _on = " selected=true"; gpio.write(1, gpio.HIGH); elseif(_GET.pin == "Unlocked") then _off = " selected=\"true\""; gpio.write(1, gpio.LOW); end else pass_message="<br> Password is WRONG!"; end buf = buf.."<option".._on..">Locked</opton><option".._off..">Unlocked</option></select></form>"; state = state.."Door state is: "; mystate =gpio.read(2); feedback = gpio.read(3); feedback_message ="<br>Command to Relay is :"; if (mystate==1) then mess="Door is closed"; elseif (mystate==0) then mess="Door is open!!!!!!"; end if (feedback==1) then feedback_mess2="Lock <br>"; elseif (feedback==0) then feedback_mess2="UN-Lock <br>"; end client:send(buf); client:send(state); client:send(mess); client:send(feedback_message); client:send(feedback_mess2); client:send(pass_message); client:close(); collectgarbage(); end) end) |
download NODMCU here: NodMCU
download ESPLORER here: Esplorer
You can buy development board here on ebay: esp-8266-development
you can buy the StandAlone board on ebay here: esp-8266-Stand-Alone board
Arduino Isolated Relay for connecting with ESP8266: Arduino Isolated Relay x 2