Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
4.8 kB
1
Indexable
Never
#include <Wire.h>
#include <driver/i2s.h>
#include <cmath>
#include <DHT.h>
#include <DHT_U.h>

// Configuración de los parámetros I2S
#define SAMPLE_BUFFER_SIZE 512
#define SAMPLE_RATE 44100
#define I2S_MIC_CHANNEL I2S_CHANNEL_FMT_ONLY_LEFT

// Pines ajustados a los mencionados anteriormente
#define I2S_MIC_SERIAL_CLOCK GPIO_NUM_27 // SCK
#define I2S_MIC_LEFT_RIGHT_CLOCK GPIO_NUM_25 // WS
#define I2S_MIC_SERIAL_DATA GPIO_NUM_26 // SD

// Pines PWM para ventiladores
#define FAN1_PWM_PIN 16
#define FAN2_PWM_PIN 17

// Pines tacómetro (RPM) para ventiladores
#define FAN1_TACH_PIN 32
#define FAN2_TACH_PIN 33

// Pines DHT
#define DHTPIN_INT 12
#define DHTPIN_EXT 13
#define DHTTYPE DHT11

// Factores de calibración
#define CALIBRATION_GAIN 0.2
#define CALIBRATION_OFFSET 30

i2s_config_t i2s_config = {
    .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
    .sample_rate = SAMPLE_RATE,
    .bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT,
    .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
    .communication_format = I2S_COMM_FORMAT_I2S,
    .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
    .dma_buf_count = 4,
    .dma_buf_len = 1024,
    .use_apll = false,
    .tx_desc_auto_clear = false,
    .fixed_mclk = 0
};

i2s_pin_config_t i2s_mic_pins = {
    .bck_io_num = I2S_MIC_SERIAL_CLOCK,
    .ws_io_num = I2S_MIC_LEFT_RIGHT_CLOCK,
    .data_out_num = I2S_PIN_NO_CHANGE,
    .data_in_num = I2S_MIC_SERIAL_DATA
};

// Inicialización de sensores DHT11
DHT dht_int(DHTPIN_INT, DHTTYPE);
DHT dht_ext(DHTPIN_EXT, DHTTYPE);

unsigned long startTime = 0;
int32_t raw_samples[SAMPLE_BUFFER_SIZE];

void setup() {
  Serial.begin(115200);
  i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
  i2s_set_pin(I2S_NUM_0, &i2s_mic_pins);

  // Inicialización del PWM
  ledcSetup(0, 25000, 10); // Canal 0, frecuencia 25 kHz, resolución 10 bits
  ledcAttachPin(FAN1_PWM_PIN, 0);
  ledcSetup(1, 25000, 10); // Canal 1, frecuencia 25 kHz, resolución 10 bits
  ledcAttachPin(FAN2_PWM_PIN, 1);

  // Inicialización de los sensores DHT11
  dht_int.begin();
  dht_ext.begin();

  startTime = millis();  // Guardar el tiempo de inicio

  // Encabezado CSV
  Serial.println("ID,PWM1,PWM2,Temp_Int,Hum_Int,Temp_Ext,Hum_Ext,Decibels,RPM1,RPM2");
}

// Función para leer RPM del ventilador (placeholder)
int read_rpm(int tach_pin) {
  // Implementa la lógica específica para leer RPM del ventilador
  return 0; // Placeholder
}

void loop() {
  int record_id = 1;

  for (int pwm1_value = 0; pwm1_value <= 1023; pwm1_value += 128) {
    for (int pwm2_value = 0; pwm2_value <= 1023; pwm2_value += 128) {
      // Ajustar PWM
      ledcWrite(0, pwm1_value);
      ledcWrite(1, pwm2_value);

      delay(30000);  // Esperar 30 segundos

      // Leer temperatura y humedad (interior)
      float humidity_int = dht_int.readHumidity();
      float temperature_int = dht_int.readTemperature();

      // Leer temperatura y humedad (exterior)
      float humidity_ext = dht_ext.readHumidity();
      float temperature_ext = dht_ext.readTemperature();

      // Verificar si hay errores en la lectura
      if (isnan(humidity_int) || isnan(temperature_int) || isnan(humidity_ext) || isnan(temperature_ext)) {
        Serial.println("Failed to read from DHT sensor!");
        continue;
      }

      // Leer decibelios
      size_t bytes_read = 0;
      i2s_read(I2S_NUM_0, raw_samples, sizeof(int32_t) * SAMPLE_BUFFER_SIZE, &bytes_read, portMAX_DELAY);
      int samples_read = bytes_read / sizeof(int32_t);

      int64_t sum_squares = 0;
      for (int i = 0; i < samples_read; i++) {
        sum_squares += int64_t(raw_samples[i]) * raw_samples[i];
      }

      float rms = sqrt((float)sum_squares / samples_read);
      float decibels = 20 * log10(rms);

      // Aplicar la calibración lineal
      decibels = decibels * CALIBRATION_GAIN + CALIBRATION_OFFSET;

      // Leer RPM
      int rpm1 = read_rpm(FAN1_TACH_PIN);
      int rpm2 = read_rpm(FAN2_TACH_PIN);

      // Imprimir datos en formato CSV
      Serial.print(record_id);
      Serial.print(",");
      Serial.print(pwm1_value);
      Serial.print(",");
      Serial.print(pwm2_value);
      Serial.print(",");
      Serial.print(temperature_int);
      Serial.print(",");
      Serial.print(humidity_int);
      Serial.print(",");
      Serial.print(temperature_ext);
      Serial.print(",");
      Serial.print(humidity_ext);
      Serial.print(",");
      Serial.print(decibels);
      Serial.print(",");
      Serial.print(rpm1);
      Serial.print(",");
      Serial.print(rpm2);
      Serial.println();

      // Incrementar el ID del registro
      record_id++;
    }
  }

  Serial.println("Recolección de datos completada.");
  while (true) {
    delay(1000);  // Mantener detenido
  }
}
Leave a Comment