Zum Inhalt

sensor_log87.tc

sensor_log87.tc - one CSV line every two minutes with every sensor of the "Umgebungssensoren" box (.87), kept on the

Source on GitHub

// @name: Sensor Log (CSV on flash, every 2 minutes)
// sensor_log87.tc - one CSV line every two minutes with every sensor of the "Umgebungssensoren" box (.87), kept on the
// device's own flash so a week without anybody reading it is covered.
//
// Why: gemu wants the particle / CO2 / TVOC curves of a whole week (holiday 09.-16.10.2026) to pick thresholds for an
// early-warning, and a Mac that sleeps is no log. ~60 bytes a line = ~43 kB a day at LOG_EVERY 120; two files of LOG_LIMIT
// bytes take turns (the older one is cleared when the newer one is full), so about 18 days stay readable at any moment
// (at 60 s it would be 9). The SPS30 itself delivers a new value about every 95 s, so 120 s loses little.
//
// Read:   http://<ip>/ufsd?download=/log1.csv   and   /log2.csv   (the one with the newer last line is current)
// Lines:  zeit,pm1,pm25,pm10,nc05,co2,eco2,tvoc,temp,rf     "# ..." lines mark a start (with the restart reason)
// Slot:   needs a free slot - on .87 it replaces the idle MP3 player (dysv17f) in slot 4.
// FTP:    every 30 minutes the new lines are APPENDED to a file on the FRITZ!NAS (ftpPut, mode 1). Credentials are not in
//         the source: /ftp.cfg on the device, four lines = host (the IP, not fritz.box), user, password, remote path
//         (e.g. /FRITZ/mediabox/umgebung87.csv). Without /ftp.cfg the FTP part stays off and the flash log works alone.
//         A failed upload (box off, WLAN down) keeps the lines in /out.csv (capped at 100 kB) and retries half an hour later.
//         Console: LGSTAT (outbox size, last result, bytes sent), LGGO (send now).
// Also:   keeps the SPS30 on a 60 s cycle (its own script defaults to 300 s); sent again every 10 minutes.

#define LOG_LIMIT 400000
#define LOG_EVERY 120

int lg_sec = 0;                        // seconds since the script started
int lg_sel = -1;                       // current file: 0 = /log1.csv, 1 = /log2.csv
int lg_boot_done = 0;

char fhost[64]    = "";                // /ftp.cfg: host, user, password, remote path (empty host = FTP off)
char fuser[32]    = "";
char fpw[32]      = "";
char fremote[96]  = "/FRITZ/mediabox/umgebung87.csv";
char lg_held[160];                     // the line written while an upload was running
int  lg_go        = 0;                 // 1 = TaskLoop should send now
int  lg_hold      = 0;                 // 1 = upload running, lines wait in lg_held
int  lg_last      = -999;              // last return value of the upload
int  lg_sent      = 0;                 // bytes sent in total

// Start of a file: the column names
void lg_header(int f) {
    char h[64];
    strcpy(h, "zeit,pm1,pm25,pm10,nc05,co2,eco2,tvoc,temp,rf\n");
    fileWrite(f, h, strlen(h));
}

// Append one line to the current file; switch to (and clear) the other one when this one is full.
void lg_write(char line[]) {
    int f; int s1; int s2; int sz;
    if (lg_sel < 0) {                                   // first call after a start: continue the file that is not full
        s1 = fileSize("/log1.csv");
        s2 = fileSize("/log2.csv");
        if (s1 < 0) { s1 = 0; }
        if (s2 < 0) { s2 = 0; }
        lg_sel = 0;
        if (s1 > LOG_LIMIT) { lg_sel = 1; }
        else { if (s2 > 0 && s2 <= LOG_LIMIT && s2 > s1) { lg_sel = 1; } }
    }
    if (lg_sel == 0) { sz = fileSize("/log1.csv"); } else { sz = fileSize("/log2.csv"); }
    if (sz > LOG_LIMIT) {                               // full: the other file starts again, this one stays readable
        lg_sel = 1 - lg_sel;
        if (lg_sel == 0) { f = fileOpen("/log1.csv", 1); } else { f = fileOpen("/log2.csv", 1); }
        if (f >= 0) { lg_header(f); }
    } else {
        if (lg_sel == 0) { f = fileOpen("/log1.csv", 2); } else { f = fileOpen("/log2.csv", 2); }
        if (f < 0) {                                    // not there yet: create it with the column names
            if (lg_sel == 0) { f = fileOpen("/log1.csv", 1); } else { f = fileOpen("/log2.csv", 1); }
            if (f >= 0) { lg_header(f); }
        }
    }
    if (f < 0) { return; }
    fileWrite(f, line, strlen(line));
    fileClose(f);
}

// Same line into the outbox that goes to the FRITZ!NAS
void lg_out(char line[]) {
    int f;
    if (!fhost[0]) { return; }
    if (lg_hold) { strcpy(lg_held, line); return; }     // upload running: keep the line until it is done
    if (fileSize("/out.csv") > 100000) { return; }      // box away for a day: the flash log keeps everything
    f = fileOpen("/out.csv", 2);
    if (f < 0) { f = fileOpen("/out.csv", 1); }
    if (f < 0) { return; }
    fileWrite(f, line, strlen(line));
    fileClose(f);
}

