Build Arduino sketches from command-line and flash to ESP8266 from Pi


I want to build sketches from the command line so that I don’t have to use the Arudino IDE to compile. Then I want to use the esptool to flash the files to the ESP manually. My ESP is connected to a remote Raspberry Pi, on my desk, so this is a good step towards a faster build process where I can build on my (fast) computer, then transfer the files to the Pi for it to my ESP 12-F chip.

Perform initial Arduino installation

Find your board FQBN by searching in the files. My NODEMCU 1.0 (ESP-12E Module) FQBN is esp8266:esp8266:nodemcuv2

Install Arduino IDE on your main machine

# pacman -S arduino

Start the Arduino IDE, go into preferences and paste the ESP8266 URL for “Additional Boards URL” and click Ok.

Open the Board Manager and search for esp8266. Install that package, now exit Arduino IDE.

I’ve installed arduino-builder from the official Arch repo

# pacman -S arduino-builder

Crate a new project directory somewhere and change to it

$ mkdir -p ~/esp-project/blink; cd ~/esp-project/blink

Create some directories to house the different files

$ mkdir src build .cache

Create the test file to make the LED blink every 777 millisecond

$ cat <<EOF > src/led_blink.ino
const short int LED = 2; // GPIO 2
const short int DELAY = 777; // ms

void setup() {
  pinMode(LED, OUTPUT);

void loop() {
  digitalWrite(LED, LOW);
  digitalWrite(LED, HIGH);

Create a build.options.json file to simplify the build command

$ cat <<EOF > build.params.json
  "fqbn": "esp8266:esp8266:nodemcuv2:CpuFrequency=80,FlashSize=4M1M,LwIPVariant=v2mss536,Debug=Disabled,DebugLevel=None____,FlashErase=none,UploadSpeed=115200",
  "builtInLibrariesFolders": "",
  "hardwareFolders": "/usr/share/arduino/hardware,${HOME}/.arduino15/packages",
  "toolsFolders": "/usr/share/arduino/tools-builder,${HOME}/.arduino15/packages",
  "otherLibrariesFolder": "${HOME}/Arduino/libraries",
  "customBuildProperties": "build.warn_data_percentage=75,${HOME}/.arduino15/packages/esp8266/tools/mkspiffs/0.2.0,${HOME}/.arduino15/packages/esp8266/tools/esptool/0.4.13,${HOME}/.arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/1.20.0-26-gb404fb9-2",
  "additionalFiles": "",
  "runtime.ide.version": "10805"

I can now build my project

$ arduino-builder -compile -warnings=default -verbose -logger=humantags -build-options-file=$(pwd)/build.options.json -build-path=$(pwd)/build -build-cache=$(pwd)/.cache $(pwd)/src/led_blink.ino

Transfer the files to the Pi

I’m connected over SSH, so I’m going to use rsync to transfer the files over.

$ rsync build/led_blink.ino.bin pi@

Flash the chip using esptool

** This section is performed on the Raspberry Pi**

Note that I’m running the latest raspbian as of June 1st 2018, which means /dev/serial0 is a symlink to whichever serial is available for me out of /dev/ttyAMA0 and /dev/ttyS0. If you’re on an older system, mind the device path in the commands.

I’m not using the raspbian apt repo for esptool, as the version there is outdated. Instead, download esptool using pip (pip3 since it’s the python3 pip we want) for this. For this post, I’m using version 2.3.1 as it was the latest one when I wrote this.

$ sudo apt update
$ sudo apt install python3-pip
$ sudo pip3 install esptool==2.3.1

Find the led_blink.ino.bin file that was compiled and transfered in the previous step and specify that in the flash command

$ --baud 115200 --port /dev/serial0 write_flash --flash_mode qio 0x00000 led_blink.ino.bin v2.4.0-dev
Detecting chip type... ESP8266
Chip is ESP8266EX
Features: WiFi
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Auto-detected Flash size: 4MB
Flash params set to 0x0040
Compressed 250400 bytes to 182663...
Wrote 250400 bytes (182663 compressed) at 0x00000000 in 16.4 seconds (effective 122.1 kbit/s)...
Hash of data verified.

Hard resetting via RTS pin...

When I’ve been using the Arduino IDE manually, the chip has reset by itself. Now, however, I have to reset in manually either by pulling the plug, or pushing the button on my prototype board.