Last active
August 14, 2017 23:20
-
-
Save enaon/daf5cff67355b415b09565d8f5eebba3 to your computer and use it in GitHub Desktop.
Nodemcu file upload over ip.
This file contains 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
--init | |
--node.setcpufreq(node.CPU80MHZ) -- 'node.CPU80MHZ|node.CPU160MHZ' | |
boot_status=file.exists("boot_status.lua") | |
node_version="driot" | |
--for m_srv.lua and init_var_xxx.lua | |
m={} | |
pl="" | |
--check if boot_status exists | |
function normal() | |
print("boot status set to normal") | |
file.open("boot_status.lua", "w") | |
file.write(1) | |
file.close() | |
node.restart() | |
end | |
if (boot_status~=true)then | |
if file.exists("m_recovery.lc") then dofile("m_recovery.lc") end | |
else | |
if file.exists("init_var.lc") then dofile("init_var.lc") set_modules() init_modules() set_modules,init_modules=nil,nil end | |
if file.exists("m_srv.lc") then dofile("m_srv.lc") end | |
if file.exists("m_client.lc") then dofile("m_client.lc") end | |
end | |
--print(node.heap()) | |
--print(boot_status) |
This file contains 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
--init | |
--node.setcpufreq(node.CPU80MHZ) -- 'node.CPU80MHZ|node.CPU160MHZ' | |
boot_status=file.exists("boot_status.lua") | |
node_version="driot" | |
--for m_srv.lua and init_var_xxx.lua | |
m={} | |
pl="" | |
function normal() | |
print("boot status set to normal") | |
file.open("boot_status.lua", "w") | |
file.write(1) | |
file.close() | |
end | |
if (boot_status~=true)then | |
if file.exists("m_recovery.lc") then dofile("m_recovery.lc") end | |
else | |
file.remove("boot_status.lua") | |
if file.exists("init_var.lc") then dofile("init_var.lc") set_modules() init_modules() set_modules,init_modules=nil,nil end | |
if file.exists("m_srv.lc") then dofile("m_srv.lc") end | |
if file.exists("m_client.lc") then dofile("m_client.lc") end | |
init_tmr=tmr.create() | |
tmr.register(init_tmr, 500, tmr.ALARM_SINGLE, function () | |
normal() | |
init_tmr=nil | |
end) | |
tmr.start(init_tmr) | |
end | |
--print(node.heap()) | |
--print(boot_status) |
This file contains 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
upload=net.createServer(net.TCP,10) | |
upload:listen(88,function(c) | |
c:on("receive", function(c, fileName) | |
upload_s=c | |
if (fileName=="file_upload_start")then | |
c:send("-Init command recieved, waiting for filename Input\n") | |
print("-Starting, waiting for filename Input") | |
upload_mode=1 | |
elseif (fileName=="file_upload_stop")then | |
file.close() | |
local send=("-Data transfer ok, now compiling "..savedFile.."\n") | |
print("-Data transfer ok, now compiling "..savedFile) | |
if savedFile~="init.lua" and savedFile~="init_safe.lua" then | |
node.compile(savedFile) | |
file.remove(savedFile) | |
send=(send.."-Finished compiling "..savedFile.."\n") | |
print("-Finished compiling "..savedFile) | |
if string.match(savedFile, "init_var_.*.lua$") and file.exists("init_var_"..string.match(savedFile, "init_var_(.*).lua$")..".lc")then | |
send=(send.."-init_var found, renaming "..string.match(savedFile, "(init_var_.*).lua$")..".lc to init_var.lc\n") | |
print("-init_var found, renaming "..string.match(savedFile, "(init_var_.*).lua$")..".lc to init_var.lc\n") | |
file.remove("init_var.lc") | |
file.rename("init_var_"..string.match(savedFile, "init_var_(.*).lua$")..".lc","init_var.lc") | |
elseif (savedFile=="m_recovery.lua") then | |
send=(send.."-m_recovery.lua was updated, restarting node to take effect\n") | |
print("-m_recovery.lua was updated, restarting node to take effect\n") | |
tmr.alarm(1,1000,0,function() node.restart() end) | |
end | |
else | |
if savedFile=="init_safe.lua" then file.rename( "init_safe.lua","init.lua") end | |
send=(send.."-Skipping compilation of lua.init \n") | |
print("-Skipping compilation of lua.init") | |
end | |
c:send(send.."-heap: "..node.heap().."\n") | |
print("-heap: "..node.heap()) | |
fileName,savedFile,upload_mode=nil,nil,nil | |
elseif (upload_mode==1)then | |
c:send("-got filename : "..fileName.." , recieving data\n-current Heap :"..node.heap().."\n") | |
print("-got filename :"..fileName..", recieving data") | |
print("-current Heap :"..node.heap().."\n") | |
savedFile=fileName | |
file.remove(fileName) | |
file.open(fileName, "w+") | |
upload_mode=2 | |
elseif (upload_mode==2)then | |
c:send(">> "..fileName.."\n") | |
--print(fileName.." >> " ..savedFile) | |
file.writeline(fileName) | |
elseif (fileName=="node_restart")then | |
c:send("-Setting boot flag to normal and Restarting node\n") | |
print("heap :"..node.heap()) | |
file.open("boot_status.lua", "w") | |
file.write(1) | |
file.close() | |
tmr.alarm(1,2000,0,function() | |
node.restart() | |
end) | |
return | |
elseif (fileName=="node_telnet")then | |
if file.exists("m_recovery_telnet.lc") then | |
c:send("-going to telnet mode, port 2323\n") | |
upload:close() | |
dofile("m_recovery_telnet.lc") | |
return | |
else | |
c:send("-no m_recovery_telnet.lc file exists, please upload one\n") | |
end | |
elseif (string.sub(fileName, 1, 5)=="file_") then | |
if string.sub(fileName, 1, 9)=="file_list" then | |
local send="" | |
l = file.list() | |
for k,v in pairs(l) do | |
send=(send..("name:"..k..", size:"..v.."\n")) | |
end | |
c:send(send) | |
elseif string.sub(fileName, 1, 9)=="file_info" then | |
remaining, used, total=file.fsinfo() | |
c:send("File system info:\nTotal : "..total.." (k)Bytes\nUsed : "..used.." (k)Bytes\nRemain: "..remaining.." (k)Bytes\n") | |
elseif string.sub(fileName, 1, 9)=="file_delt" then | |
file.remove(string.sub(fileName, 11, 35)) | |
c:send("File "..string.sub(fileName, 11, 35).." removed\n") | |
else | |
c:send("No option -"..string.sub(fileName, 6, 15).."- found.\nOptions are: list,info,delt\n") | |
end | |
else | |
c:send("No command -".. fileName .."- found.\nYou can update configs using:\n./uploadtoesp.sh x.x.x.x xxx.lua\nor node_restart\nto reboot in normal mode\nor file_\n (dangerous)\n") | |
end | |
end) | |
end) | |
--node.compile("m_recovery.lua") |
This file contains 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
#!/bin/bash | |
echo -n file_upload_start | nc -w 1 $1 88 | |
echo -n $2 | nc -w 1 $1 88 | |
echo "" | |
#while read -r x; do sleep 0.05; echo "$x"; echo -n "$x" | nc -w 1 $1 88 ; done < $2 | |
while read -r x; do sleep 0.03; echo -n $x | nc -w 1 $1 88 ; done < $2 | |
echo "" | |
echo -n file_upload_stop | nc -w 2 $1 88 | |
read -p "Do you want to restart(r) esp or continue(c) recovery (r/c)?" RC | |
if echo "$RC" | grep -iq "^R" ;then | |
#if [ "$RC" == "r" ];then | |
echo "restarting esp" | |
echo -n node_restart | nc -w 2 $1 88 | |
else | |
echo "done, waiting for next file" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
simple how to use.
upload m_recovery.lua to esp
( optionally compile it with node.compile("m_recovery.lua"))
and run it with dofile("m_recovery.lua")
(or dofile("m_recovery.lc") if compiled)
on the pc (linux/mac) place uploadtoesp.sh in the same folder as the lua files and do
then upload files to esp using:
./uploadtoesp.sh ipaddrOfEsp file.lua
example:
it will compile the uploaded files so they will only run if compiled ok, and do some extra staff like rename init_var_xxx.lua to init_var.lua before compiling, as to use multiple configs in the same folder. It will also exclude init.lua and init_safe.lua from compiling, and rename the safe one.
the point of the two init.lua files is so that one can try new things and fear not that the module will go into a bootloop.
The init_safe.lua will break the loop and load m_recovery should that happen. When all is good and tested, init.lua is uploaded to prevent going to recovery due to power failures etc.
some extra commands can be sent manually as:
(remove the "file_" elseif part to save some mem.)
this is part of a bigger setup, but it is useful on its own I believe.
Spaces should not exist in math expressions in the lua file uploaded
netcat (nc) is needed on pc side.