diff --git a/README.md b/README.md index 0f294b9..57b9bc9 100644 --- a/README.md +++ b/README.md @@ -96,10 +96,26 @@ git clone https://github.com/MrSossidge/victron-esp32-ble-mqtt.git cd victron-esp32-ble-mqtt ``` -### 2. Install VS Code + PlatformIO +### 2. Install PlatformIO -Install [VS Code](https://code.visualstudio.com) and the **PlatformIO IDE** extension, then -open the project folder — PlatformIO reads `platformio.ini` and pulls dependencies itself. +Use either the VS Code extension (handy for interactive work) or the CLI in a +Python virtual environment: + +- **VS Code** — install [VS Code](https://code.visualstudio.com) and the + **PlatformIO IDE** extension. +- **CLI** — PlatformIO needs a Python environment: + + ```bash + sudo apt install python3-venv # Debian/Ubuntu, only if missing + python3 -m venv ~/.venvs/pio + source ~/.venvs/pio/bin/activate + python -m pip install --upgrade pip platformio + pio --version + ``` + + The venv isolates the `pio` CLI only; PlatformIO still downloads the ESP32 + toolchain and libraries into `~/.platformio` (a few hundred MB on the first + build). ### 3. Configure @@ -146,12 +162,138 @@ so its entities and discovery topics do not collide with the first one. ### 4. Build and upload +With the venv active (or call `~/.venvs/pio/bin/pio` directly): + +```bash +pio run # compile only +pio run -t upload # compile + flash +pio device monitor -b 115200 # serial log at 115200 baud +pio run -t upload -t monitor # flash, then open the monitor +``` + +In VS Code the PlatformIO status bar does the same: + | Action | Shortcut | |---|---| | Build | `Ctrl+Alt+B` | | Upload | `Ctrl+Alt+U` | | Serial monitor | `Ctrl+Alt+S` | +#### Build output + +Everything is written to the hidden `.pio/build/esp32dev/` directory: + +``` +.pio/build/esp32dev/ +├── firmware.bin # application image (the file you flash / ship via OTA) +├── bootloader.bin # ESP32 bootloader +├── partitions.bin # partition table +├── firmware.elf # ELF with debug symbols +├── firmware.map # linker map +└── src/ # compiled objects for main.cpp and victron_ble.cpp +``` + +- `.pio/` is gitignored, so build artefacts are never committed. +- `pio run -t upload` flashes all three images automatically at the usual + offsets: `bootloader.bin` at `0x1000`, `partitions.bin` at `0x8000`, + `firmware.bin` at `0x10000`. +- For OTA updates or a manual `esptool` write, `firmware.bin` is the image + you need. +- `firmware.elf` is used to decode crash backtraces, e.g. + `xtensa-esp32-elf-addr2line -pfiaC -e .pio/build/esp32dev/firmware.elf `, + or with PlatformIO's exception decoder in the serial monitor. + +#### Build notes + +- `platformio.ini` pins `espressif32@6.13.0` (Arduino core 2.0.17). It is the + last platform compatible with NimBLE-Arduino 1.4.x — do not bump it without + migrating to NimBLE-Arduino 2.x. +- Dependencies are NimBLE-Arduino 1.4.x, PubSubClient 2.8.x and ArduinoJson + 6.21.x, resolved by PlatformIO from `lib_deps`. +- A clean build takes a few minutes and needs network access; rebuilds only + compile changed files. +- Reference sizes with the default 4 MB partition table: about **77 % flash** + and **17 % RAM** used. Disabling `HA_DISCOVERY_ENABLED` frees ~4.6 KB of flash. +- Clean rebuild: `pio run -t clean`. + +#### Upload troubleshooting + +- `fatal error: config.h: No such file or directory` — run step 3 first. +- Upload times out at 921600 baud — lower `upload_speed` in `platformio.ini` + to `460800` (or `115200`). +- `Permission denied: /dev/ttyUSB0` — `sudo usermod -aG dialout $USER`, then + log out and back in. On Windows, install the CH340 driver. +- Board not detected — hold **BOOT** while the upload starts (needed on some + ESP32-C3 SuperMini boards). + +#### Manual flashing with esptool + +`pio run -t upload` drives esptool internally. For recovery, a board without +PlatformIO, or a machine that only has the pre-built images, flash the three +partitions yourself. + +```bash +pip install esptool +``` + +esptool **5.x** installs the `esptool` command and uses hyphenated subcommands +(`write-flash`, `erase-flash`, `merge-bin`). The older esptool **4.x** used +`esptool.py` with underscores (`write_flash`); that still works but prints +`Command 'write_flash' is deprecated`, so prefer the 5.x form below. + +Port names: Linux `/dev/ttyUSB0`, macOS `/dev/cu.usbserial-XXXX`, Windows `COM5`. + +Linux / macOS: + +```bash +esptool --chip esp32 --port /dev/ttyUSB0 --baud 460800 write-flash \ + 0x1000 .pio/build/esp32dev/bootloader.bin \ + 0x8000 .pio/build/esp32dev/partitions.bin \ + 0x10000 .pio/build/esp32dev/firmware.bin +``` + +Windows PowerShell (backtick continues the line): + +```powershell +esptool --chip esp32 --port COM5 --baud 460800 write-flash ` + 0x1000 .pio\build\esp32dev\bootloader.bin ` + 0x8000 .pio\build\esp32dev\partitions.bin ` + 0x10000 .pio\build\esp32dev\firmware.bin +``` + +Erase the whole flash first if the board is in a bad state or has a different +partition table: + +```bash +esptool --chip esp32 --port /dev/ttyUSB0 erase-flash +``` + +Optional: merge everything into one factory image (handy for archiving or +sharing a known-good build): + +```bash +esptool --chip esp32 merge-bin \ + --flash-mode dio --flash-freq 40m --flash-size 4MB \ + -o factory.bin \ + 0x1000 .pio/build/esp32dev/bootloader.bin \ + 0x8000 .pio/build/esp32dev/partitions.bin \ + 0x10000 .pio/build/esp32dev/firmware.bin + +esptool --chip esp32 --port /dev/ttyUSB0 --baud 460800 write-flash 0x0 factory.bin +``` + +Notes: + +- These offsets assume the default PlatformIO `esp32dev` 4 MB layout. If you + changed the partition table, flash `partitions.bin` and `firmware.bin` using + the offsets from that table instead. +- 921600 baud works on most boards; drop to 460800 or 115200 for flaky CH340 + clones. +- If esptool reports `Failed to connect`, hold **BOOT** while it tries, and + make sure the serial monitor is closed. +- `firmware.bin` alone is what an OTA update expects; the bootloader and + partition table are only written over USB. + --- ## MQTT topics