Skip to content

Instantly share code, notes, and snippets.

@judepereira

judepereira/ota.lua

Last active Dec 20, 2020
Embed
What would you like to do?
Send OTA updates to remotely update lua scripts on your ESP8266.
-- Send OTA updates to remotely update lua scripts on your ESP8266.
--
-- LICENCE: http://opensource.org/licenses/MIT
-- Created by Jude Pereira <contact@judepereira.com>
-- See https://judepereira.com/blog/sending-ota-updates-over-wifi-to-your-esp8266/
srv = net.createServer(net.TCP)
current_file_name = nil
srv:listen(8080, function(conn)
conn:on("receive", function(sck, payload)
if string.sub(payload, 1, 5) == "BEGIN" then
current_file_name = string.sub(payload, 7)
file.open(current_file_name, "w")
file.close()
sck:send("NodeMCU: Writing to " .. current_file_name .. '...\n')
elseif string.sub(payload, 1, 4) == "DONE" then
sck:send("NodeMCU: Wrote file " .. current_file_name .. "!\n")
current_file_name = nil
elseif string.sub(payload, 1, 7) == "RESTART" then
sck:send("NodeMCU: Restart!\n")
tmr.create():alarm(500, tmr.ALARM_SINGLE, node.restart)
else
if file.open(current_file_name, "a+") then
if file.write(payload) then
file.close()
sck:send("ok\n")
else
sck:send("NodeMCU: Write failed!\n")
end
else
sck:send("NodeMCU: Open failed!\n")
end
end
end)
conn:on("sent", function(sck) sck:close() end)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment