Stap 6: MAINS Power Switch Software
Macht schakelfunctie en webserver software zijn min of meer hetzelfde als in het vorige artikel over CBDB WIFI lichtnet schakelenwe verandert gewoon de gebruikte GPIO pin voor Triac opto opdracht pin uit outpin = 3 aan outpin = 1 goed aan onze nieuwe MPSM v2 forum configuratie.
Voor het programmeren van MPSMv2 bestuur en uploaden van het stuurprogramma en de software zullen we blijven gebruiken de LuaUploader zoals voorheen.
- Define gebruikte GPIO pin:
outpin=1 -- Select Triac command PIN - GPIO5 gpio.mode(outpin,gpio.OUTPUT) gpio.write(outpin,gpio.LOW)
function PwrSW(swstat,payload) gpio.mode(outpin,gpio.OUTPUT) newstat=string.sub(payload,swstat[2]+1,#payload) status = newstat if newstat=="ON" then gpio.write(outpin,gpio.HIGH) return end if newstat=="OFF" then gpio.write(outpin,gpio.LOW) return end end
function sendPage(conn) conn:send('HTTP/1.1 200 OK\n\n') conn:send('<!DOCTYPE HTML>') conn:send('<html>') conn:send('<head><meta content="text/html; charset=utf-8"><style>input{width: 100px; height: 100px;}</style>') conn:send('<title>ESP8266 - Power Switch Controller</title></head>') conn:send('<body><h1>Power Switch Controller</h1>') conn:send('Status: <b>') if (status == "ON") then conn:send('<B><font color=red>ON</font></B>') elseif (status == "OFF") then conn:send('<B><font color=green>OFF</font></B>') else conn:send(status) conn:send('%') end conn:send('</b><br /><br />') conn:send('<form action="/" method="POST">') if (status == "ON") then conn:send('<input type="submit" style="background-color:red" name="cmd1" value="OFF"/><br /><br />') elseif (status == "OFF") then conn:send('<input style="background-color:green" type="submit" name="cmd1" value="ON"/>') end conn:send('</form>') conn:send('</body></html>') end
srv=net.createServer(net.TCP) srv:listen(80,function(conn) conn:on("receive", function(conn,payload) --next row is for debugging output only --print(payload) if (string.find(payload, "GET / HTTP/1.1") ~= nil) then print("GET received") sendPage(conn) else swstat={string.find(payload,"cmd1=")} --If POST value exist, set LED power if swstat[2]~=nil then print("Command received: " .. payload) PwrSW(swstat,payload) sendPage(conn) end end end) conn:on("sent", function(conn) conn:close() print("Connection closed") end) end)
Sla de programmacode op als 'web_switch.lua' ESP, herstart ESP en uitvoeren:
=wifi.sta.getip() -- find the IP Address where your new Web Server will be dofile("web_switch.lua") -- Start the Web Server
Open uw favoriete webbrowser en typ uw nieuwe Web Server-IP-adres. Allemaal ok, moet kijken iets als hieronder:
Als u wilt dat de software van het Web / uit-schakelaar automatisch wordt gestart wanneer je MPCMv2 Board wordt gestart of opnieuw wordt opgestart, vervolgens u neet maken en toevoegen sommige lijnen in het bestand 'init.lua':
tmr.now() -- for debug only, you can skip it wifi.sta.getmac() -- for debug only, you can skip it wifi.sta.getip() -- for debug only, you can skip itnode.heap() dofile("web_switch.lua") -- needed to start Web Server for command input
Sla de code op ESP als 'init.lua', start opnieuw op ESP. Het moet opnieuw opstarten en automatisch het programma opnieuw start en initialiseren van de webserver: