Created
February 7, 2016 17:51
-
-
Save smcl/2b39ca641ddad9d3e851 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--wifi.setmode(wifi.STATION) | |
--wifi.sta.config("MyAccessPoint","mypassword") | |
led = 4 | |
ledStatus = gpio.HIGH | |
gpio.mode(led, gpio.OUTPUT) | |
file.open("led.html") | |
htmlTemplate = file.read() | |
file.close() | |
server = net.createServer(net.TCP) | |
server:listen(80, function(conn) | |
conn:on("receive", function(conn, req) | |
-- note: this is NOT a smart way to route requests :) | |
if string.find(req, "GET") then | |
print("GET") | |
ledStatusString = "on" | |
if ledStatus == gpio.HIGH then | |
ledStatusString = "off" | |
end | |
html = string.format(htmlTemplate, ledStatusString) | |
conn:send(html) | |
else | |
print("POST") | |
if ledStatus == gpio.HIGH then | |
ledStatus = gpio.LOW | |
else | |
ledStatus = gpio.HIGH | |
end | |
gpio.write(led, ledStatus) | |
conn:send("OK") | |
end | |
conn:close() | |
end) | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment