I actually found this script
https://github.com/balu-/nodemcu-telnet-uploaderto upload files OTA
But it wasn't working very well, I did some improvements, I found it ok for my needs:
README.txt
============
Code: Select all---Guy---August 2017
--------------------
Original script come from
https://github.com/balu-/nodemcu-telnet-uploader
improvement done:
1- removing the comment following the shebang #!/usr/bin/expect
it is causing the expect command to fail
2- file.open() doesn't return true or false anymore but an object
IMHO this is a NodeMCU firmware change.
script modified accordingly
3- The original script fails to upload big files (around 100 lines)
This is due to a ESP restart during transfer. IMHO heap/memory
problem. To avoid this I modify the script to send batch of
60 lines (arbitrary choice) only. So I use the a+ instead of
w+ when opening the file (append mode). Send batch of 60 lines
flush, close and start again.
To achieve this there is a wrapper bash script calling the
expect script. expect script renamed NodeMcuTelnetUploaderByBatch
(from uload.sh) and wrapper script is NodeMcuTelnetUploader
Usage:
NodeMcuTelnetUploader <NodeMcu IP> <port> <Dest. file> <Source file>
Make sure the file <Dest. file> doesn't exist before transfer
because it doesn't overwrite but append.
NodeMcuTelnetUploaderByBatch
================================
Code: Select all#!/usr/bin/expect -f
#If it all goes pear shaped the script will timeout after 20 seconds.
set timeout 20
#First argument is assigned to the variable ip
set ip [lindex $argv 0]
#Second argument is assigned to the variable port
set port [lindex $argv 1]
#Third argument is assigned to the variable filename
set remotefilename [lindex $argv 2]
set filename [lindex $argv 3]
#prep file content
set fp [open "$filename" r]
set file_data [read -nonewline $fp]
close $fp
# Process data file
set data [split $file_data "\n"]
#This spawns the telnet program and connects it to the variable ip
spawn telnet $ip $port
#The script expects login
#expect "Connected to $ip."
expect "Escape character is '^]'."
sleep 1
# Modif Guy. Try: change w+ to a+ on file open and send file by smaller blocks
# + file.open() doesn't return true or false anymore but an object
# send "if true == file.open(\"$remotefilename\", \"w+\") then \r"
send "if nil ~= file.open(\"$remotefilename\", \"a+\") then \r"
expect ">>"
send "print(\"ok\")\r"
expect ">>"
send "end\r"
expect "ok"
foreach line $data {
# do some line processing here
expect ">"
#replace all ' with \'
regsub -all {\\r} $line {\\\\r} line
regsub -all {\\n} $line {\\\\n} line
regsub -all {'} $line {\\'} line
puts "writing $line"
send "if true == file.writeline('$line') then \r"
expect ">>"
send "print(\"ok\")\r"
expect ">>"
send "end\r"
expect "ok"
}
expect ">"
send "file.flush()\r"
expect ">"
send "file.close()\r"
expect ">"
send "print(\"done\")\r"
expect "done"
sleep 1
close
#This hands control of the keyboard over two you (Nice expect feature!)
#interact
NodeMcuTelnetUploader
================================
Code: Select all#!/bin/bash
# Guy August 2017
#
if [ $# -lt 4 ]; then
echo "Usage = $0 <NodeMcu IP address> <Telnet port> <Destination file> <Source file>"
exit
fi
# Let's split the file to send by blocks of 60 lines (arbitray choice)
split --lines=60 $4 /tmp/TmpFile
echo "Here are the temporary files we are going to send"
ls -l /tmp/TmpFile*
let "NumberOfFile=`ls -l /tmp/TmpFile* | wc -l`"
echo "Number of files to send = $NumberOfFile"
for i in {a..z} # 26 files max should be enough ;-)
do
if [ -e /tmp/TmpFilea$i ]; then
echo -e "\033[32mTransfer of /tmp/TmpFilea$i\033[0m"
# The 2 scripts must be in the same folder (the working dir)
./NodeMcuTelnetUploaderByBatch $1 $2 $3 /tmp/TmpFilea$i
echo -e "\033[32mTransfer done\033[0m"
sleep 1
fi
done
echo -e "\033[32mALL TRANSFERS DONE !"
echo "Removing temporary files"
rm /tmp/TmpFile*
echo -e "END\033[0m"
It may help others,
simple but do the job
--
Jmp0