ESP32
Learn how BoxLang can be deployed to ESP32 devices.
Building for ESP32
MatchBox supports building and flashing BoxLang scripts directly to ESP32 microcontrollers. This is achieved by cross-compiling a specialized MatchBox runner for the Xtensa or RISC-V architectures and deploying your compiled bytecode to a dedicated flash partition.
Prerequisites
To build for ESP32, you must have the following installed on your development machine:
Rust ESP32 Toolchain: Install using
espup:cargo install espup espup install # Follow instructions to source the export-esp.sh scriptespflash: For flashing the binary to the device. Version 3.3.0+ is required.
ESP-IDF Prerequisites: Standard C build tools, Python, and CMake (required for the
esp-idf-syscrate).
WSL Users (USB Access)
If you are using Windows Subsystem for Linux (WSL), you must "attach" your USB device to the Linux instance using usbipd-win.
From a Windows Administrator PowerShell:
usbipd list
usbipd attach --busid <BUSID> --auto-attachBuilding and Flashing
Use the --target esp32 flag to trigger an ESP32 build. You should always specify your chip type via --chip (e.g., esp32, esp32s3, esp32c3).
1. Initial Setup (Full Flash)
The first time you flash a device, you must perform a "Full Flash." This installs the MatchBox Runner firmware and the custom partition table required for BoxLang.
Note: --full-flash implicitly triggers the flash process.
2. Fast Deployment (Default)
Once the Runner is on the device, you only need to update the BoxLang bytecode. This takes ~1 second and does not require re-flashing the firmware.
Watch Mode (Live Coding)
MatchBox features a built-in watch mode that provides a "Hot Reload" experience for physical hardware.
What Watch Mode does:
Initial Flash: Performs a fast-deploy of your script.
Integrated Monitor: Automatically opens
espflash monitorand performs a hardware reset.Auto-Update: Watches your directory for
.bxschanges. Upon save, it kills the monitor, flashes the new bytecode in 1s, and restarts the monitor/reset cycle.
How it Works
Compilation: Your
.bxsscript is compiled into.bxbbytecode using the Postcard serialization format, ensuring 64-bit to 32-bit architecture compatibility.Partitioning: MatchBox uses a custom partition table (
partitions.csv) that reserves a 1MBstoragepartition at offset0x110000for bytecode.Runtime: The ESP32 Runner starts a dedicated FreeRTOS task with a 48KB stack to host the MatchBox VM.
Environment Awareness: The BoxLang
serverscope is automatically populated with hardware information (e.g.,server.os.archwill returnxtensaorriscv).
Memory and Performance
SRAM: ESP32 devices have limited memory (usually 520KB). The VM is configured with a large stack to prevent overflows, but you should still be mindful of creating massive arrays.
Flash: The VM and runtime add roughly 800KB - 1.2MB to the firmware size. The bytecode is stored separately in the 1MB
storagepartition.
Native Hardware Access
Standard BoxLang BIFs (Built-in Functions) like println are mapped to the ESP32's serial console. To access hardware pins (GPIO, I2C, WiFi), you can use Native Fusion or use a BoxLang module that provides hardware wrappers.
Last updated
Was this helpful?
