Using BASH script to upload files

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
User avatar
iotman
Posts: 52
Joined: Sat Feb 02, 2019 4:06 pm
Location: Nanoose Bay, Canada
Contact:

Re: Using BASH script to upload files

Post by iotman » Sun Sep 22, 2019 11:52 pm

Hi, ok, that did the trick, now that I reflashed MicroPython and rebooted everything. I guess my system was somehow confused because I had run ampy and rshell so many times. The rshell file transfer that @kevinkk525 suggested works just fine!

I also tried reflashing before I rebooted the laptop and it could not find the USB port, so there was definitely something wrong. So mea culpa, I didn't follow my own advice that I often give - if things aren't working, reboot your machine! :)

Just curious, does anyone know if rshell will transfer a "lib" sub-directory that contains files and other sub-directories? I'm able to do that with AMPY (with a single command), but I would like to stick with an "all rshell" solution if that is possible.

Cheers, AB

User avatar
iotman
Posts: 52
Joined: Sat Feb 02, 2019 4:06 pm
Location: Nanoose Bay, Canada
Contact:

Re: Using BASH script to upload files

Post by iotman » Mon Sep 23, 2019 5:15 pm

Hi, so I have been studying the rshell program that @dhylands created, and it seems very capable, with the ability to do everything I need to do.

Just to recap, I am trying to flash an ESP32 with Micropython (from a BASH script), then upload a file set to it that includes sub-directories with files in them, and also further lower level sub-directories with files in them.

I was hoping beyond hope I could use pattern matching just to upload everything in a folder on my Ubuntu laptop all at once, something like this:

rshell -r /dev/ttyUSB0 "cp *.* /pyboard/"

but I just don't seem to be able to use the syntax correctly.

I can upload files to the root folder using this line in a bash script (as per previous discussion):

rshell -p /dev/ttyUSB0 "cp f.py /pyboard/;cp boot.py /pyboard/;cp config_wifi.py /pyboard/"

but I cannot seem to upload a sub-directory that includes lower level files and folders. I have tried all kinds of syntax combinations, but I keep getting the dreaded "rshell: error: unrecognized arguments:" message. After studying @dhylands github notes, I would have thought that this would work:

rshell -r /dev/ttyUSB0 "cp /lib /pyboard/"

but it doesn't. I have tried many combinations and just can't figure it out. Does anyone know what it should be??

Tks, AB

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: Using BASH script to upload files

Post by kevinkk525 » Mon Sep 23, 2019 6:36 pm

That's because you didn't read the whole documentation of rshell. It has a rsync feature that will copy your subdirectories.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
iotman
Posts: 52
Joined: Sat Feb 02, 2019 4:06 pm
Location: Nanoose Bay, Canada
Contact:

Re: Using BASH script to upload files

Post by iotman » Mon Sep 23, 2019 7:19 pm

Hi, well, I actually did read all the documentation, but I did not realize that rsync might be the command that would do what I need. So it seems to suggest that rshell can be used to upload all files and folders to an ESP32 with a single command.

But call me dense, I still can't figure out what the actual syntax should be. I'm surprised by this because I would have thought that once an actual product production is taking place, this would be a standard thing that people would want to do, yet in all the searching I have done, I cannot find a single example of how to do it.

Anyway, thanks for your example of how to use it to upload single files, at least that part worked.

Tks, AB

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Using BASH script to upload files

Post by Roberthh » Mon Sep 23, 2019 7:28 pm

Code: Select all

$ rshell rsync --help

usage: rsync [-m|--mirror] [-n|--dry-run] [-q|--quiet] SRC_DIR DEST_DIR

Synchronizes a destination directory tree with a source directory tree.

positional arguments:
  SRC_DIR        Source directory
  DEST_DIR       Destination directory

optional arguments:
  -h, --help     show this help message and exit
  -a, --all      Don't ignore files starting with .
  -m, --mirror   causes files in the destination which don't exist in the
                 source to be removed. Without --mirror only file copies
                 occur. No deletions will take place.
  -n, --dry-run  shows what would be done without actually performing any file
                 copies. Implies --verbose.
  -q, --quiet    Doesn't show what has been done.

User avatar
iotman
Posts: 52
Joined: Sat Feb 02, 2019 4:06 pm
Location: Nanoose Bay, Canada
Contact:

Re: Using BASH script to upload files

Post by iotman » Mon Sep 23, 2019 7:48 pm

Hi, so here I am trying to copy a folder (lib) with all its files and sub-directories to the root folder on an ESP32 with rshell; does anyone know what it should be? This does not work, and I have re-read the instructions and tried many combinations ...

rshell -m /dev/ttyUSB0 "rsync /lib /pyboard/lib/"

Tks, AB

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: Using BASH script to upload files

Post by kevinkk525 » Mon Sep 23, 2019 8:52 pm

That command can't work. You are trying to copy the lib folder in your root directory of your system.

If you execute rshell inside the directory of your lib, the command should be:
rshell -p /dev/ttyUSB0 "rsync -m lib /pyboard/"
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
iotman
Posts: 52
Joined: Sat Feb 02, 2019 4:06 pm
Location: Nanoose Bay, Canada
Contact:

Re: Using BASH script to upload files

Post by iotman » Mon Sep 23, 2019 9:07 pm

Hi, ok, thank you, that helps me to understand where the optional commands need to go. It also seems like a slash in the wrong place can cause problems too.

Up to this point I have been opening a terminal screen in a special folder for scripts, but ultimately I want to be able to plug an ESP32 into a USB port, then execute this script from an icon on the desktop (to install MicroPython and upload files & folders). This way it should be a total no-brainer for my co-workers to prepare the MCU boards.

It seems like something like that should be pretty straightforward, but it ain't so! Anyway, I will keep plugging along, and if I get it to finally work I will publish the results here, in the hope that it may help someone else ...

Thanks again for the help, regards AB

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: Using BASH script to upload files

Post by kevinkk525 » Tue Sep 24, 2019 6:51 am

Are you on windows? I never tried rshell on windows.

On linux you'd adapat your lib path to something like "~/project/lib". Then it wouldn't matter from where your script is called.
But since your question is about BASH it might be linux so this should be the way to go.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Using BASH script to upload files

Post by pythoncoder » Tue Sep 24, 2019 9:26 am

You can also use the -r flag with cp (and rm):

Code: Select all

/mnt/qnap2/data/Projects/Python/client_server> cp --help
usage: cp SOURCE DEST               Copy a single SOURCE file to DEST file.
       cp SOURCE... DIRECTORY       Copy multiple SOURCE files to a directory.
       cp [-r|--recursive] [SOURCE|SOURCE_DIR]... DIRECTORY
       cp [-r] PATTERN DIRECTORY    Copy matching files to DIRECTORY.

The destination must be a directory except in the case of copying a single
file. To copy directories -r must be specified. This will cause directories
and their contents to be recursively copied.

positional arguments:
  FILE             Pattern or files and directories to copy

optional arguments:
  -h, --help       show this help message and exit
  -a, --all        Don't ignore files starting with .
  -r, --recursive  Copy directories recursively
Peter Hinch
Index to my micropython libraries.

Post Reply