-- Your Wifi connection data
local SSID = "xxxxxxxxxxxxxxx"
local SSID_PASSWORD = "xxxxxxxxxxxxxxx"
SENDER_PIN = 6
OUTLETS = {
{name="Test Steckdose 1", codeON=5506385, codeOFF=5506388, status="OFF"} --,
--{name="Test Steckdose 2", codeON=5506373, codeOFF=5506376, status="OFF"},
--etc
}
function wait_for_wifi_conn ( )
tmr.alarm (1, 1000, 1, function ( )
if wifi.sta.getip ( ) == nil then
print ("Waiting for Wifi connection")
else
tmr.stop (1)
print ("ESP8266 mode is: " .. wifi.getmode ( ))
print ("The module MAC address is: " .. wifi.ap.getmac ( ))
print ("Config done, IP is " .. wifi.sta.getip ( ))
end
end)
end
function setOutlet(index, status)
print(index,OUTLETS[index])
if OUTLETS[index] ~= nil then
print("ok")
if status == true then
rfswitch.send(1, 300, 5, SENDER_PIN, OUTLETS[index]["codeON"], 24)
OUTLETS[index]["status"] = "ON"
elseif status == false then
rfswitch.send(1, 300, 5, SENDER_PIN, OUTLETS[index]["codeOFF"], 24)
OUTLETS[index]["status"] = "OFF"
end
end
end
function connect (conn, data)
local data = ""
local function outletHTML(index, outlet)
local output = ""
output = output .. "
"
output = output .. ""
output = output .. ""
return output
end
conn:on ("receive",
function (cn, req_data)
if req_data:find('^POST / HTTP/1.1') ~= nil then
-- POST data
--print(req_data)
local l = req_data:match(".*()\r\n")
if l < string.len(req_data) then
local d = string.sub(req_data, l)
local nr, val = string.match(d, "(%d+)=(%d+)")
nr = tonumber(nr)
if nr ~= nil then
if val == "0" then
setOutlet(nr, false)
elseif val == "1" then
setOutlet(nr, true)
end
end
end
end
data = data .. ""
data = data .. ""
data = data .. ""
data = data .. "ESP8266 433MHz Funksteckdosen Steuerung"
data = data .. "
"
data = data .. "
"
for i, outlet in pairs(OUTLETS) do
data = data .. outletHTML(i, outlet)
end
data = data .. "
"
data = data .. "
"
data = data .. ""
--cn:send("HTTP/1.1 200 OK\r\nServer: NodeLuau\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n")
cn:send(data)
-- Close the connection for the request
cn:on("sent",function(cn) cn:close() end)
end)
end
-- Configure the ESP as a station (client)
wifi.setmode(wifi.STATION)
--wifi.sta.config(SSID, SSID_PASSWORD)
wifi.sta.config {ssid=SSID, pwd=SSID_PASSWORD}
wifi.sta.autoconnect(1)
-- Hang out until we get a wifi connection before the httpd server is started.
wait_for_wifi_conn()
-- Create the httpd server
svr = net.createServer(net.TCP, 30)
-- Server listening on port 80, call connect function if a request is received
svr:listen (80, connect)