Programming ESP8266 Without Compiling EVERY Time

Awesome PieterP, thanks for all the help! I'll investigate and see what I can/can't figure out lol.

Here's a new version of that script:

read -p "Path to binary: " path
read -p "Password: " password
read -p "Subnet IP: " sn_ip
read -a ip_arr -p "IP addresses: "
read -p "Press [ENTER] to start OTA"
echo    "--------------------------"

for ip in ${ip_arr[@]}
do
    python2 /home/$USER/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/tools/espota.py -i $sn_ip.$ip -p 8266 --auth="$password" -f "$path" 2> /dev/null && echo -e "Success:\t$sn_ip.$ip" || echo -e "Fail:   \t$sn_ip.$ip" &
done
wait

It just takes the path to a bin file, the password, and a range of IP addresses as input, and runs the OTA upload script for all these IP addresses at the same time.

Sample output:

Path to binary: /tmp/arduino_build_######/filename.ino.bin
Password: password123
Subnet IP: 192.168.1
IP addresses: 2 3 4 5 6
Press [ENTER] to start OTA
--------------------------
Fail:   	192.168.1.2
Fail:   	192.168.1.4
Fail:   	192.168.1.3
Fail:   	192.168.1.5
Success:	192.168.1.6

(I only have an ESP8266 on 192.168.1.6, so that's why the others failed.)

Pieter

That's really cool! How would I do that on Windows? Or even on Linux for that matter? Do I have to save that script with a certain extension and download Bash, then do something special to run it, etc. etc. Again, total noob here lol.

Also, the IP for my device is 192.168.1.73 so with that method how would I really know the range?

Well, this is just a bash script, all you need is a Linux distro like Ubuntu (that comes with Bash and Python) and the Arduino IDE with ESP8266 Core.
Then save the script with a ".sh" extension, e.g. OTA.sh. Then open a terminal (CTRL+ALT+T), go to the folder where you saved it, using cd foldername. Allow executing the file by using chmod +x OTA.sh, and run it: ./OTA.sh or source OTA.sh. (You can use tab to auto-complete the filename etc.)

I'm not familiar with the scripting languages on Windows, but it's a pretty basic script, so you could write a Windows equivalent if you wanted to.

androidfanboy:
Also, the IP for my device is 192.168.1.73 so with that method how would I really know the range?

Just use:

Subnet IP: 192.168.1
IP addresses: 73

If you had ESPs on 192.168.1.33, 192.168.1.53 and 192.168.1.73, for example, you would use

Subnet IP: 192.168.1
IP addresses: 33 53 73

If you want to try an entire range (e.g. from 192.168.1.2 to 192.168.1.254), you could just change the header of the for loop to go from 1 to 254. Keep in mind that that will create 254 python processes at the same time, so it could use a lot of RAM.

To know what IP addresses to use, you can check the Tools > Port menu in the IDE (it will do the network discovery for you) or use avahi-browse _arduino._tcp --resolve.

Pieter

By the way, do you know if there's an easy way to edit part of the text in command line? For example, if I wanted to change "COM5" to "COM6" from a copy/pasted command? I have to manually press and hold the left key to move the cursor there, backspace, then change it. Is there a way where I can just click there and edit?

You could write a shell script.
Or you can use CTRL+Left-arrow to jump from "word" to "word" instead of character by character.

Pieter

Here's another one:

read -p "Path to binary: " path
read -p "Password: " password

echo "Scanning for Arduino OTA devices on the network ..."
ip_arr=($(((timeout 10 avahi-browse _arduino._tcp --resolve -p -t) 2>/dev/null | grep -F "=;") | cut -d\; -f8))
if [ ${#ip_arr[@]} == 0 ]
then
    echo "No devices found"
    exit
fi
echo "IP addresses found: "
printf '\t%s\n' "${ip_arr[@]}"

read -p "Press [ENTER] to start OTA"
echo    "--------------------------"

for ip in ${ip_arr[@]}
do
    python2 /home/$USER/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/tools/espota.py -i $ip -p 8266 --auth="$password" -f "$path" 2> /dev/null && echo -e "Success:\t$ip" || echo -e "Fail:   \t$ip" &
done
wait

It prompts the path to the binary and the OTA password, then scans the network for _arduino._tcp services and resolves them to IP addresses. Then it starts a new process for each of the IP addresses and runs the OTA script for each device, all at the same time.

Pieter

Thanks again! However, I'm having a hard time converting it to work with a windows batch script. I wrote a script where the user can manually enter password and IP address and it works great (already a huge improvement!) but I need to figure out the syntax of converting your code to Windows.

I'm afraid I can't help you with that, I've never done any advanced shell scripting on Windows.

Most of the lines are pretty self-explanatory: they just read user inputs into variables (using "read"). The option "-p" means "prompt", so it displays the text that follows. The "-a" options tells "read" to read the data as an array.

Then Bash just loops over the array of IP addresses, and starts python:

python2 /home/$USER/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/tools/espota.py -i $sn_ip.$ip -p 8266 --auth="$password" -f "$path" 2> /dev/null && echo -e "Success:\t$sn_ip.$ip" || echo -e "Fail:   \t$sn_ip.$ip" &

Everything up to "2>" is just the normal command that is used by the IDE as well. Bash just fills in the variables (with dollar signs ($)).

"2> /dev/null" writes all error output to the "null" device, i.e. ignore error messages and don't print them in the console.

"&&" does the same as in C, it's a logical AND operator, and "||" is the logical OR operator.
In short: "python OTA && print(success) || print(fail)". That's basically equivalent to a ternary operator:
"python OTA ? print(success) : print(fail)"

The ampersand at the end of the line just means that it will start this line of code as a new process, so that you can program multiple devices at the same time, instead of one after the other.

The script in reply #27 can't be directly translated to Windows code, because it uses Avahi for mDNS.

I'd strongly recommend Ubuntu for these kinds of things. There's a reason why most professional programmers use Linux.
Compilation will be much faster, and it comes with much more powerful tools than Windows. Bash scripting is many times easier, more powerful, and the documentation is much better than Window's CMD or PowerShell.

Just install Ubuntu 16.04 as a secondary OS and dual-boot it with Windows if you can spare the disk space.

(You could even install an official Linux subsystem in Windows 10 that you can use to run Bash scripts under Windows, but I'm not sure if it includes Avahi.)

Pieter

OK I think you convinced me to get Ubuntu lol. I'm getting it onto a flash drive. Will let you know if I get your script working! :slight_smile:

I know this is an old thread, but just for interests sake...

Running bash scripts on windows is easily achieved with Cygwin ( https://www.cygwin.com/ )

Cygwin provides most GNU Linux/Unix command line stuff to Windows, including bash, emacs, gcc etc
There are also a large number of other packages that can be installed.

Cheers...

The problem is not that Windows doesn't support bash, it's that Windows doesn't have Avahi installed.

You could probably use the Linux sub-system for Windows though, which is probably a much easier solution than Cygwin.