# LunaLink node for ESP-IDF
A [LunaLink](https://github.com/lunasoft-llc/lunalink) node written in C11. It connects an ESP32 to a LunaLink master over TCP, publishes tag values with durable sequence numbers and answers write commands, using the same protocol as the .NET library.
- Frames, CRC-16 and MessagePack messages as defined in the [protocol specification](https://github.com/lunasoft-llc/lunalink/blob/main/docs/PROTOCOL.md) (revision 2.0; a 1.1 master still works).
- Monotonic NodeSequence across reboots, kept in NVS (one flash write per 1000 batches).
- RAM outbox of 32 batches with stop-and-wait delivery, or a bounded durable outbox in flash (LittleFS) that survives power loss; a full-state baseline after a reboot or an overflow.
- TLS 1.3 with CA verification, optional key pinning and mutual TLS (`ll_esp_tls_transport`).
- Optional protocol 2.0 features, negotiated per connection: goodbye, lz4-block, no-crc (TLS only), master-delta, browse-v1 and events-v1 (`ll_config_t.features`).
- Write commands run once per CommandId, even when the master retries after a reconnect.
- The protocol core is portable C with no heap allocation; `port/esp/` adapts it to lwIP, esp-tls, NVS, LittleFS and `esp_timer`. `port/stm32/` is a compile-tested port for STM32 with lwIP.
Requires ESP-IDF 5.4 or later and a LunaLink master, version 1.1 or later.
## Installation
```shell
idf.py add-dependency "siqurd/lunalink^0.2.0"
```
To start from the demo firmware instead:
```shell
idf.py create-project-from-example "siqurd/lunalink^0.2.0:demo"
```
## Usage
```c
#include "ll_esp_port.h"
#include "lunalink/ll_session.h"
static ll_session_t session; /* about 28 KB; keep it off the task stack */
ll_config_t cfg = {
.host = "192.168.1.10", .port = 7788,
.node_id = "pump-station-1", .node_name = "Pump station 1",
.auth_token = "change-me", .version = "1.0.0",
};
ll_session_init(&session, &cfg, ll_esp_transport(), ll_esp_clock(),
ll_esp_seq_store(), ll_esp_cmd_store(),
(ll_callbacks_t){my_snapshot, my_write, my_log, NULL});
for (;;) {
ll_session_poll(&session, 50);
/* call ll_session_publish(&session, values, count) when you have new values */
}
```
`nvs_flash_init()` must run before the session starts. `my_snapshot` returns the current value of every tag and `my_write` applies a write command. When the session is disconnected, `ll_session_poll` returns at once, so yield in your loop to let the idle task feed the watchdog.
The [repository](https://github.com/lunasoft-llc/lunalink-esp32) has the full documentation, host tests and an interop harness.
TLS: pass `ll_esp_tls_transport(&tls_config)` instead of `ll_esp_transport()` (needs `CONFIG_MBEDTLS_SSL_PROTO_TLS1_3=y`). On TLS 1.3 with mutual TLS the build grows by about 126 KiB of flash and 1 KB of static RAM (see the repository README, section "TLS"). The plaintext transport remains for trusted networks, where the auth token travels in clear text.
## License
MIT. Bundled [MPack](third_party/mpack/LICENSE) is MIT licensed.
48f2428d2526952432cde16ba38dcfc02aa05bce
idf.py add-dependency "siqurd/lunalink^0.2.0"