-- Note: extra space added at end of name local SSID = "YourSSID" local password = "YourPassword" local serverAddress = nil local waitTime = 10000 local waterDetected = 0 local listening = 0 -- D4 = GPIO2 myInputPin = 4 gpio.mode(myInputPin, gpio.INPUT) -- Use the internal pull up resistor gpio.write(myInputPin,gpio.HIGH) -- Format of command: sensorName command param function startListening() port = 3333 myName = "temp1" srv=net.createServer(net.UDP) srv:on("receive", function(connection, pl) print("Command Received "..pl) len = string.len(myName) -- Get server address if string.sub (pl,0,6) == "server" then print ("Server address received.") serverAddress = string.sub (pl, 8) print ("Address:"..serverAddress.."." ) end end) srv:listen(port) listening = 1 end function sendData() print("Sending data to "..serverAddress..".") sk=net.createConnection(net.TCP, 0) conn=net.createConnection(net.TCP, 0) conn:on("receive", function(conn, payload) print(payload) end) print ("Connect") conn:connect(80,serverAddress) MAC=wifi.sta.getmac() msg = "GET /Paulware/updateSensor.php?MAC="..MAC.."&value="..waterDetected.." HTTP/1.1\r\n" print (msg) conn:send(msg) conn:send("Host: "..serverAddress.."\r\n") conn:send("Accept: */*\r\n") conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n") conn:send("\r\n") conn:on("sent",function(conn) print("Data Sent!") conn:close() end) conn:on("disconnection", function(conn) print("Got disconnection...") end) end function readSensor() valueDetected = gpio.read (myInputPin) end function getReady() ip = wifi.sta.getip() if (ip == nil) then wifi.sta.config(SSID, password) print("v1.1, Connecting to AP...") else if (serverAddress ==nil) then print "Waiting until server Address is known" else print ("Continue reading sensor, my address is:"..ip..".") readSensor() if (valueDetected == 1) then print ("Value detected") tmr.stop(1) tmr.alarm (1,waitTime,1,getReady) else print ("No Value detected") tmr.stop(1) tmr.alarm (1,waitTime,1,getReady) end sendData() end end end startListening() tmr.alarm(1, 5000, 1, getReady)