Add a flash button to a web page
In this tutorial, you put a <nanoflash-button> on a web page of your own and flash an ESP32-S3
from Chrome, over Web Serial. You reuse the firmware of Flash your first
ESP32-S3 and the JSON manifest of the repository.
What you need
- The ESP32-S3 board and the two images of the first tutorial (
bootloader.binandhello_world.bin). - Chrome, Edge or Opera. Firefox and Safari do not implement Web Serial.
- mise and Python 3. The tutorial builds the package from the repository,
next to the example manifest. In your own project,
npm install nanoflashgives the same package.
1. Build the browser package
$ git clone https://github.com/AlixANNERAUD/nanoflash
$ cd nanoflash
$ mise install
$ mise run web-wasm
$ npm --prefix web ci
$ npm --prefix web run build
web-wasm compiles the library to WebAssembly modules, one per protocol. npm run build compiles
the TypeScript package into web/dist, with the modules in web/dist/wasm.
2. Put the firmware next to the manifest
The repository has a JSON manifest for the ESP32-S3-DevKitC-1, hardware/esp32-s3/manifest.json. It
names firmware/bootloader.bin and firmware/hello_world.bin. Copy your images there:
$ mkdir -p hardware/esp32-s3/firmware
$ cp ~/esp32-s3-hello/firmware/*.bin hardware/esp32-s3/firmware/
Git ignores this directory.
3. Write the page
Create flash-page/index.html at the root of the repository:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Flash my board</title>
<style>
nanoflash-button { --nanoflash-button-background: #1f6feb; }
</style>
</head>
<body>
<h1>Flash my board</h1>
<nanoflash-button id="button" manifest="../hardware/esp32-s3/manifest.json" label="Install"></nanoflash-button>
<pre id="log"></pre>
<script type="module">
import "../web/dist/button.js";
const button = document.getElementById("button");
const log = document.getElementById("log");
for (const name of ["nanoflash-done", "nanoflash-error"]) {
button.addEventListener(name, (event) => {
const detail = event.detail instanceof Error ? event.detail.message : JSON.stringify(event.detail);
log.textContent += `${name} ${detail}\n`;
});
}
</script>
</body>
</html>
In a project that installs the package from npm, the import is import "nanoflash/button";.
4. Serve the page
A page needs a secure origin to use Web Serial, and localhost is one. From the root of the
repository:
$ python3 -m http.server 8000 --bind 127.0.0.1
Open http://localhost:8000/flash-page/ in Chrome.
5. Flash
- Connect the board.
- Press Install. Chrome shows the ports that match the USB identity of the manifest.
- Choose
USB JTAG/serial debug unit. - Watch the progress. The button ends in the
donestate, and the log shows the report ofnanoflash-done.
The page used the same Rust code as the command, compiled to WebAssembly: it checked the images, generated the partition table from the layout, wrote and verified the flash.
What you learned
<nanoflash-button>needs one attribute, the URL of a JSON manifest.- The browser asks the user for the port. Only Chromium-based browsers on a secure origin can flash.
- The events
nanoflash-doneandnanoflash-errortell the page what happened.
Next steps
- Use the browser package from your own code, and for Arduino boards with a second permission.
- Use the flasher page to flash without writing a page.
- Browser API for the attributes, states, events and styles.