Skip to content
Max Prokhorov edited this page Nov 13, 2020 · 39 revisions

Build & Flash the Firmware with PlatformIO

Installing PlatformIO

NOTICE: Python 3.6 is the minimum supported version.

Refer to the latest platformio-core documentation and follow the instructions for your platform: https://docs.platformio.org/en/latest/core/installation.html

By default, PlatformIO does not add pio / platformio command to the system PATH variable: https://docs.platformio.org/en/latest/core/installation.html#piocore-install-shell-commands

Building

For example, to build a binary for Sonoff Basic:

$ pio run -e sonoff-basic

The resulting .bin file can be found at .pio/build/sonoff-basic/firmware.bin

Or, using special generic environment:

$ env ESPURNA_FLAGS="-DITEAD_SONOFF_BASIC" pio run -e esp8266-1m-base

(including any other flags / environment variables, depending on the board you are working with)

For Window's cmd.exe, syntax is a bit different:

> cmd /V /C "set ESPURNA_FLAGS=-DITEAD_SONOFF_BASIC && pio run -e esp8266-1m-base"

Flash your board

Notice This describes serial upload, see OTA page for information about wireless upload and PlatformIO documentation

Wire your board (check the Hardware page) and flash the firmware (with upload):

$ env ESPURNA_FLAGS="-DITEAD_SONOFF_BASIC" pio run -e esp8266-1m-base -t upload

Libraries

PlatformIO will take care of the library dependencies. The first time you run the build process it will fetch and install all the libraries required by ESPurna in the specified versions.

For the most up-to-date list, please see code/platformio.ini lib_deps = ... in the [common] section.

Core libraries:

Optional libraries, depending on specific ..._SUPPORT flags:

Button management and settings storage inspired by:

At last, legacy libraries for Core 2.3.0 (deprecated, only matters when building using platform = [email protected]):

Customize build settings

platformio.ini format

Please refer to the official PlatformIO documentation for platformio.ini: https://docs.platformio.org/en/latest/projectconf/

ESPurna's platformio.ini has the following structure:

  • code/espurna/ is set as source code root directory
  • code/platformio_override.ini can be used to override existing or create new sections.
  • [common] sections is used to set some variables that later will be used as ${common.variable}
  • [env] section implicitly provides keys for every environment, so we don't have to repeat ourselves
  • [env:...-base] specifies framework (arduino, only available option), board (upload settings, variant build flags), ldscript (memory regions, flash constants and flash size), platform (PlatformIO build system version) and platform_packages (in case we need to change build tools versions)
  • [env:named] extends from the -base environment and sets src_build_flags with our project-specific flags. Common way to specify hardware is to have #if defined(HARDWARE_FLAG) (see code/espurna/config/hardware.h') and add -DHARDWARE_FLAGwhen you need to enable multiple#define` options.

It is important to note that in case of repeating keys:

[env:example]
extends = esp8266-1m-base
src_build_flags = -DMY_CUSTOM_HARDWARE -DUSE_CUSTOM_H

[env:override]
extends = env:example
src_build_flags = -DANOTHER_HARDWARE

When building env:override, -DUSE_CUSTOM_H will not be added to the compiler build flags. To append to the existing variable you would need to explicitly specify the key that you want it to use. For example:

[env:example]
extends = esp8266-1m-base
src_build_flags = -DMY_CUSTOM_HARDWARE -DUSE_CUSTOM_H

[env:override]
extends = esp8266-1m-base
src_build_flags = ${env:example.src_build_flags} -DSOME_OTHER_FLAG=1

build_flags and src_build_flags

  • src_build_flags is used only by our project's files
  • build_flags sets command line options for project's files, Core and libraries

For example, our [env] sections contains the following flags:

build_flags = -g -w -DNO_GLOBAL_EEPROM -DPIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH

Where:

