Modified HTML to call lua script via javascript. Also embedded an image just for stress testing the server
This commit is contained in:
parent
e571f19d81
commit
65538efc1b
@ -6,14 +6,60 @@
|
|||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>Garage Remote</title>
|
<title>Garage Remote</title>
|
||||||
</head>
|
</head>
|
||||||
|
<script>
|
||||||
|
var xmlHttp = null;
|
||||||
|
|
||||||
|
function pushTheButton(door)
|
||||||
|
{
|
||||||
|
var url = "/garage_door_opener.lua?door=" + door;
|
||||||
|
|
||||||
|
xmlHttp = new XMLHttpRequest();
|
||||||
|
xmlHttp.onreadystatechange = processRequest;
|
||||||
|
xmlHttp.open("GET", url, true);
|
||||||
|
xmlHttp.send( null );
|
||||||
|
}
|
||||||
|
|
||||||
|
function processRequest()
|
||||||
|
{
|
||||||
|
if(xmlHttp.readyState == 0)
|
||||||
|
{
|
||||||
|
document.getElementById('label').innerHTML = 'Initalizing...';
|
||||||
|
}
|
||||||
|
else if(xmlHttp.readyState == 1)
|
||||||
|
{
|
||||||
|
document.getElementById('label').innerHTML = 'Server connection established.';
|
||||||
|
}
|
||||||
|
else if(xmlHttp.readyState == 2)
|
||||||
|
{
|
||||||
|
document.getElementById('label').innerHTML = 'Request received.';
|
||||||
|
}
|
||||||
|
else if(xmlHttp.readyState == 3)
|
||||||
|
{
|
||||||
|
document.getElementById('label').innerHTML = 'Processing request.';
|
||||||
|
}
|
||||||
|
else if(xmlHttp.readyState == 4)
|
||||||
|
{
|
||||||
|
if(xmlHttp.status == 200)
|
||||||
|
{
|
||||||
|
document.getElementById('label').innerHTML = xmlHttp.responseText;
|
||||||
|
}
|
||||||
|
else if(xmlHttp.status == 400)
|
||||||
|
{
|
||||||
|
document.getElementById('label').innerHTML = 'Bad request.';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
<body bgcolor="#777777">
|
<body bgcolor="#777777">
|
||||||
<link href='http://fonts.googleapis.com/css?family=Khand:700' rel='stylesheet' type='text/css'>
|
<link href='http://fonts.googleapis.com/css?family=Khand:700' rel='stylesheet' type='text/css'>
|
||||||
<a href="garage_door_opener.lua?door=1" class="button">
|
<a href="#" onclick="pushTheButton(1); " class="button"">
|
||||||
<span>Door 1</span>
|
<span>Door 1</span>
|
||||||
</a>
|
</a>
|
||||||
<a href="garage_door_opener.lua?door=2" class="button">
|
<a href="#" onclick="pushTheButton(2); " class="button"">
|
||||||
<span>Door 2</span>
|
<span>Door 2</span>
|
||||||
</a>
|
</a>
|
||||||
|
<div id="label">Text goes here</div>
|
||||||
|
<img src="delorean.jpg">
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
|
|||||||
@ -4,10 +4,6 @@
|
|||||||
|
|
||||||
local function pushTheButton(connection, pin)
|
local function pushTheButton(connection, pin)
|
||||||
|
|
||||||
-- Redirect the user back to the static page where the garage door opener buttons are.
|
|
||||||
connection:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\Cache-Control: private, no-store\r\n\r\n")
|
|
||||||
connection:send('<script type="text/javascript">window.location.replace("/garage_door_opener.html");</script>')
|
|
||||||
|
|
||||||
-- push the button!
|
-- push the button!
|
||||||
-- Note that the relays connected to the garage door opener are wired
|
-- Note that the relays connected to the garage door opener are wired
|
||||||
-- to close when the GPIO pin is low. This way they don't activate when
|
-- to close when the GPIO pin is low. This way they don't activate when
|
||||||
@ -19,12 +15,18 @@ local function pushTheButton(connection, pin)
|
|||||||
gpio.write(pin, gpio.HIGH)
|
gpio.write(pin, gpio.HIGH)
|
||||||
gpio.mode(pin, gpio.INPUT)
|
gpio.mode(pin, gpio.INPUT)
|
||||||
|
|
||||||
|
-- Send back JSON response.
|
||||||
|
connection:send("HTTP/1.0 200 OK\r\nContent-Type: application/json\r\Cache-Control: private, no-store\r\n\r\n")
|
||||||
|
connection:send('{"error":0, "message":"OK"}')
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return function (connection, args)
|
return function (connection, args)
|
||||||
print('Garage door button was pressed!')
|
print('Garage door button was pressed!', args.door)
|
||||||
print('Door', args.door)
|
if args.door == "1" then pushTheButton(connection, 3) -- GPIO0
|
||||||
if args.door == "1" then pushTheButton(connection, 3) -- GPIO0
|
elseif args.door == "2" then pushTheButton(connection, 4) -- GPIO2
|
||||||
elseif args.door == "2" then pushTheButton(connection, 4) -- GPIO2
|
else
|
||||||
else dofile("httpserver-error.lc")(connection, {code = 400}) end -- Bad Request
|
connection:send("HTTP/1.0 400 OK\r\nContent-Type: application/json\r\Cache-Control: private, no-store\r\n\r\n")
|
||||||
|
connection:send('{"error":-1, "message":"Bad door"}')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user