Skip to content

Embedded C SDK

Connect microcontrollers to Espoirnet. The SDK is written for firmware: a portable C core with no dynamic memory, a loop that never blocks for long, and a thin layer for each platform.

Supported today: ESP32 with the Arduino core 3.x, using the Arduino IDE or PlatformIO. Tested on ESP32 DevKit-style boards and on the RAK11200 WisBlock.

  • Readings survive outages. Every reading carries the time it was taken. Readings taken while offline are queued and delivered in order, with confirmation, once the device is back.
  • State reports for changes made on the device, keeping only the latest value per sensor.
  • Commands, each acknowledged with its outcome. A command delivered twice runs once.
  • Token or certificate login, either of which you can revoke from the console.
  • Deep-sleep cycles: wake, deliver, receive any waiting commands, confirm, sleep. Queued data survives sleep.

Flash the Provisioning example, open the serial monitor at 115200 baud, and:

  1. Type k to generate a key on the chip. The private key never leaves the device.
  2. For token login, type p to print the public key, then add it to the device in the console. For certificate login, see below.

For a key that can’t be read out even with physical access to the board, see Secure Elements.

#include <WiFi.h>
#include <Espoirnet.h>
static espoirnet_persist_t persist; // queued readings and state
static Espoirnet device(persist);
void setup() {
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
configTime(0, 0, "pool.ntp.org"); // readings and login need the time
espoirnet_config_t cfg = {};
cfg.host = "rayon.espoirnet.io";
cfg.port = 8883;
cfg.account_id = ACCOUNT_KEY;
cfg.project_id = PROJECT_KEY;
cfg.device_id = DEVICE_KEY;
device.beginWithToken(cfg);
}
void loop() {
device.poll(); // all the work happens here
// when you have a reading:
// device.send("tempe", 21.5); // queued, delivered when online
delay(5);
}

The keys come from the device’s page in the console (see Find your keys).

  • One loop. poll() does a bounded amount of work and returns. There are no background tasks, and your callbacks only run inside poll(), so it won’t trip a watchdog.
  • You own the memory. Every buffer has a size fixed at compile time.
  • Time is required. Until the clock is set, send() returns ESPOIRNET_ERR_CLOCK_NOT_SET, and the device waits before logging in.
  • Errors are return codes. Every function returns an espoirnet_err_t, and espoirnet_err_str() turns it into text.
  • Reconnecting is automatic. Network failures and temporary platform problems are retried with a randomised, growing delay, and sessions are renewed before they expire.
  • A refused login is final. A revoked or unknown key or certificate stops the device instead of retrying, and raises ESPOIRNET_EVENT_REJECTED. Call espoirnet_start() once it’s fixed.
  • 7 days: a reading is accepted if it was taken within the last 7 days. The SDK drops older queued readings itself.
  • When the queue is full, the oldest unsent reading is dropped and ESPOIRNET_EVENT_TELEMETRY_DROPPED is raised.
  • 5 minutes: readings older than this when they arrive are stored and charted, but don’t trigger automations, because automations react to current conditions.
  • Duplicates are possible. After a connection drop, a reading whose confirmation was lost may be delivered twice, with the same timestamp.
device.reportState("switc", switchOn ? "1" : "0");

Only the latest value per sensor is kept. Flip a switch five times while offline, and the console gets the final position when the device reconnects. See Sensor State.

static void onCommand(espoirnet_device_t *, const espoirnet_command_t *cmd, void *) {
bool on;
if (espoirnet_value_bool(cmd, &on) != ESPOIRNET_OK) {
device.ack(*cmd, false); // shows as failed in the console
return;
}
digitalWrite(LED_PIN, on ? HIGH : LOW);
device.ack(*cmd, true);
}
// in setup(), before begin:
cfg.on_command = onCommand;

cmd->sensor_id says which sensor the command is for. espoirnet_value_number() and espoirnet_value_string() read other kinds of value. A command is only valid during the callback, so copy what you need to act on it later. See Sending Commands.

  1. In the Provisioning example, type c to print a certificate request.
  2. On the device’s page in the console, under Certificates, issue a certificate for that request (see Device Certificates).
  3. Type i in the serial monitor and paste the certificate back.
  4. Use device.beginWithCertificate(cfg) in place of beginWithToken. The CertificateAuth example shows the whole sketch.

Certificates from your own certificate authority work the same way.

Put the queue in RTC memory so it survives sleep, and run one delivery cycle per wake-up:

RTC_DATA_ATTR static espoirnet_persist_t persist;
device.cycleBegin(20000); // budget for this wake-up, in ms
while (device.cyclePoll() == ESPOIRNET_CYCLE_RUNNING) delay(5);
device.stop();
esp_deep_sleep_start();

cyclePoll() returns ESPOIRNET_CYCLE_DONE once everything queued is confirmed and no further commands arrive for a short quiet period. It returns ESPOIRNET_CYCLE_TIMEOUT if the budget runs out, and anything undelivered stays queued for the next wake-up.

Example Shows
Provisioning Key generation on the device, public key, certificate request and install
Telemetry Readings every 10 seconds, buffered while offline
Commands Switching an LED from the console, with acknowledgement
State Reporting a local button press
CertificateAuth Certificate login
DeepSleep One delivery cycle per wake-up
SecureElementProvisioning, SecureElement A key held in an ATECC608 (see Secure Elements)

Sizes are compile-time settings. Override them with build flags, for example -DESPOIRNET_QUEUE_RECORDS=64.

Setting Default Meaning
ESPOIRNET_QUEUE_RECORDS 16 Readings held until confirmed
ESPOIRNET_RECORD_SIZE 96 Bytes per send() call, encoded
ESPOIRNET_MAX_INFLIGHT 4 Unconfirmed readings being delivered at once
ESPOIRNET_MAX_STATE_SENSORS 4 Sensors with reported state
ESPOIRNET_RX_BUF_SIZE 512 Largest command kept; larger ones are acknowledged and discarded
ESPOIRNET_TX_BUF_SIZE 768 Outgoing packet buffer
ESPOIRNET_ENABLE_TOKEN_AUTH 1 0 removes token login for certificate-only firmware
ESPOIRNET_LOG_LEVEL 3 0 off to 4 debug; removed at compile time

On an ESP32 with the defaults, the SDK adds about 20 KB of flash and 7 KB of RAM to what Wi-Fi and TLS already use. TLS also uses about 40 KB of heap while connected.

Wi-Fi with TLS alone fills about 81% of the default 1.25 MB app partition. For a larger application, choose a bigger one, such as PlatformIO’s board_build.partitions = huge_app.csv. That scheme has no room for over-the-air updates.

Connecting on the original ESP32: name lookup, TCP and TLS advance a little on each poll(), except for one step of the TLS handshake. That step takes up to about 1 second inside a single poll(), once per connection, because the original ESP32 has no elliptic-curve hardware. It stays well inside the 5-second task watchdog.

RAK11200: its base board has no automatic reset into the bootloader. Connect BOOT0 to GND and press RESET before uploading, then remove BOOT0 and press RESET to run. In PlatformIO, build for esp-wrover-kit and set board_upload.before_reset = no-reset.

  • Keys stay on the device. They’re generated on the chip and never printed or exported.
  • The server is always verified, and there’s no option to skip it. The hosted platform is checked against the built-in public CA bundle.
  • Protect the flash for production. By default, the key and certificate sit in flash unencrypted. Enable flash encryption, NVS encryption and secure boot.
  • Consider a secure element for a key that can’t be extracted at all.