0 min
0

使用 PlatformIO 与 Nix 搭建 ESP32 项目

使用 PlatformIO 与 Nix 搭建 ESP32 项目的简要说明

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

使用 Nix 初始化项目

terminal
mkdir -p ~/test/esp32cd ~/test/esp32nix flake init

为 Nix 配置开发环境

  • 编辑项目中的 `flake.nix` 文件。以下是我个人项目中使用的示例。
nixNIX
1{2  description = "ESP32 + PlatformIO devshell";3  inputs = {4    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";5  };6  outputs = {7    self,8    nixpkgs,9  }: let10    system = "x86_64-linux";11    pkgs = import nixpkgs {inherit system;};12  in {13    devShells.${system}.default = pkgs.mkShell {14      packages = with pkgs; [15        platformio16        python317        gcc18        gnumake19        pkg-config20        picocom21        clang-tools # this for my setup lsp and formatter22      ];23      shellHook = ''24        if [ -z "$ZSH_VERSION" ]; then25          export SHELL=${pkgs.zsh}/bin/zsh26          exec ${pkgs.zsh}/bin/zsh -l27        fi28      '';29    };30  };31}
  • 在项目目录中执行 `nix develop`

使用 PlatformIO 初始化项目

  • 使用 `arduino` 框架时:
terminal
pio project init --board esp32dev --project-option "framework=arduino"
  • 使用 `espidf` 框架时:
terminal
pio project init --board esp32dev --project-option "framework=espidf"

编译项目

terminal
pio run -t compiledb

配置 clangd 以减少 LSP 报错

  • 在项目根目录创建 `.clangd` 文件。
TXT
1CompileFlags:2  Add:3    - "-ferror-limit=0"4    - "-Wno-unknown-attributes"5    - "-Wno-ignored-attributes"6  Remove:7    - "-mlongcalls"8    - "-mtext-section-literals"9    - "-fstrict-volatile-bitfields"10    - "-fno-tree-switch-conversion"11    - "-fno-jump-tables"12    - "-fno-rtti"13    - "-fno-exceptions"14    - "-mfix-esp32-psram-cache-issue"15    - "-mfix-esp32-psram-cache-strategy=*"16    - "-Wno-frame-address"17    - "-Wno-error=unused-but-set-variable"18    - "-freorder-blocks"19  Compiler: clang++20Diagnostics:21  Suppress:22    - pp_file_not_found23    - ovl_no_viable_member_function_in_call24    - ovl_no_viable_function_in_call25    - typecheck_invalid_operands26    - typecheck_nonviable_condition27    - builtin_requires_header28    - implicit_function_decl29  UnusedIncludes: None30  MissingIncludes: None

使用 PlatformIO 时的常用命令

  • 运行 / 烧录 / 监视项目。
terminal
pio run # run projectpio run -t upload # upload projectpio device monitor # monitor device
  • 为项目安装库。可通过命令安装,或在执行 `pio lib install` 后在 `platformio.ini` 中声明依赖。
terminal
pio lib install <LIB_NAME>
INI
1[env:esp32dev]2platform = espressif323board = esp32dev4framework = arduino5monitor_speed = 1152006lib_deps =7  adafruit/Adafruit SSD13068  adafruit/Adafruit GFX Library

参考资料

>Newsletter

Stay Updated

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