Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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.bin and hello_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 nanoflash gives 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

  1. Connect the board.
  2. Press Install. Chrome shows the ports that match the USB identity of the manifest.
  3. Choose USB JTAG/serial debug unit.
  4. Watch the progress. The button ends in the done state, and the log shows the report of nanoflash-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-done and nanoflash-error tell the page what happened.

Next steps