Chat about current Lua tools and IDEs
User avatar
By enaon
#20051 Hello,

this is a simple q&d solution that enables me to send files over the network to my esp. It is supposed to be helpful in situations where one is testing code in a esp connected to the uart using maybe luauploader, and when he is happy with the results, he can then send them to another esp over the air.

The code that goes to the esp is the following:
m_upload.lua
Code: Select allupload=nill
upload=net.createServer(net.TCP,10)
upload:listen(88,function(c)

 c:on("receive", function(c, fileName)
-- print(fileName)
    if (fileName=="file_upload_start")then
        c:send("**enter the file name**\n")
        print("**file upload started**")
        upload_mode=1
    elseif (fileName=="file_upload_stop")then
        file.close()
        c:send("**file "..savedFile.." saved**\n")
        c:send("heap :"..node.heap().."\n")
        print("\n\r**file "..savedFile.." saved**")
        print("heap :"..node.heap())
        savedFile,fileName,upload_mode=nill,nill,nill
    elseif (upload_mode==1)then
        c:send("**opened file "..fileName.." for writing**\n")
        c:send("heap :"..node.heap().."\n")
        print("**opened file "..fileName.." for writing**")
        print("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)
    end

 end)
end)

--file.format()
--dofile("m_upload.lc")
--node.compile("m_upload.lua")
--print(node.heap())
--file.remove("init.lua")


you upload it to the esp, then call it by dofile("m_upload.lua") (or lc if you compile it) and it listens on port 88
for a connection from the pc

You can then use this script on linux (netcat is needed)

save it as an sh in the same folder as your lua scripts(chmode +x uploadtoesp.sh) and call it using the filename.lua and the ip of the remote esp as arguments, like so
uploadtoesp.sh test.lua 192.168.4.10
Code: Select all#!/bin/bash
echo -n file_upload_start | nc -w 1  $2 88
echo  -n $1 | nc  -w 1  $2 88
echo ""
#while read -r x; do sleep 0.2; echo "$x"; echo -n "$x" | nc -w 1 $2 88 ; done < $1
while read -r x; do sleep 0.2; echo -n $x | nc -w 1 $2 88 ; done < $1
echo ""
echo -n file_upload_stop | nc -w 2  $2 88



from windows use the following 2 files:

uploadtoesp.bat (again call it like uploadtoesp.bat XXX.lua X.X.X.X)
Code: Select all@echo off
echo | set /p dummyName="file_upload_start" | nc.exe -w 1  %2 88
echo | set /p dummyName="%1" | nc.exe  -w 1  %2 88
echo.
nc.exe -e "parse.bat %1"  %2 88
type %1
echo.
echo.
echo | set /p dummyName="file_upload_stop"  | nc.exe -w 1  %2 88


parse.bat
Code: Select all@echo off
set wait=50
echo wscript.sleep %wait% > wait.vbs
for /F "delims=" %%a in (%1) do ( echo | set /p dummyName=%%a
wscript.exe wait.vbs )
del wait.vbs


you will again neet to place them where you keep your lua script, or adjust the path.

you will also need nc for windows.