-->
Page 1 of 2

simple file over network to esp

PostPosted: Wed Jun 10, 2015 9:23 am
by enaon
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.

Re: simple file over network to esp

PostPosted: Sat Aug 12, 2017 6:19 am
by enaon
some years have passed and I still use it with no problems ( except spaces in math should be avoided), but if anyone want to test, this is up to date.

https://gist.github.com/enaon

Re: simple file over network to esp

PostPosted: Wed Aug 30, 2017 10:51 am
by JumpZero
Hi Enaon,

I'm back home and just tried your scripts (lua and bash) the ones on this post, the simple ones, not the customized versions on you git. And it works like a charm, it just does the job.
So that's another way to do OTA maintenance.
Great,
but ESPresso looks good and Lucas is back and working on it, let's see what's coming.
--
Jmp0

Re: simple file over network to esp

PostPosted: Tue Sep 12, 2017 11:06 pm
by enaon
I am glad it worked for you, if you plan on using it you may want to test the git version, as it is a bit more refined.

In any case, although this way of uploading files works just fine, it requires that a connection is made from the pc to the esp. As I have found out in practice, this is not ideal in real world implementations, where you put an esp to a friends house or something, behind a nat modem.

I am now testing the next version where the esp is initiating the connection, and not the other way around. It periodically checks for an update flag on the server, and if one is found it will download the files marked and update itself.

It is written in node.js, I will post the code here sometime this month.

Also, if anyone is interested, in my git there is a custom nodemcu-firmware version, with ipforwarding and napt enabled, so as to use the esp as an Access point for other esps ect.

https://github.com/enaon/nodemcu-firmwa ... pfwd-apdns