#!/bin/bash

##############################################################################
# This script inits the esp with the miropython image
# end some needed dependencies like the sdcard and
# bmp180 libs
#
# This scripts has following dependencies:
# - esptool.py (https://github.com/espressif/esptool)
# - ampy (https://github.com/scientifichackers/ampy)
#
# The paths, files etc. may need to be configured 
# to your own environment
#
# Script is provided by Christopher Scheuring <chris@aucmail.de>
# as it is. Use by your own risks.
#
##############################################################################

echo "Flush the esp32 etc. using the esptool and ampy for uploading the micropyhton libs."
python3 /usr/local/bin/esptool.py --chip esp32 --port /dev/ttyUSB0 erase_flash
echo "Flashing starts in 1 second..."
sleep 1
python3 /usr/local/bin/esptool.py --chip esp32 --port /dev/ttyUSB0 --baud 230400 write_flash -z 0x1000 ../src/fw-bin/esp32-20220117-v1.18.bin
echo "Flash micropython finished"
sleep 1
echo "Upload micropython dependency..."
ampy -p /dev/ttyUSB0 put ../src/lib/sdcard.py
ampy -p /dev/ttyUSB0 put ../src/lib/bmp180.py
ampy -p /dev/ttyUSB0 ls
echo "Flashing finished"