Any environment that wants to modify build flags needs to explicitly specify the existing build_flags variable as build_flags = ${common.build_flags} -DLIBRARY_CUSTOM_FLAG=12345 -DPIO_FRAMEWORK_ARDUINO_ESPRESSIF_SDK221.

Custom environment

If you want to modify the stock configuration but you don't want to touch the repo files you can create a brand new environment in the code/platformio_override.ini:

[env:my-environment]
extends = esp8266-1m-base
src_build_flags = -DITEAD_SONOFF_BASIC -DDHT_SUPPORT=1

custom.h

Add -DUSE_CUSTOM_H to src_build_flags and create code/espurna/config/custom.h. For example:

#define WEB_EMBEDDED 0
#define NTP_SERVER              "it.pool.ntp.org"
#define MQTT_TOPIC              "my/topic"
#define DOMOTICZ_ENABLED 1
#define ALEXA_SUPPORT 0

(see code/espurna/config/general.h and code/espurna/config/sensor.h for available flags)

It is also possible to add flag through command line by setting OS environment variables PLATFORMIO_SRC_BUILD_FLAGS and PLATFORMIO_BUILD_FLAGS, for src_build_flags and build_flags respectively:

export PLATFORMIO_SRC_BUILD_FLAGS="-DUSE_CUSTOM_H"

We also provide a ESPURNA_FLAGS OS environment variable that is used by the OTA:

export ESPURNA_FLAGS="-DUSE_CUSTOM_H"

ESPURNA_FLAGS is interchangeable with the PLATFORMIO_SRC_BUILD_FLAGS and both will append flags into the existing src_build_flags contents.

Advanced options

-DDISABLE_POSTMORTEM_STACKDUMP (build_flags / src_build_flags option)

Used to disable serial print of postmortem stack dump after a board crashes. This functionality is part of the Arduino Core, so we build a dummy file with an empty printf function and replace the reference to printf in the resulting .o file. Kind of a hack, this should probably be configured in the Core itself. But, we need to write a patch for that.

ESPURNA_PIO_PATCH_ISSUE_1610 (OS environment variable)

Temporary solution for the https://github.com/xoseperez/espurna/issues/1610, ATM this option is only used in CI to build release .bin files

ESPURNA_PIO_SHARED_LIBRARIES (OS environment variable)

Before building the source, PIO build system will call pio lib -d <destination dir> install <platformio.ini lib_deps contents>. This was introduces as a workaround for the following change in PlatformIO 4.0:

  • Library Management
    Switched to workspace .pio/libdeps folder for project dependencies instead of .piolibdeps"
  • In CI, destination dir is set to ~/.platformio/lib
  • Otherwise, destination is set to ${common.shared_libdeps_dir}

ESPURNA_BUILD_SINGLE_SOURCE (OS environment variable)

Combine every .cpp into a single file to significantly speed up compilation process. This will also result in a reduced .bin size (~7KB smaller). ATM this option is only used in CI to build release .bin files

ESPURNA_FLAGS, ESPURNA_IP, ESPURNA_AUTH, ESPURNA_BOARD (OS environment variables)

  • ESPURNA_IP flags upload type as OTA (when using -t upload) and adds --ip=$ESPURNA_IP to the espota.py arguments list
  • ESPURNA_AUTH adds --auth=$ESPURNA_AUTH to the espota.py arguments list (depends on ESPURNA_IP)
  • ESPURNA_FLAGS adds it's contents into the projects build flags (modify src_build_flags for the same result)
  • (legacy) ESPURNA_BOARD updates build flags with -D$ESPURNA_BOARD. This was used previously as a platformio.ini substitution, but is no longer used and will be removed in the future.

1 See MQTT configuration.
2 See I2C configuration.
3 See Sensors configuration.
4 See Lights configuration.
5 See RPN Rules configuration.
6 See RFBridge configuration.

Home
Change log

Getting started

Supported hardware and options

Configuration

Integrations

Network

Developers

More around ESPurna

Clone this wiki locally