Layer: L7 Application ยท Build flags: PROTOCORE_ENABLE_UDP_TELEMETRY
What this example teaches
For high-frequency metrics you often do not want the cost or back-pressure of TCP. This builds an InfluxDB line-protocol record and casts it to a collector over UDP once a second - zero-heap, no ACK, no retry. Point it at Telegraf or InfluxDB's UDP listener (or just nc -u -l 8094 to watch the packets).
Set the destination once:
protocore_udp_telemetry_begin(COLLECTOR_IP, COLLECTOR_PORT);
Build a line into a caller-owned buffer, then cast it. protocore_line_* appends typed fields (the i suffix InfluxDB uses for integers comes from the _int/_uint helpers); protocore_udp_telemetry_write() sends one datagram:
protocore_line line;
protocore_line_init(&line, buf, sizeof(buf), "esp32");
protocore_line_add_uint(&line, "heap", ESP.getFreeHeap());
protocore_line_add_int(&line, "rssi", Physical.wifi->rssi());
protocore_line_add_float(&line, "temp", temperatureRead(), 1);
protocore_udp_telemetry_cast(&line);
#define PROTOCORE_UDP_TELEMETRY_BUF
Stack buffer for one telemetry line (bytes).
There is no server here - the device is purely a telemetry source - so loop() just casts on a timer.
Build and run
pio ci --board=esp32dev --project-option="framework=arduino" \
--project-option="build_flags=-DPROTOCORE_ENABLE_UDP_TELEMETRY=1 -DPROTOCORE_ENABLE_UDP=1" \
--lib="." examples/L7-Application/UdpTelemetry/UdpTelemetry.ino
nc -u -l 8094 # watch the line-protocol datagrams (set COLLECTOR_PORT to match)
Annotated source
The complete sketch (UdpTelemetry.ino), reproduced verbatim with added explanatory comments:
#define PROTOCORE_ENABLE_UDP_TELEMETRY 1
#include "protocore.h"
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
static const char *COLLECTOR_IP = "192.168.1.10";
static const uint16_t COLLECTOR_PORT = 8094;
void setup()
{
Serial.begin(115200);
Physical.wifi->init(SSID, PASSWORD);
Serial.print("Connecting to WiFi");
while (!Physical.wifi->ready())
{
delay(250);
Serial.print('.');
}
uint32_t ip = Physical.link->egress_ip();
Serial.printf("IP: %u.%u.%u.%u\n", (unsigned)(ip & 0xFF), (unsigned)((ip >> 8) & 0xFF),
(unsigned)((ip >> 16) & 0xFF), (unsigned)((ip >> 24) & 0xFF));
protocore_udp_telemetry_begin(COLLECTOR_IP, COLLECTOR_PORT);
}
void loop()
{
static uint32_t last = 0;
if (millis() - last >= 1000)
{
last = millis();
protocore_line line;
protocore_line_init(&line, buf, sizeof(buf), "esp32");
protocore_line_add_uint(&line, "heap", ESP.getFreeHeap());
protocore_line_add_int(&line, "rssi", Physical.wifi->rssi());
protocore_line_add_float(&line, "temp", temperatureRead(), 1);
if (protocore_udp_telemetry_cast(&line))
Serial.printf("cast: %s\n", buf);
}
}
Layer 1 (Physical) - link bring-up, the interface registry, and live egress reporting.
One point in line protocol, cast to a collector as one UDP datagram (PROTOCORE_ENABLE_UDP_TELEMETRY).