Skip to content

Instantly share code, notes, and snippets.

@ben-chain
Created February 20, 2015 03:36
Show Gist options
  • Save ben-chain/5d3b75252456f319ee92 to your computer and use it in GitHub Desktop.
Save ben-chain/5d3b75252456f319ee92 to your computer and use it in GitHub Desktop.
print("Bens ESP8266 starting up!")
cfg={}
cfg.ssid="neu"
cfg.pwd="secretsharing"
print(cfg.ssid)
wifi.ap.config(cfg)
wifi.setmode(wifi.SOFTAP) --access point, broadcasts like a rounter
-- cfg = config settings for this
n = 100; --Number of people needed to come together.
--Make this high so it's obvious it needs to be set
rem = 100;--Remaining entries needed.
calctable = {} --Table for Neville's Algorithm
values = {} --Table of points
bc = wifi.ap.getip()
print(bc) --192.158.4.1
srv=net.createServer(net.TCP) --TCP Server on port 80
srv:listen(80,function(conn)
conn:on("receive",function(conn,payload)
print(payload) --print each request over serial
if (string.find(payload,"GET") == 1) then
if (string.find(payload,"GET / HTTP") == 1) then --homepag
if rem == 0 then
conn:send("<h1>SUCCESS. SECRET: "..calctable[n-1][1].."</h1>") --in case we're done!
end
conn:send("<h1> Hello, Agent. "..rem.." more codes needed.</h1>")
conn:send("<form action='addvalue' method='post'>X-Value: <input type='text' name='xval'><br>Y-Value: <input type='text' name='yval'><br><input type='submit' value='Submit'></form>")
conn:send([[<a href="/reset">RESET, NEW SECRET</a>]])
elseif (string.find(payload,"GET /reset HTTP") == 1) then --form to set new n/reset
conn:send("<h1>Note: THIS WILL DELETE ALL PREVIOUSLY ENTERED POINTS</h1>")
conn:send("<form action='setn' method='post'>New N needed: <input type='text' name='newn'><br><input type='submit' value='Submit'></form>"
end
end
if (string.find(payload,"POST /setn HTTP") == 1) then --setting new n
npos = string.find(payload,"newn=")
numstr = string.sub(payload, npos+5, string.len(payload))
n = tonumber(numstr) --set n
rem = n --set remaining = n, no points yet
conn:send("<h1>Done.</h1>")
conn:send([[<a href="/">Go Back</a>]])
elseif (string.find(payload,"POST /addvalue HTTP") == 1) then --new point
conn:send("<h1>Done.</h1>")
conn:send([[<a href="/">Go Back</a>]])
xpos = string.find(payload,"xval=") --these lines parse each coord
ypos = string.find(payload,"yval=")
xstr = string.sub(payload, xpos+5, ypos-2) --this makes sure we get all digits
ystr = string.sub(payload, ypos+5, string.len(payload))
print("xstr: "..xstr.." and ystr: "..ystr) --debug
values[2*(n-rem)] = tonumber(xstr) --add the parsed values to our stack
values[2*(n-rem) + 1] = tonumber(ystr)
rem = rem - 1
if rem == 0 then --in case we're done!
calctable[0] = {}
for i = 1,n do
calctable[0][i] = values[2*(i-1) + 1] --send y values to caltable[0][1->n]
print('i= '..i)
print('vslue = '..values[2*(i-1) + 1])
end
for i = 1,(n-1) do -- Neville's algorithm, cycle through each column, index 0 to n-1
calctable[i] = {} --make 2D
for k = 1,n-i do --"triangle": further we go out, fewer rows calculated
calctable[i][k] = (values[2*(k+i-1)]*calctable[i-1][k] - values[2*(k-1)]*calctable[i-1][k+1])/(values[2*(k+i-1)] - values[2*(k-1)]) -- This implementation is a mess but it is working. I should have indexed everything starting at 0, but oh well. denom = (x[k+i] - x[k])
end
end
conn:send("<h1>SUCCESS. SECRET: "..calctable[n-1][1].."</h1>") --Woohoo!
end
end
end)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment