wifi.setmode(wifi.STATION)
wifi.sta.config("Your Wifi","Your Password")
wifi.sta.autoconnect(1);
print("Hacklab Power Switch v0.3 by Av")
names = {[83]='Button 1', [92]='Button 2', [112]='Button 3', [208]='Button 4'}
setpin = 0
transmits = 0
lastCmd = ""
bInit = {0,0,0,0,0,0,0,1,0,0,0,1}
bOn = {0,0,1,1,0}
bOff = {1,1,0,0,0}
gpio.mode(0, gpio.OUTPUT)
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+)=([^&]+)&*") do
_GET[k] = v
end
end
buf = buf.."
HackLab Power Switch
"
for k,v in pairs(names) do
buf = buf .. v .. ' on off
'
end
if (_GET.p ~= nil) then
sendBytes(tonumber(_GET.p), _GET.s)
lastCmd = "Outlet ID " .. _GET.p .. ": Switched " .. _GET.s .. ""
buf=buf..lastCmd
else
buf=buf.."last command: " .. lastCmd
end
client:send(buf);
client:close();
collectgarbage();
end)
end)
function sendBytes(bID, bState)
local write, dy = gpio.write, tmr.delay
write(0, gpio.HIGH)
dy(100000)
write(0, gpio.LOW)
dy(50000)
local cmd = {}
for i = 1, 12 do table.insert(cmd, bInit[i]) end
for i = 1, 8 do
table.insert(cmd, bit.band(bID, 128))
bID = bit.lshift(bID, 1);
end
if (bState == "on") then for i = 1, 5 do table.insert(cmd, bOn[i]) end end
if (bState == "off") then for i = 1, 5 do table.insert(cmd, bOff[i]) end end
for j=1, 10 do
for i=1, 25 do
if (cmd[i] == 0) then
write(0, 1)
dy(200)
write(0, 0)
dy(570)
else
write(0, 1)
dy(600)
write(0, 0)
dy(140)
end
end
dy(6000)
end
end