------------Configuration Field----- WIFI_SSD = "SSD" WIFI_PASS = "PASSWORD" relay_pin = 6 -- the pin of the switch blink_open = "https://s28.postimg.org/xd1frdn19/image.jpg" -- open image blink_close = "https://s27.postimg.org/57xuqg477/off.jpg" -- close image state = "Light Off" externalIP = 0 ------------------------------------ -- each 10 secound check connection tmr.alarm(1, 10000, 1, function() if wifi.sta.getip() == nil then print("Problem accure, reconnectiong...") wifi.setmode(wifi.STATION) wifi.sta.config(WIFI_SSD,WIFI_PASS) -- Replace these two args with your own network else print("Connected, IP is "..wifi.sta.getip()) http.get("http://api.ipify.org/", nil, function(code, data) if (code < 0) then print("HTTP request failed") else for token in string.gmatch(data, "[^%s]+") do externalIP = token end print(externalIP) http.get("http://your_host_url/setHomeExternalIP.php?token=a1a1a1a1&ip=http://"..externalIP.."", nil, function(code, data) if (code < 0) then print("HTTP request failed") else print(data) end end) end end) end end) ---------------- -- GPIO Setup -- ---------------- gpio.mode(relay_pin, gpio.OUTPUT) site_image = blink_close gpio.write(relay_pin,gpio.HIGH) ---------------- -- Web Server -- ---------------- print("Starting Web Server...") -- Create a server object with 30 second timeout srv = net.createServer(net.TCP, 30) -- server listen on 80, -- if data received, print data to console, -- then serve up a sweet little website srv:listen(80,function(conn) conn:on("receive", function(conn, payload) function esp_update() mcu_do=string.sub(payload,postparse[2]+1,#payload) if mcu_do == "TURN+ON" then site_image = blink_open print("TURN ON") gpio.write(relay_pin,gpio.LOW) state = "Light On" end if mcu_do == "TURN+OFF" then print("TURN OFF") site_image = blink_close gpio.write(relay_pin,gpio.HIGH) state = "Light Off" end end --parse position POST value from header postparse={string.find(payload,"mcu_do=")} --If POST value exist, set LED power if postparse[2]~=nil then esp_update()end -- CREATE WEBSITE -- -- HTML Header Stuff conn:send('HTTP/1.1 200 OK\n\n') conn:send('\n') conn:send('\n') conn:send('\n') conn:send('Gilboa 42A Light yard\n') conn:send('

Turn ON/OFF Light

\n') conn:send('

\n') -- Buttons conn:send('
\n') conn:send('   \n') conn:send('\n') conn:send('\n

Current state is : '..state..'') conn:send('\n
The Relay is connected to pin : '..relay_pin..'') conn:send('
\n') conn:on("sent", function(conn) conn:close() end) end) end)