MCU-integratie

Overzicht

VBatPower accepteert telemetriedata van elk apparaat dat HTTP POST-verzoeken kan sturen. Het dataformaat is bewust eenvoudig: een platte CSV die met printf op elke 8-bit MCU gegenereerd kan worden.

De gebruikelijke werkwijze:

  1. Maak een project aan in de VBatPower-app
  2. Genereer een API-sleutel onder Instellingen > API
  3. Stuur telemetrie van je MCU naar POST /api/v1/telemetry
  4. Bekijk grafieken en analyses in de app

Arduino / ESP32

Met de WiFi HTTP-client stuur je telemetriedata:

// Global state
uint32_t nonce = 0;
uint32_t runid = 1;

// Read sensors and send data
void sendTelemetry() {
    float batteryV = analogRead(VBAT_PIN) * 3.3 / 4095.0 * 2.0;
    float currentMA = readINA219();
    uint32_t elapsed = millis();
    nonce++;

    String json = "{\"records\":[";
    // Battery voltage (type: v = value measurement)
    json += "{\"id\":\"0\",\"type\":\"v\",\"value\":";
    json += String(batteryV, 3);
    json += ",\"unit\":\"V\",\"nonce\":";
    json += String(nonce);
    json += ",\"runid\":";
    json += String(runid);
    json += "},";
    // State execution duration (type: t = telemetry timing)
    json += "{\"id\":\"0.0.0\",\"type\":\"t\",\"value\":";
    json += String(500);  // execution duration in ms
    json += ",\"unit\":\"ms\",\"nonce\":";
    json += String(nonce);
    json += ",\"runid\":";
    json += String(runid);
    json += "},";
    // Current draw (type: v = value measurement)
    json += "{\"id\":\"0.0.0\",\"type\":\"v\",\"value\":";
    json += String(currentMA, 2);
    json += ",\"unit\":\"mA\",\"nonce\":";
    json += String(nonce);
    json += ",\"runid\":";
    json += String(runid);
    json += "},";
    // Elapsed time from boot (type: n = elapsed-time measurement)
    json += "{\"id\":\"0.0.0\",\"type\":\"n\",\"value\":";
    json += String(elapsed);
    json += ",\"unit\":\"ms\",\"nonce\":";
    json += String(nonce);
    json += ",\"runid\":";
    json += String(runid);
    json += "}]}";

    HTTPClient http;
    http.begin("https://vbatpower.com/api/v1/telemetry");
    http.addHeader("Content-Type", "application/json");
    http.addHeader("Authorization", "Bearer " + String(API_KEY));
    int code = http.POST(json);
    http.end();
}

STM32 (bare metal / HAL)

Voor STM32 met een UART-naar-WiFi-module (ESP8266 AT-commando’s) of Ethernet formatteer je de JSON-payload met snprintf:

char buf[512];
int len = snprintf(buf, sizeof(buf),
    "{\"records\":["
    "{\"id\":\"0.0.0\",\"type\":\"t\","
    "\"value\":%lu,\"unit\":\"ms\","
    "\"nonce\":%lu,\"runid\":%lu},"
    "{\"id\":\"0.0.0\",\"type\":\"v\","
    "\"value\":%.2f,\"unit\":\"mA\","
    "\"nonce\":%lu,\"runid\":%lu},"
    "{\"id\":\"0.0.0\",\"type\":\"n\","
    "\"value\":%lu,\"unit\":\"ms\","
    "\"nonce\":%lu,\"runid\":%lu}"
    "]}",
    exec_duration_ms, nonce, runid,
    current_ma, nonce, runid,
    HAL_GetTick(), nonce, runid
);

Stuur buf als HTTP POST-body naar /api/v1/telemetry met de headers Content-Type: application/json en Authorization: Bearer your-key.

Offline verzamelen

Heeft je MCU geen netwerkverbinding, sla de data dan in CSV-formaat op op een SD-kaart of via de seriële poort:

// Print CSV to Serial or SD file
void logSample(uint32_t nonce, uint32_t runid) {
    float vbat = readBattery();
    float current = readCurrent();
    uint32_t elapsed = millis();

    Serial.printf("0,v,%.3f,V,%lu,%lu\n", vbat, nonce, runid);
    Serial.printf("0.0.0,t,500,ms,%lu,%lu\n", nonce, runid);
    Serial.printf("0.0.0,v,%.2f,mA,%lu,%lu\n", current, nonce, runid);
    Serial.printf("0.0.0,n,%lu,ms,%lu,%lu\n", elapsed, nonce, runid);
}

Sla de seriële uitvoer op in een .csv-bestand en importeer dat op de datapagina.

Nonce-strategie

De nonce is een steeds oplopende teller die metingen groepeert die op hetzelfde moment zijn genomen. Regels:

  • Verhoog hem één keer per meetcyclus (niet per record)
  • Alle records in één cyclus delen dezelfde nonce
  • Reset hem nooit binnen een run: gebruik runid om experimenten te scheiden
  • Gebruik een 32-bit geheel getal zonder teken: bij 1 sample per seconde loopt dit pas na 136 jaar over

Tips

  • Batch-uploads — buffer meerdere nonces en stuur ze in één POST om WiFi-wake-ups te beperken en stroom te besparen.
  • 8-bit MCU’s — het CSV-formaat vereist geen JSON-parsing aan de kant van het apparaat. Gebruik printf om rijen op te maken en upload ze later als bestand.
  • Meerdere toestanden — meet verschillende bedrijfsmodi (slaapstand, actief, TX) door elk een ander toestand-ID toe te kennen (bijv. 0.0.0 voor slaapstand, 0.0.1 voor actief).
  • Batterijbewaking — neem een spanningsmeting op diepte 1 op (bijv. 0,v,3.28,V,...) om de ontlading in de tijd te volgen.