1 min
3

Setup project esp32 use platformio and nix

A note about how to setup project esp32 use platformio and nix

  • #nix
  • #esp32
  • #platformio
  • #embedded
By Baiyuechu@github/baiyuechuz

Setup project esp32 use platformio and nix

0:00
-0:00

Init project with nix

terminal
mkdir -p ~/test/esp32
cd ~/test/esp32
nix flake init

Setup environment for nix

  • Edit file `flake.nix` in project. This is a example i use for my project.
nixNIX
1{
2  description = "ESP32 + PlatformIO devshell";
3  inputs = {
4    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
5  };
6
7  outputs = {
8    self,
9    nixpkgs,
10  }: let
11    system = "x86_64-linux";
12    pkgs = import nixpkgs {inherit system;};
13  in {
14    devShells.${system}.default = pkgs.mkShell {
15      packages = with pkgs; [
16        platformio
17        python3
18        gcc
19        gnumake
20        pkg-config
21        picocom
22        clang-tools # this for my setup lsp and formatter
23      ];
24
25      shellHook = ''
26        if [ -z "$ZSH_VERSION" ]; then
27          export SHELL=${pkgs.zsh}/bin/zsh
28          exec ${pkgs.zsh}/bin/zsh -l
29        fi
30      '';
31    };
32  };
33}
  • Run command `nix develop` in project.

Init project with platformio

  • For `arduino` framework:
terminal
pio project init --board esp32dev --project-option "framework=arduino"
  • For `espidf` framework:
terminal
pio project init --board esp32dev --project-option "framework=espidf"

Compile project

terminal
pio run -t compiledb

Setup clangd to reduce err of lsp

  • Create file `.clangd` in root of project.
TXT
1CompileFlags:
2  Add:
3    - "-ferror-limit=0"
4    - "-Wno-unknown-attributes"
5    - "-Wno-ignored-attributes"
6  Remove:
7    # Xtensa-specific flags unsupported by clang
8    - "-mlongcalls"
9    - "-mtext-section-literals"
10    - "-fstrict-volatile-bitfields"
11    - "-fno-tree-switch-conversion"
12    - "-fno-jump-tables"
13    # ESP-IDF specific
14    - "-fno-rtti"
15    - "-fno-exceptions"
16    - "-mfix-esp32-psram-cache-issue"
17    - "-mfix-esp32-psram-cache-strategy=*"
18    # GCC-specific warnings clang doesn't know
19    - "-Wno-frame-address"
20    - "-Wno-error=unused-but-set-variable"
21    - "-freorder-blocks"
22  Compiler: clang++
23Diagnostics:
24  Suppress:
25    # Missing Arduino/ESP headers (framework quirks)
26    - pp_file_not_found
27    # Arduino Print class overload resolution
28    - ovl_no_viable_member_function_in_call
29    - ovl_no_viable_function_in_call
30    # Template issues with Arduino libraries
31    - typecheck_invalid_operands
32    - typecheck_nonviable_condition
33    # ESP-IDF macro expansions
34    - builtin_requires_header
35    - implicit_function_decl
36  UnusedIncludes: None
37  MissingIncludes: None

Some commnads when use platformio

  • Run/Upload/Monitor project.
terminal
pio run # run project
pio run -t upload # upload project
pio device monitor # monitor device
  • Install lib for project. You can install by command or declare in `platformio.ini` after use `pio lib install`.
terminal
pio lib install <LIB_NAME>
INI
1[env:esp32dev]
2platform = espressif32
3board = esp32dev
4framework = arduino
5monitor_speed = 115200
6
7lib_deps =
8  adafruit/Adafruit SSD1306
9  adafruit/Adafruit GFX Library

References

Join Baiyuechu Newsletter!

Get notified when I publish new articles, tutorials, and project updates. Subscribe for insights and actionable content.