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)
});
'ble': Bluetooth Low Energy, on Puck.js and ESP32, with no router. No connections or pairing: every round the message goes into the advertising packet, and the neighbours are the devices heard recently (ttl). Devices listen forscanms per period, which saves battery;minRssiignores distant ones.'udp': WiFi, on ESP32, through a router or one board acting as access point. Withpositionandrange, neighbours are the devices within range; without, everyone on the network is a neighbour.'sim': the simulator, in the browser or in Node.
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.
- Integers are cheaper than decimals, which become 32-bit floats (5 bytes).
- Objects travel as arrays of their values, without the field names:
{dist: 2.8, value: 'a'}takes 9 bytes. - With
xc, each value specific to a neighbour adds about 3 bytes. - A message over 23 bytes is split into up to 4 adverts that the board sends in turn, each with whole operators and 22 bytes of room: about 90 bytes in all. A neighbour that misses one of them keeps those operators' values from the round before. The limit follows what a neighbour hears while listening (
scan, 400 ms, at one advert every 100 ms);chunkschanges it. What does not fit is left out, and the console says so. - Over WiFi (
'udp') there is no such limit.
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:
- yes: arrow functions, template literals, classes,
Math,JSON, array methods; - no: destructuring, spread, default parameters, Map/Set,
for...of, async; - no hoisting: define functions before using them (
var f = () => ...).
The browser simulator is more permissive: the examples on this site all stay within the subset, so they also run on a board.