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:
Code: Select allupload=nill

 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**")
    elseif (fileName=="file_upload_stop")then
        c:send("**file "..savedFile.." saved**\n")
        c:send("heap :"..node.heap().."\n")
        print("\n\r**file "..savedFile.." saved**")
        print("heap :"..node.heap())
    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")
        file.remove(fileName), "w+")
    elseif (upload_mode==2)then
       print(fileName.." >> " ..savedFile)



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 and call it using the filename.lua and the ip of the remote esp as arguments, like so test.lua
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
nc.exe -e "parse.bat %1"  %2 88
type %1
echo | set /p dummyName="file_upload_stop"  | nc.exe -w 1  %2 88

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.
User avatar
By JumpZero
#69574 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.
but ESPresso looks good and Lucas is back and working on it, let's see what's coming.
User avatar
By enaon
#69938 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. ... pfwd-apdns