Ir al contenido

← Todas las funcionalidades

Registro de datos en CSV con un Ardbox Q Lite: ejemplo completo

El Ardbox Q Lite guarda sus entradas cada segundo en un archivo CSV diario en su eMMC interna, listo para abrir en una hoja de cálculo.

Qué hace

El microcontrolador lee las cuatro entradas digitales y las seis entradas de 0-10 V. La app de Linux las recoge con Arduino Bridge y añade una línea por segundo a un archivo CSV en /home/arduino/ardbox-logs. Cada día empieza un archivo nuevo.

Qué necesitas

  • Ardbox Q Lite AD o ADR con alimentación de 12 a 24 Vdc.
  • Arduino IDE 2.0+ con el paquete de placas de Industrial Shields (placa: Ardbox Q, tu versión).
  • Arduino App Lab para ejecutar la app de Linux.

Cómo ponerlo en marcha

  1. Carga sketch/sketch.ino con Arduino IDE.
  2. Crea una app en Arduino App Lab con app.yaml y python/main.py de esta página, y ejecútala.
  3. Copia los archivos con scp o ábrelos por SSH.

sketch/sketch.ino

/*
  Ardbox Q Lite example 04: microcontroller side

  Gives the Linux app access to the inputs and one output through Arduino Bridge.
  Output: Q0.0 on Ardbox Q Lite AD, relay R1 on Ardbox Q Lite ADR.
  Upload with Arduino IDE 2.0+ or arduino-cli, board Ardbox Q, your version.
*/

#include <Arduino_RouterBridge.h>

#if defined(IS_ARDBOX_Q_RELAY)
const pin_size_t OUT = R1;    // Ardbox Q Lite ADR: relay R1
#else
const pin_size_t OUT = Q0_0;  // Ardbox Q Lite AD: digital output Q0.0
#endif

const pin_size_t DIGITAL_IN[] = { I0_0, I0_1, I0_2, I0_3 };
const pin_size_t ANALOG_IN[]  = { I0_4, I0_5, I0_6, I0_7, I0_8, I0_9 };

// Digital input 0 to 3 (I0.0 to I0.3). Returns -1 if the channel does not exist.
int readDigital(int ch) {
  if (ch < 0 || ch > 3) return -1;
  return digitalRead(DIGITAL_IN[ch]);
}

// Analog input 0 to 5 (I0.4 to I0.9), raw value 0 to 1023 (10 bits by default).
int readAnalog(int ch) {
  if (ch < 0 || ch > 5) return -1;
  return analogRead(ANALOG_IN[ch]);
}

bool setOutput(bool on) {
  digitalWrite(OUT, on ? HIGH : LOW);
  return on;
}

void setup() {
  for (pin_size_t p : DIGITAL_IN) pinMode(p, INPUT);
  pinMode(OUT, OUTPUT);
  Bridge.begin();
  Bridge.provide_safe("read_digital", readDigital);
  Bridge.provide_safe("read_analog", readAnalog);
  Bridge.provide_safe("set_output", setOutput);
}

void loop() {}

python/main.py

"""
Ardbox Q Lite example 04: data logging

Every second, reads the four digital inputs and the six analog inputs
and adds a line to a CSV file on the internal eMMC. A new file starts each day.

1. Upload sketch/sketch.ino with Arduino IDE (board Ardbox Q, your version).
2. Run this app from Arduino App Lab.
"""
import csv
import os
import time
from datetime import datetime

from arduino.app_utils import *

LOG_DIR = "/home/arduino/ardbox-logs"
PERIOD_S = 1.0
HEADER = ["time"] + [f"I0.{i}" for i in range(10)]

os.makedirs(LOG_DIR, exist_ok=True)


def loop():
    now = datetime.now()
    row = [now.isoformat(timespec="seconds")]
    row += [Bridge.call("read_digital", ch) for ch in range(4)]
    row += [Bridge.call("read_analog", ch) for ch in range(6)]

    path = os.path.join(LOG_DIR, now.strftime("%Y%m%d") + ".csv")
    new_file = not os.path.exists(path)
    with open(path, "a", newline="") as f:
        writer = csv.writer(f)
        if new_file:
            writer.writerow(HEADER)
        writer.writerow(row)

    time.sleep(PERIOD_S)


App.run(user_loop=loop)

app.yaml

name: Ardbox Q Lite data logging
description: Saves the inputs to a CSV file every second
icon: "📈"
bricks: []