Device libraries

Own the application. Reuse the device plumbing.

Use the Arduino/PlatformIO SDK on a standalone ESP32, or keep control code on an STM32-class MCU and carry the same variables, functions, events, commands, and OTA lifecycle over NivaloLink.

ESP32 · Arduino / PlatformIO

Use NivaloDevice

The browser quickstart installs a reviewed factory image containing NivaloDevice and NivaloProvisioning; it does not require repository access. For custom firmware, use the immutable SDK bundle supplied with the customer release and pin its exact version or commit in the product build. A floating branch is not a reproducible dependency.

#include <NivaloDevice.h>
#include <NivaloProvisioning.h>

NivaloDevice device;
NivaloProvisioning provisioning;

// Runtime claim data populates NivaloDeviceConfig.
// No Wi-Fi or MQTT password belongs in the sketch.

Factory firmware writes Wi-Fi and MQTT credentials at runtime instead of compiling them into the application. Public MQTT uses certificate-validating TLS on mqtt.nivalo.io:8883; plaintext is restricted to explicit local broker transports.

Application API

Register behavior before connecting

#include <NivaloDevice.h>

NivaloDevice device;
float temperatureC = 0;
bool ledState = false;

bool validSetLed(JsonObjectConst arguments) {
  return arguments.size() == 1U && arguments["state"].is<bool>();
}

NivaloFunctionOutcome setLed(JsonObjectConst arguments) {
  ledState = arguments["state"].as<bool>();
  return NIVALO_FUNCTION_SUCCEEDED;
}

void registerApplication() {
  NivaloFunctionMetadata metadata;
  metadata.displayName = "Set LED";
  metadata.argumentExample = "{\"state\":true}";
  metadata.validateArguments = validSetLed;
  device.function("setLed", setLed, metadata);
  device.variable("temperature", &temperatureC, "C");
  device.variable("ledState", &ledState);
}
APIBehavior
device.function(name, handler, metadata)Adds a function to the device's capability catalog. The handler runs at most once per operation and reports succeeded, failed (no effect), unknown (may have had an effect) or pending.
device.variable(name, &value, unit)Adds a typed variable to the catalog. The referenced value must remain valid for the device lifetime.
device.addTelemetry(name, value, unit), device.flushTelemetry()Batches readings into samples (up to 8 samples and 24 readings per message) and publishes them.
device.publishEvent(name, data, severity)Publishes a structured event through the versioned envelope.
device.loop()Advances reconnect, presence heartbeats, watchdog, the command journal, OTA, and optional NivaloLink work without a blocking reconnect loop.

A reported function becomes callable from the console and the customer API once the device's product defines it with a server-side argument schema.

Application MCU · portable C / STM32 HAL

Bridge through NivaloLink

The Nivalo MCU library provides a pure C99 frame/JSON core, a bounded transmit queue, command dispatch, and STM32 HAL/CMSIS-RTOS integration. The ESP32 remains the TLS/MQTT bridge; the application MCU owns its control loop.

#include "nivalo_mcu.h"

nivalo_mcu_init(&hspi4, DATA_READY_GPIO_Port, DATA_READY_Pin);
nivalo_mcu_start();

nivalo_mcu_update_variable("temperature", "23.4", "C");
nivalo_mcu_publish("control.loop", "{\"state\":\"stable\"}");
nivalo_mcu_command_status(command_id, "succeeded", "Applied");

NivaloLink SPI v1 specifies fixed-size frames, CRC, explicit flow control, bounded retries, and message fragmentation. Optional SWD wiring is separate from the runtime link and is used only for signed STM32 update/recovery flows.

Reference project shapes

Standalone ESP32

Factory claim provisioning, runtime TLS identity, application variables and functions, and bounded telemetry. Start with the public browser image, then move to a reviewed SDK bundle for custom builds.

ESP32 + STM32 bridge

An ESP32 cloud bridge with NivaloLink transport to an application MCU and separate recovery wiring. Use the published NivaloLink specification to keep each side compatible.

Protocol and dependency versioning