Last active
September 14, 2015 20:49
-
-
Save stonehippo/e7a6f0160101389c0570 to your computer and use it in GitHub Desktop.
Tankbot esp8266 controller
This file contains hidden or 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
-- startup logic | |
dofile("wifi.lua") | |
dofile("server.lua") |
This file contains hidden or 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
-- HTTP server for RESTful remote API | |
local function create_response(request) | |
local response = "HTTP/1.1 200 OK\r\nServer: Tankbot\r\nContent-Type: text/html\r\n\r\n" | |
return response | |
end | |
local function handle_request(c) | |
c:on("receive", | |
function(c, request) | |
print(request) | |
c:send(create_response(request)) | |
c:close() | |
end) | |
end | |
-- Listen for HTTP requests on TCP port 80 | |
server = net.createServer(net.TCP) | |
server:listen(80, handle_request) |
This file contains hidden or 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
-- network setup | |
local SSID = "ssid" | |
local SSID_PWD = "pwd" | |
-- timeout before we give up trying to connecting at all | |
local CONNECTION_TIMEOUT = 60000 | |
-- interval between checks to see if connected | |
local RETRY_TIMEOUT = 1000 | |
local function check_timeout() | |
if wifi.sta.getip() == nil then | |
print("Waiting to connect to wifi") | |
else | |
tmr.stop(1) | |
print("Connected to wifi" .. wifi.sta.getip()) | |
end | |
end | |
local function connect() | |
-- set up the network | |
wifi.setmode(STATIONAP) | |
wifi.sta.config(SSID, SSID_PWD) | |
-- wait for the module to connect to the network | |
tmr.alarm(1, RETRY_TIMEOUT, 1, check_timeout) | |
-- cancel the retries if we haven't connected after a while | |
tmr.alarm(2, CONNECTION_TIMEOUT, 0, function() | |
tmr.stop(1) | |
end) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment