Flash script linux

From Let's Control It
Revision as of 14:33, 26 February 2017 by Tancapoo (talk | contribs)
Jump to navigation Jump to search

Flash script for linux

To flash an ESP8266 on Linux, you need the esptool as it is shipped with Arduino IDE or as standalone tool, see github source and binary releases.

Felix' script

Felix posted a script in the forum that works like the windows flash.cmd script:

   #!/bin/bash
   read -e -p "Serial Port                          :" -i "/dev/ttyUSB0" comport
   read -e -p "Flash Size (example 512, 1024, 4096) :" -i "4096" fsize
   read -e -p "Build (example 71, 72, ..)           :" -i "120" build
   size="${fsize}K"
   if [ $fsize -gt 1000 ]; then
           size=$(($fsize/1024))"M"
   fi
   echo "Expected Flash Size: $size"
   echo "Using com port: $comport"
   if [ -f ESPEasy_R${build}_${fsize}.bin ]; then
           echo "Using bin file: ESPEasy_R${build}_${fsize}.bin [FOUND]"
           ./esptool -vv -cd nodemcu -cb 115200 -bz $size -cp $comport -ca 0x00000 -cf ESPEasy_R${build}_${fsize}.bin
   else
           echo "Expected bin file: ESPEasy_R${build}_${fsize}.bin [NOT FOUND!!!]"
   fi

Thonshic's script

The following script tries to find the esptool binary

  • in Arduino IDE install paths
  • in a dir of the PATH variable
  • the local directory (with the script and firmware files)

You get a choice of firmware files, either with dialog or by number.

   #!/bin/bash
   selectst=""
   for i in ESPEasy*bin
   do
           selectst="$i $i off $selectst"
   done
   # check for esptool
   esptoolbin=$(which esptool)
   if [ ! -x "$esptoolbin" ] ; then
           if [ -x "./esptool" ] ; then
                   esptoolbin="./esptool"
           else
                   eardu=$(find $HOME/.arduino15/packages/esp8266/tools/esptool/* -name esptool|sort|tail -1)
                   if [ -x "$eardu" ] ; then
                           echo "found esptool: $eardu"
                           esptoolbin="$eardu"
                   fi
           fi
   fi
   if [ ! -x "$esptoolbin" ] ; then
           echo "esptool not found, you can link it here or put in \$PATH"
           exit 2
   else
           echo "using esptool $esptoolbin"
   fi
   read -e -p "serial device, (USB2serial devices: $(ls /dev/ttyUSB*)):" -i "/dev/ttyUSB0" serdev
   if [ ! -r "$serdev" ] ; then
           echo "device '$serdev' does not exist"
           exit 2
   fi
   if [ -n "$(which dialog)" ] ; then
           cmd=(dialog --radiolist "Select firmware" 22 76 16)
           choice=$("${cmd[@]}" ${selectst} 2>&1 >/dev/tty)
   else
           # bash only, do it with an array
           fa=(ESPEasy_*.bin)
           cnt=0
           nfa=${#fa[*]}
           while [ $cnt -lt $nfa ]
           do
                   echo $cnt ${fa[$cnt]}
                   ((cnt++))
           done
           read -p "choose number of firmware file to flash [0-$((nfa-1))]: " nsel
           choice=${fa[$nsel]}
           if [ ! -f "$choice" ] ; then 
                   echo "cannot find file \"$choice\", exiting"
                   exit 2
           fi
   fi
   echo "----"
   read -p "flash $choice? [Y/n]" yn
   if [ "$yn" != "n" ] ; then
       "$esptoolbin" -vv -cd nodemcu -cb 115200 -cp "$serdev" -ca 0x00000 -cf "$choice"
   fi

Example output

using esptool ./esptool
serial device, (USB2serial devices: /dev/ttyUSB0):/dev/ttyUSB0
   ┌──────────────────────────────────────────────────────────────────────────┐
   │ Select firmware                                                          │  
   │ ┌──────────────────────────────────────────────────────────────────────┐ │  
   │ │           (*) ESPEasy_R147_512.bin   ESPEasy_R147_512.bin            │ │  
   │ │           ( ) ESPEasy_R147_4096.bin  ESPEasy_R147_4096.bin           │ │  
   │ │           ( ) ESPEasy_R147_1024.bin  ESPEasy_R147_1024.bin           │ │  
   │ │                                                                      │ │  
   │ │                                                                      │ │  
   │ │                                                                      │ │  
   │ │                                                                      │ │  
   │ │                                                                      │ │  
   │ │                                                                      │ │  
   │ │                                                                      │ │  
   │ │                                                                      │ │  
   │ │                                                                      │ │  
   │ │                                                                      │ │  
   │ │                                                                      │ │  
   │ │                                                                      │ │  
   │ └──────────────────────────────────────────────────────────────────────┘ │  
   ├──────────────────────────────────────────────────────────────────────────┤  
   │                   <   OK    >          <Abbrechen>                       │  
   └──────────────────────────────────────────────────────────────────────────┘  
   ----
   flash ESPEasy_R147_512.bin? [Y/n]
   esptool v0.4.9 - (c) 2014 Ch. Klippel <ck@atelier-klippel.de>
       setting board to nodemcu
       setting baudrate from 115200 to 115200
       setting port from /dev/ttyUSB0 to /dev/ttyUSB0
       setting address from 0x00000000 to 0x00000000
       espcomm_upload_file
       espcomm_upload_mem
   opening port /dev/ttyUSB0 at 115200
       tcgetattr
       tcsetattr
       serial open
   opening bootloader
   resetting board
   trying to connect
       espcomm_send_command: sending command header
       espcomm_send_command: sending command payload
   trying to connect
       espcomm_send_command: sending command header
       espcomm_send_command: sending command payload
       espcomm_send_command: receiving 2 bytes of data
       espcomm_send_command: receiving 2 bytes of data
       espcomm_send_command: receiving 2 bytes of data
       espcomm_send_command: receiving 2 bytes of data
       espcomm_send_command: receiving 2 bytes of data
       espcomm_send_command: receiving 2 bytes of data
       espcomm_send_command: receiving 2 bytes of data
       espcomm_send_command: receiving 2 bytes of data
   Uploading 431376 bytes from ESPEasy_R147_512.bin to flash at 0x00000000
       erasing flash
       size: 069510 address: 000000
       first_sector_index: 0
       total_sector_count: 106
       head_sector_count: 16
       adjusted_sector_count: 90
       erase_size: 05a000
       espcomm_send_command: sending command header
       espcomm_send_command: sending command payload
       setting timeout 15000
       setting timeout 100
       espcomm_send_command: receiving 2 bytes of data
       writing flash
   ................................................................................ [ 18% ]
   ................................................................................ [ 37% ]
   ................................................................................ [ 56% ]
   ................................................................................ [ 75% ]
   ................................................................................ [ 94% ]
   ......................                                                           [ 100% ]
   starting app without reboot
       espcomm_send_command: sending command header
       espcomm_send_command: sending command payload
       espcomm_send_command: receiving 2 bytes of data
   closing bootloader