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.
What it does
Section titled “What it does”- 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.
Create the device’s identity
Section titled “Create the device’s identity”Flash the Provisioning example, open the serial monitor at 115200 baud, and:
- Type
kto generate a key on the chip. The private key never leaves the device. - For token login, type
pto 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.
Connect and send readings
Section titled “Connect and send readings”#include <WiFi.h>#include <Espoirnet.h>
static espoirnet_persist_t persist; // queued readings and statestatic 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 insidepoll(), 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()returnsESPOIRNET_ERR_CLOCK_NOT_SET, and the device waits before logging in. - Errors are return codes. Every function returns an
espoirnet_err_t, andespoirnet_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. Callespoirnet_start()once it’s fixed.
The reading queue
Section titled “The reading queue”- 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_DROPPEDis 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.
State reports
Section titled “State reports”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.
Commands
Section titled “Commands”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.
Certificate login
Section titled “Certificate login”- In the
Provisioningexample, typecto print a certificate request. - On the device’s page in the console, under Certificates, issue a certificate for that request (see Device Certificates).
- Type
iin the serial monitor and paste the certificate back. - Use
device.beginWithCertificate(cfg)in place ofbeginWithToken. TheCertificateAuthexample shows the whole sketch.
Certificates from your own certificate authority work the same way.
Deep sleep
Section titled “Deep sleep”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 mswhile (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.
Examples
Section titled “Examples”| 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) |
Configuration
Section titled “Configuration”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 |
Size and performance
Section titled “Size and performance”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.
Board notes
Section titled “Board notes”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.
Before you ship
Section titled “Before you ship”- 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.
Next steps
Section titled “Next steps”- Secure Elements — keeping the device key in dedicated hardware.
- Telemetry — viewing and querying your readings.
- Troubleshooting — when a device won’t connect.

