serverlight.lua
local pin = 7
_G.webpage = "Testmin.html"
local u = require("Utility")
local webcontent = u.getPage()
gpio.mode(pin,gpio.OUTPUT)
print("server has been initiated \nversion: light")
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive",process)
conn:on("sent",
function(conn)
conn:close()
collectgarbage()
end)
end)
function process(conn, payload)
local query = u.getQuery(payload)
if (query.on)
then
print(query.on)
--close and open lights
if query.on == "true"
then
gpio.write(pin,gpio.HIGH)
elseif query.on == "false"
then
gpio.write(pin,gpio.LOW)
end
--sending
conn:send(webcontent)
print(node.heap())
end
collectgarbage()
end
Utility.lua
local M = {}
--start of utility functions
local webpage = "Testmin.html"
function M.getPage()
local s = ""
if file.open(webpage, "r")
then
while true do
local c = file.readline()
if c==nil
then
break
end
s = s .. c
end
end
return s
end
--process request
function M.getQuery(payload)
local q = {}
local str = payload
repeat
--get payload and then regex; getting parameters
local var,val,amp,new = str:match("(%w+)=(%w+)()&? ?()")
q[var] = val:gsub("%+"," ")
str = str:sub(amp,str:len())
until new
return q
end
--end of utility functions
return M
Testmin.html
<!DOCTYPE html>
<html lang=en>
<head>
<title>Test</title>
<link rel=stylesheet href=https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css>
<link rel=stylesheet href=https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css>
<style type=text/css>p{padding:100px;font-size:32px;font-weight:bold;text-align:center;background:#f1f1f1}div{cursor:pointer}</style>
</head>
<body>
<div class=container>
<div class=row-sm-6>
<div class=col-sm-6 onClick="self.location=self.location.protocol+'//'
+self.location.host
+self.location.pathname+'?on=true'">
<p>ON</p>
</div>
<div class=col-sm-6 onClick="self.location=self.location.protocol+'//'
+self.location.host
+self.location.pathname+'?on=false'">
<p>OFF</p>
</div>
</div>
</div>
<script src=https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js></script>
<script src=https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js></script>
</body>
</html>