NbrJS on ESP32

From a board fresh out of the box to a group of ESP32s, robots or sensors, computing together over Bluetooth or WiFi.

What you need

1. Install Espruino

ESP32 boards ship without Espruino: install it once.

  1. Download the ESP32 firmware from the Espruino download page. It contains bootloader.bin, partitions_espruino.bin and espruino_esp32.bin.
  2. Connect the board and flash them with Espruino's online tool, or from a terminal with esptool:
esptool.py --chip esp32 --port /dev/ttyUSB0 --baud 921600 --after hard_reset \
  write_flash -z --flash_mode dio --flash_freq 40m --flash_size detect \
  0x1000 bootloader.bin 0x8000 partitions_espruino.bin 0x10000 espruino_esp32.bin

On Windows the port looks like COM3, on macOS like /dev/cu.usbserial-…. Later updates only need 0x10000 espruino_esp32.bin.

2. Choose the network

ESP32 boards can talk over Bluetooth or WiFi. ESP-NOW, Espressif's direct protocol, is not available in Espruino.

Bluetooth: target: 'ble'

No infrastructure: each board hears the ones nearby, Puck.js included, so the network follows the space. Messages of 23 bytes per advert, up to 4 adverts sent in turn. nbrSense('range') estimates distance from the signal; minRssi sets how far "near" is.

WiFi: target: 'udp'

Boards join the same access point and broadcast to each other, with no practical limit on messages. Robots that know their position pass position and range, and get a spatial neighbourhood with real distances. A PC running Node can join too.

No router around? One ESP32 can be the access point for the others:

require('Wifi').startAP('swarm', { password: 'swarm-pass', authMode: 'wpa2' });

Turn off the radio you do not use: it frees a lot of memory for your programs. The command restarts the board and clears the saved program.

ESP32.enableWifi(false);   // on Bluetooth: about 3900 more variables
ESP32.enableBLE(false);    // on WiFi: about 1800 more variables

3. Upload the program

Open the playground, write or pick a program, and press To device ↗:

  1. choose Bluetooth or WiFi, and for WiFi type the network name and password;
  2. tick Save if the program should start at every power-up;
  3. press Upload via USB and pick the board's port.

The first time the page installs the library in the board's flash, then uploads the program. Repeat for each ESP32.

This is the file that ends up on a WiFi robot. The BOOT button is BTN1 and the on-board LED is LED1, though they can differ on compatible boards:

require('nbrjs').globals();
require('nbrjs/lib').globals();

var program = () => hopsTo(sense('source'));

start(program, {
  target: 'udp',
  wifi: { ssid: 'swarm', password: 'swarm-pass' },
  position: () => myRobot.position(),   // [x, y] in metres, from your robot's localisation
  range: 1.5,                           // neighbours within 1.5 m
  period: 500,
  sensors: () => ({ source: BTN1.read() }),
  onResult: (h, id) => LED1.write(h === 0)
});

Without position, every board on the WiFi network is a neighbour of every other.

With the Espruino Web IDE

If you prefer Espruino's IDE:

  1. in Settings → Communications set the Baud Rate to 115200: the ESP32 does not use the default;
  2. in the same panel add to Module URL, separated by |, the modules address shown in the playground's To device dialog;
  3. copy the file from that dialog, paste it in the editor and press Upload.

When something goes wrong