8 · On devices

The program you wrote in the simulator runs as is on Puck.js and ESP32, with Espruino. Only where you start it changes.

Where it runs: start

On a device the file ends with start(program, config). The program does not know which network it runs on: config.target decides.

require('nbrjs').globals();

var hops = (src) => share(Infinity, (d) => src ? 0 : d.min() + 1);
var program = () => hops(sense('source'));

start(program, {
  target: 'ble',
  period: 1000,
  sensors: () => ({ source: BTN1.read() }),
  onResult: (h) => LED1.write(h === 0)
});

Small messages

An advertising packet has 23 usable bytes. Over Bluetooth, messages use a compact binary format: each operator costs 2 bytes plus its value, and an integer from 0 to 63 fits in a single byte. The gradient below sends 4 bytes: hover a node to see its message and size.

To see what a program sends over Bluetooth, and how it copes with lost messages, see chapter 9.

Uploading

From the playground, in Chrome or Edge: To device ↗, pick the network, then Upload via Bluetooth or via USB. The first time, the library is installed in the board's flash memory, where it runs without using much RAM; after that only the program travels. With Save the program starts by itself at every power-up.

Prefer the Espruino Web IDE? In Settings → Communications → Module URL add the modules address shown in the same dialog: require('nbrjs') then works like an official module.

For ESP32, from installing Espruino to choosing between Bluetooth and WiFi, see the ESP32 guide. With a single Puck.js, the Puck.js demo makes it part of a simulated network.

Espruino's JavaScript

Espruino interprets the source directly and supports a subset of JavaScript:

The browser simulator is more permissive: the examples on this site all stay within the subset, so they also run on a board.