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 » Tue Sep 24, 2019 3:02 pm

Hi, so a big THANK YOU goes out to all that helped. I'm now realizing that rshell is indeed a powerful utility that will be a big help with our esp32 MicroPython projects going forward.

Yes, we use Linux here (Ubuntu 16.04) on all our machines. We have a few Windows machines too, but we hardly ever use them these days. We find that Linux is much better suited for work with microprocessors running MicroPython.

So we now have a BASH script that runs from the root-launched terminal that erases the flash memory, installs MicroPython, and uploads all the files and folders for our zwave controller. This allows us to simply plug in an ESP32 to a USB port and run the script.

We also have these nice 8 way USB port splitters, so I think we can use rshell to process 8 at a time, if what I'm reading about the addressability of rshell is correct.

Once the files are uploaded, the final step is to press the reset button on the board to see if the correct IP text appears on the OLED screen. So I'm wondering if that could be automated too? The AMPY program has a 'run' command, but I'm not seeing that with rshell (I run a simple python file on the ESP32 that reboots it).

Does anyone know if there is a trick to forcing a reboot with rshell? Or a way to run a program on the ESP32?

Cheers, AB

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Using BASH script to upload files

Post by dhylands » Tue Sep 24, 2019 3:16 pm

rshell's repl command can be used to run a script.

Code: Select all

repl ~ import something ~
The trailing ~ tells rshell to exit the repl when 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 » Tue Sep 24, 2019 3:27 pm

Hi @dhylands, thanks so much for creating your great rshell system!

Actually, I just discovered that my ESP32 actually does an automatic reboot, or at least it runs the code files that have just been uploaded. Am I understanding that it somehow does an automatic reboot when rshell is finished (or maybe the bash script is finished)??

Regards, AB

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Using BASH script to upload files

Post by dhylands » Tue Sep 24, 2019 4:29 pm

It's possible that the board reboots when the serial port is closed, but that's a side-effect and not something specific that rshell is doing.

I know that some EPS32 boards reboot when the serial port is opened.

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 » Tue Sep 24, 2019 4:45 pm

Hi @dhylands, I think you might be right because it does not seem to be consistent. Although these boards are all identical, some have a slightly different chipset (voltage regulator and battery charge controller, I think). Some of them do, and some don't reboot, so I will see if I can run a MicroPython boot script using your rshell command, as a final proof-of-the-pudding.

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 » Wed Sep 25, 2019 7:02 pm

Hi Dave, thanks for the tip, that worked perfectly. Using rshell and an ESP32, here is the BASH script that erases the flash memory, installs MicroPython, then uploads all the files and sub-folders for our zwave blind and shade controller. Hopefully it will help someone else along the way ...

Code: Select all

#!/bin/bash  
echo "This is the Micropython FLASH & file/folder upload script ..."  

echo ""
cd /media/ab/shared/wamp/www/NodeSwitch.com/esp32-flash-files/non-spi-ram-flash/
echo "changed directory to ESP32 non-spiram flash files"
echo ""
echo "executing MCU erasure:"
esptool.py --port /dev/ttyUSB0 erase_flash
echo "===finished==="

echo ""
echo "executing Micropython installation:"
esptool.py --chip esp32 --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect -z 0x1000 esp32-2019-07-22-v1.11-167-latest.bin
echo ""
echo "===finished==="

echo ""
echo "Pausing 5 seconds for reboot ..."
sleep 5
echo ""
echo "changing directory to ZWAVE CORE FILES directory:"
echo "NOTE: this should be set one level above the actual target folder ..."
cd /media/ab/NodeSwitch.com/Unity-Project/MCU/OLED/core-files
echo "Zwave folder is now set as the SOURCE directory ..."

echo "copying all files and folders:"
echo ""
rshell -p /dev/ttyUSB0 "rsync -m zwave /pyboard/"
echo ""
echo "ALL FILES AND FOLDERS TRANSFERRED"

echo ""
echo "Pause 5 seconds before final re-set:"
sleep 5
echo ""
rshell -p /dev/ttyUSB0 "repl ~ import x ~"
echo ""
echo "===ALL DONE!==="
The BASH script filename is esp32-zwave, so it would be launched with this command in the terminal prompt:

./esp32-zwave

I should also mention that you have to install esptool and rshell on your system first.

Cheers, Adrian

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 » Thu Sep 26, 2019 9:55 am

@iotman As an advanced user of rshell and rsync you might be interested in my fork which contains code for a couple of PR's which are pending review. The additional functionality is as follows.
  • rsync can be set to ignore files and/or directories. This enables you to synchronise a hardware device to a Python package while ignoring docs, private directories and suchlike.
  • A macro facility is provided enabling text macros (with optional parameters) to be expanded. This can save a lot of typing and protect you from destructive typos (e.g. create a project-specific macro to move to the right directory before issuing rsync). It also enables new rshell commands like mv (move/rename) to be created.
Peter Hinch
Index to my micropython libraries.

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 » Thu Sep 26, 2019 3:56 pm

Hi Peter, thanks for those tips; it is appreciated. I am new to rshell and bash scripting, so I can see I have a lot to learn. I'm particularly interested in the macro capability since our primary goal has been to automate the preparation of the ESP32 MCU boards.

Also, the first point about ignoring certain files solved a specific problem for me. I had some test files that I did not want uploaded from the source directory, so I had deleted them (which I didn't really want to do). So now I see they can be ignored.

Cheers, Adrian

Post Reply