AgriQL · Embedded · Data to the server

Data to the server

A field device keeps readings until a gateway has them. There are two ways to move them.

Forward over a link

This suits LoRaWAN, NB-IoT, satellite IoT, BLE, Wi-Fi and serial. On the device:

uint64_t seg;
while (logbook.oldestSealed(seg)) {
  logbook.readSegment(seg, sendReading, nullptr);  // your radio code sends each reading
  if (!gatewayConfirmed()) break;                  // keep it until the gateway says it has it
  logbook.release(seg);
}

On the gateway, store what arrives with SQL:

RECORD READINGS (7, '2026-11-27T12:00:00Z', 3005), (7, '2026-11-27T12:01:00Z', 3011, 1, SUSPECT);

The fields are sensor, time, raw value, and optionally a sequence number and a quality flag. The sequence number lets the server recognise a reading that was sent twice.

Then calibrate and query:

REGISTER SENSOR 7 NAME soil_moisture_a UNIT M3_PER_M3 LOCATION 'farm1/plot3';
RECALIBRATE SENSOR 7 FROM '2026-11-27T12:00' SCALE 0.0001 OFFSET 0.002;
SCAN READINGS SENSOR 7 EVERY 1 H AGGREGATE AVG;

Read the chip

Read the partition out over SWD or JTAG, or move a removable SPI flash chip to the gateway. The server reads the device's format directly, without conversion. On a Linux gateway with SPI NOR flash wired to it, the server can open /dev/mtdN directly.

Opening a raw flash image is part of the AgriQL server's Rust API in 3.10.1. The agriql command does not have a command for it yet, so forwarding over a link is the simpler route today.

Time

A microcontroller's crystal drifts by up to several seconds a day. Stamp each reading with millis() and, whenever GNSS, NTP, the network or an RTC gives real time, record a clock anchor:

// at millis() == tick, UTC was utcMs, +/- 50 ms, learned from GNSS
logbook.anchor(0, (int64_t)millis(), utcMs, 50, AGRIQL_EDGE_CLOCK_GNSS);

The server converts the ticks to UTC and keeps the error bound with each reading.