Skip to main content
nerves_hub_link_atomvm_esp32 is a NervesHub client for AtomVM on the ESP32. It is written in Erlang and you can call it from Erlang or Elixir. The firmware it manages is the application’s packbeam archive, not AtomVM itself. This page describes version 0.2.0 and notes where the main branch differs.
A device needs AtomVM built from source. The agent’s WebSocket transport is an ESP-IDF component, so a stock AtomVM build cannot connect to NervesHub.

Requirements

Build the VM

The AtomVM build needs up to three changes: Without the two partitions a device connects and reports its firmware but cannot be updated. Without libsodium a device configured with firmware_keys reports verification_unavailable and does not install the update. The agent ships the files for the last two changes in priv/atomvm. After rebar3 compile in your project they are in _build/default/lib/nerves_hub_link_atomvm_esp32/priv/atomvm:
Replace <AtomVM> with your AtomVM checkout and <transport> with your checkout of atomvm_websocket_client.
Use idf.py set-target, not reconfigure. CMake caches its component list, and a plain idf.py build after adding a component reports success without compiling it.

Flash the VM and the application

Then write your application’s packbeam to the main.avm partition. Read the offset from the device’s own partition table:
Writing to an offset from a stale copy of the table puts the application inside boot.avm. The only symptom is Failed app start: invalid_avm.

The partition table

main.avm and alt.avm must be the same size, because either one has to hold the archive. boot.avm and factory are larger than in stock AtomVM, to fit the Elixir boot archive and libsodium. priv/atomvm/partitions.csv explains each size.

Install

rebar.config
The transport is a git dependency because it is not published on Hex. It must also be compiled into the VM.

Connect

Set the device clock before you start the agent, for example with SNTP. A shared-secret signature is valid for 90 seconds, and an ESP32 boots with its clock at the epoch. start/1 returns {error, {clock_not_set, Now}} if the clock is not set.
With no host or url, the agent connects to NervesCloud. Set one of them for another instance. Setting both is an error.

Options

These are the options you are most likely to set. The repository’s README lists all of them. One of shared_secret and client_cert is required.

Handle events

The agent sends {nerves_hub, Event} to the process that started it, or to handler.
New events are added between versions. If your handler is a gen_server, give handle_info/2 a catch-all clause.

Updates

With updates => auto, the agent downloads the packbeam NervesHub offers into whichever of main.avm and alt.avm the device is not running from. It then arms that partition and sends {update_ready, Slot}. Your application decides when to reboot.

Firmware on trial

After an update, the new firmware is on trial until it joins NervesHub. It has three boots to join, and five minutes on each. If it uses up either, the device points back at the previous firmware and restarts into it. That firmware then reports to NervesHub that an update was reverted. Start the agent early in your application. A firmware that crashes before it calls nerves_hub_link:start/1 is not counted. firmware_trial => off turns the trial off.

Deciding for yourself

With updates => manual, the offer arrives as {message, <<"update">>, Payload}. Answer it with one of:
The agent also supports device-managed updates:

Verifying signatures on the device

Set firmware_keys to your organization’s fwup public keys. A device with keys refuses any update the keys do not cover, including an unsigned one, before it changes the boot partition. A device with no keys installs what it is sent. Verification needs AtomVM built with AVM_USE_LIBSODIUM=ON. See AtomVM packbeam archives for how to sign an archive.

Console, scripts and extensions

  • Console. AtomVM has no shell, so console => true serves a fixed set of commands: help, info, firmware, memory, partitions, net, geo, signature, uptime and reboot. It does not evaluate Erlang.
  • Support scripts. A script is a list of console commands, one per line. Blank lines and lines that start with # are skipped. The first line that is not a command fails the script, so a script written in Elixir for Nerves devices reports a failure. reboot is refused in a script.
  • Extensions. Version 0.2.0 implements health, geo and logging. The product and the device must also have the extension enabled in NervesHub.
AtomVM’s logger raises badarg when given a binary message, which crashes the calling process. From Elixir, pass a charlist or a map to :logger, or call nerves_hub_link:send_log/3.

What is on main but not in 0.2.0

The main branch has unreleased additions, listed in the repository’s changelog. They include an alarms extension, custom console commands (console_commands), and keep_open_above and before_update for devices with too little memory to hold two TLS sessions during a download.

What the device reports

The device does not send a UUID. NervesHub derives it from the SHA-256, by the same rule it uses at upload, so the device matches the firmware you uploaded only if it runs the same bytes.