// Sending must not happen in EverySecond (the transfer would hold the VM mutex), so it runs here
void TaskLoop() {
    int r; int f; char hdr[64];
    delay(1000);
    if (!lg_go) { return; }
    lg_go = 0;
    if (!fhost[0]) { return; }
    if (fileSize("/out.csv") <= 0) { return; }
    lg_hold = 1;
    r = ftpOpen(fhost, fuser, fpw);
    if (r >= 0) {
        if (ftpSize(fremote) < 0) {                     // new file on the box: column names first
            strcpy(hdr, "zeit,pm1,pm25,pm10,nc05,co2,eco2,tvoc,temp,rf\n");
            ftpPutStr(fremote, hdr, 0);
        }
        r = ftpPut(fremote, "/out.csv", 1);             // 1 = append
        ftpClose();
    }
    lg_last = r;
    if (r >= 0) {
        lg_sent = lg_sent + r;
        f = fileOpen("/out.csv", 1);                    // sent: empty the outbox
        if (f >= 0) { fileClose(f); }
    }
    lg_hold = 0;
    if (lg_held[0]) { lg_out(lg_held); lg_held[0] = 0; }
}

void trim(char s[]) {                                   // drop \r and blanks at the end (a Windows editor adds \r)
    int n = strlen(s);
    while (n > 0 && (s[n - 1] == '\r' || s[n - 1] == ' ' || s[n - 1] == '\n')) { n--; s[n] = 0; }
}

int cfgRead() {                                         // 1 = /ftp.cfg read
    char cfg[256];
    int f = fileOpen("/ftp.cfg", 0);
    int n;
    if (f < 0) { return 0; }
    n = fileRead(f, cfg, 255);
    fileClose(f);
    if (n <= 0) { return 0; }
    cfg[n] = 0;
    if (webParse(cfg, "\n", 1, fhost)   > 0) { trim(fhost); }
    if (webParse(cfg, "\n", 2, fuser)   > 0) { trim(fuser); }
    if (webParse(cfg, "\n", 3, fpw)     > 0) { trim(fpw); }
    if (webParse(cfg, "\n", 4, fremote) > 0) { trim(fremote); }
    return 1;
}

void Command(char cmd[]) {
    char resp[120];
    if (strcmp(cmd, "STAT") == 0) {
        sprintf(resp, "outbox=%d B last=%d sent=%d B ftp=%s", fileSize("/out.csv"), lg_last, lg_sent, fhost[0] ? "on" : "off");
        responseCmnd(resp);
    } else if (strcmp(cmd, "GO") == 0) {
        lg_go = 1;
        responseCmnd("sending");
    } else {
        responseCmnd("LGSTAT | LGGO");
    }
}

void EverySecond() {
    char ts[24]; char line[160]; char r[512]; char why[48]; char c[24];
    float pm1; float pm25; float pm10; float nc05; float t; float rf;
    int co2; int eco2; int tvoc; int p;
    lg_sec = lg_sec + 1;
    if (lg_sec == 200 || lg_sec % 1800 == 1750) { lg_go = 1; }   // first upload soon after the start, then every 30 minutes
    if (lg_sec == 45 || lg_sec % 600 == 0) {            // the SPS30 script starts with 300 s; ask for one measurement a minute
        r[0] = 0;
        tasmCmd("SPS30Interval 60", r);
    }
    if (lg_sec % LOG_EVERY != 30) { return; }
    timeStamp(ts);
    if (!lg_boot_done) {                                // once per start: why did the device start?
        lg_boot_done = 1;
        r[0] = 0; why[0] = 0;
        tasmCmd("Status 1", r);
        p = strFind(r, "RestartReason");
        if (p >= 0) {
            strSub(why, r, p + 16, 40);                 // after RestartReason":"
            p = strFind(why, "\"");
            if (p > 0) { why[p] = 0; }
        }
        sprintf(line, "# %s start, reason=[%s] uptime %d s\n", ts, why, tasm_uptime);
        lg_write(line);
        lg_out(line);
    }
    pm1  = sensorGet("SPS30#PM1_0");
    pm25 = sensorGet("SPS30#PM2_5");
    pm10 = sensorGet("SPS30#PM10");
    nc05 = sensorGet("SPS30#NCPM0_5");
    co2  = (int)sensorGet("SCD30#CarbonDioxide");
    eco2 = (int)sensorGet("SGP30#eCO2");
    tvoc = (int)sensorGet("SGP30#TVOC");
    t    = sensorGet("BME280#Temperature");
    rf   = sensorGet("BME280#Humidity");
    sprintf(line, "%s,%.1f,%.1f,%.1f,%.0f,%d,%d,%d,%.1f,%.0f\n", ts, pm1, pm25, pm10, nc05, co2, eco2, tvoc, t, rf);
    lg_write(line);
    lg_out(line);
}

int main() {
    lg_held[0] = 0;
    if (cfgRead()) { addLog("sensor_log87: /ftp.cfg read, FTP on"); } else { addLog("sensor_log87: no /ftp.cfg, FTP off"); }
    tcpConnectTimeout(3000);
    addCommand("LG");
    addLog("sensor_log87: one line every 2 minutes into /log1.csv and /log2.csv");
    return 0;
}