Skip to content

Instantly share code, notes, and snippets.

@paulcuth
Last active August 29, 2015 14:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulcuth/0e051edd3304a2559721 to your computer and use it in GitHub Desktop.
Save paulcuth/0e051edd3304a2559721 to your computer and use it in GitHub Desktop.
Creates a web server that controls a shift register on an ESP8266.
gpio.write(8,0) --> Input
gpio.write(9,0) --> Clock
current = 8;
function set (val)
local zeros = 0
local ones = 0
if (val < current) then
zeros = 8 - val
ones = val
else
ones = val - current
end
if zeros > 0 then
gpio.write(8, 0)
for _ = 1, zeros do
gpio.write(9, 1)
gpio.write(9, 0)
end
end
if ones > 0 then
gpio.write(8, 1)
for _ = 1, ones do
gpio.write(9, 1)
gpio.write(9, 0)
end
end
current = val
end
set(0)
local srv = net.createServer(net.TCP)
srv:listen(80, function (conn)
conn:on('receive', function (conn, payload)
local val = string.match(payload, '^GET /(%d+) ')
if val ~= nil then
set(number(val))
conn:send'{result:1}'
else
conn:send"<input type=range value=0 min=0 max=8 id='slider'/>"
conn:send"<script>var x=0;function onchange() { if (this.value == x) return; x = this.value;"
conn:send"var xhr = new XMLHttpRequest();xhr.open('GET', '/'+x, true);xhr.responseType = 'text';xhr.send({});"
conn:send"}document.getElementById('slider').addEventListener('input',onchange);</script>"
end
end)
conn:on('sent', function (conn)
conn:close()
end)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment