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
- One or more classic ESP32 boards, such as an ESP32-WROOM DevKit. Espruino's ESP32 firmware targets this chip; the official documentation does not cover C3 and S3.
- A USB data cable, not a charge-only one.
- Chrome or Edge, to flash the firmware and upload programs from the browser.
1. Install Espruino
ESP32 boards ship without Espruino: install it once.
- Download the ESP32 firmware from the Espruino download page. It contains
bootloader.bin, partitions_espruino.bin and espruino_esp32.bin.
- 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 ↗:
- choose Bluetooth or WiFi, and for WiFi type the network name and password;
- tick Save if the program should start at every power-up;
- 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:
- in Settings → Communications set the Baud Rate to 115200: the ESP32 does not use the default;
- in the same panel add to Module URL, separated by
|, the modules address shown in the playground's To device dialog;
- copy the file from that dialog, paste it in the editor and press Upload.
When something goes wrong
- The board does not show up as a port: try another cable; some boards need the CP210x or CH340 driver.
- WiFi errors or reboots: often the power supply. A weak USB port or regulator is not enough when WiFi transmits.
- Boards do not see each other on WiFi: some routers isolate clients or filter broadcasts. Try a hotspot, or one ESP32 as access point.
- "message is N bytes" on the console: over Bluetooth the message does not fit even in 4 adverts (about 90 bytes), and part of it is left out. Use fewer operators or small integers, or switch to WiFi.