Skip to content

Instantly share code, notes, and snippets.

@kovachwt
Created January 14, 2016 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kovachwt/93513c7edf7500927bb6 to your computer and use it in GitHub Desktop.
Save kovachwt/93513c7edf7500927bb6 to your computer and use it in GitHub Desktop.
LUA code for web controlled 3-phase power socket
pin=2
ssid="yourSSID"
password="yourPass"
serverip = "192.168.1.2"
serverurl = "/termopechka.aspx"
-- set safe low signal
gpio.mode(pin, gpio.OUTPUT)
gpio.write(pin, gpio.LOW)
wifi.setmode(wifi.STATION)
wifi.sta.config(ssid , password)
wifi.sta.autoconnect(1)
print("IP: ")
print(wifi.sta.getip())
connecting = 0
previouspin = "0"
function dojob()
print("Connecting...")
print(tmr.now())
--print("IP: ")
--print(wifi.sta.getip())
connecting = 1
conn = net.createConnection(net.TCP, 0)
conn:on("receive", function(sck, payload)
print("Got response!")
print(tmr.now())
--print(payload)
value = "0"
vstart, vend = string.find(payload, "OFFONVAL=");
if vstart and vend then
mstart, mend = string.find(payload, "$", vend + 1);
end
if mstart and mend then
value=string.sub(payload, vend + 1, mstart - 2)
end
if previouspin ~= value then
if value == "1" then
print("Turning ON!")
gpio.write(pin, gpio.HIGH)
else
print("Turning OFF!")
gpio.write(pin, gpio.LOW)
end
previouspin = value
end
connecting = 0
tmr.delay(3000000)
tmr.alarm(1, 5000, 1, function()
reconnect()
end)
end)
conn:on("disconnection", function(sck, payload)
connecting = 0
print("Connection lost, reconnecting...")
tmr.alarm(1, 5000, 1, function()
reconnect()
end)
end)
conn:on("connection", function(sck, payload)
request = "GET "..serverurl.." HTTP/1.1\r\nHost: "..serverip.."\r\n"
.."Accept: */*\r\n\r\n"
conn:send(request)
end)
conn:connect(80, serverip)
end
function reconnect()
if wifi.sta.status() == 5 and wifi.sta.getip() ~= nil then
if (connecting == 0) then
tmr.stop(1)
dojob()
end
else
print("Waiting for Wifi...")
end
end
tmr.alarm(1, 5000, 1, function()
reconnect()
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment