1 min
250

Setup project esp32 use platformio and nix

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

  • #nix
  • #esp32
  • #platformio
  • #embedded

Init project with nix

terminal
mkdir -p ~/test/esp32cd ~/test/esp32nix 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  };67  outputs = {8    self,9    nixpkgs,10  }: let11    system = "x86_64-linux";12    pkgs = import nixpkgs {inherit system;};13  in {14    devShells.${system}.default = pkgs.mkShell {15      packages = with pkgs; [16        platformio17        python318        gcc19        gnumake20        pkg-config21        picocom22        clang-tools # this for my setup lsp and formatter23      ];2425      shellHook = ''26        if [ -z "$ZSH_VERSION" ]; then27          export SHELL=${pkgs.zsh}/bin/zsh28          exec ${pkgs.zsh}/bin/zsh -l29        fi30      '';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 clang8    - "-mlongcalls"9    - "-mtext-section-literals"10    - "-fstrict-volatile-bitfields"11    - "-fno-tree-switch-conversion"12    - "-fno-jump-tables"13    # ESP-IDF specific14    - "-fno-rtti"15    - "-fno-exceptions"16    - "-mfix-esp32-psram-cache-issue"17    - "-mfix-esp32-psram-cache-strategy=*"18    # GCC-specific warnings clang doesn't know19    - "-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_found27    # Arduino Print class overload resolution28    - ovl_no_viable_member_function_in_call29    - ovl_no_viable_function_in_call30    # Template issues with Arduino libraries31    - typecheck_invalid_operands32    - typecheck_nonviable_condition33    # ESP-IDF macro expansions34    - builtin_requires_header35    - implicit_function_decl36  UnusedIncludes: None37  MissingIncludes: None

Some commnads when use platformio

  • Run/Upload/Monitor project.
terminal
pio run # run projectpio run -t upload # upload projectpio 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 = espressif323board = esp32dev4framework = arduino5monitor_speed = 11520067lib_deps =8  adafruit/Adafruit SSD13069  adafruit/Adafruit GFX Library

References

>Newsletter

Stay Updated

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