Hi,
I have a sound decibel meter in an ESP32 using a digital microphone.
I am able to successfully print audio volume data on the serial port and also send it to Blynk at a rate of one sample per second.
What I am not understanding is that i can see the data live on SuperChart but on the other timeframes like 15 minutes, 1 hour … the widget says No data.
Here is the relevant code
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "XXXXXXXXXX"
#define BLYNK_TEMPLATE_NAME "XXXXXXXX"
#define BLYNK_AUTH_TOKEN "XXXXXXXXXXX"
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <BlynkSimpleEsp32_SSL.h>
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "XXXXXXX";
char pass[] = "XXXXXXX";
#include <driver/i2s.h>
#include "sos-iir-filter.h"
void setup()
{
// Debug console
Serial.begin(115200);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
}
void loop()
{
Blynk.run();
takeMeasurements();
}
void takeMeasurements() {
samples_queue = xQueueCreate(8, sizeof(sum_queue_t));
// Create the I2S reader FreeRTOS task
// NOTE: Current version of ESP-IDF will pin the task
// automatically to the first core it happens to run on
// (due to using the hardware FPU instructions).
// For manual control see: xTaskCreatePinnedToCore
xTaskCreate(mic_i2s_reader_task, "Mic I2S Reader", I2S_TASK_STACK, NULL, I2S_TASK_PRI, NULL);
sum_queue_t q;
uint32_t Leq_samples = 0;
double Leq_sum_sqr = 0;
double Leq_dB = 0;
// Timeout for xQueueReceive
const TickType_t xTicksToWait = pdMS_TO_TICKS(500); // Adjust as needed
TickType_t lastBlynkRunTime = xTaskGetTickCount();
while (true) {
if(xQueueReceive(samples_queue, &q, xTicksToWait)) {
// Read sum of samaples, calculated by 'i2s_reader_task'
// while (xQueueReceive(samples_queue, &q, portMAX_DELAY)) {
// Calculate dB values relative to MIC_REF_AMPL and adjust for microphone reference
double short_RMS = sqrt(double(q.sum_sqr_SPL) / SAMPLES_SHORT);
double short_SPL_dB = MIC_OFFSET_DB + MIC_REF_DB + 20 * log10(short_RMS / MIC_REF_AMPL);
// In case of acoustic overload or below noise floor measurement, report infinty Leq value
if (short_SPL_dB > MIC_OVERLOAD_DB) {
Leq_sum_sqr = INFINITY;
} else if (isnan(short_SPL_dB) || (short_SPL_dB < MIC_NOISE_DB)) {
Leq_sum_sqr = -INFINITY;
}
// Accumulate Leq sum
Leq_sum_sqr += q.sum_sqr_weighted;
Leq_samples += SAMPLES_SHORT;
// When we gather enough samples, calculate new Leq value
if (Leq_samples >= SAMPLE_RATE * LEQ_PERIOD) {
double Leq_RMS = sqrt(Leq_sum_sqr / Leq_samples);
Leq_dB = MIC_OFFSET_DB + MIC_REF_DB + 20 * log10(Leq_RMS / MIC_REF_AMPL);
Leq_sum_sqr = 0;
Leq_samples = 0;
// Serial output, customize (or remove) as needed
Serial.printf("%.1f\n", Leq_dB);
Blynk.virtualWrite(V1, Leq_dB);
// Debug only
//Serial.printf("%u processing ticks\n", q.proc_ticks);
}
}
// Check if 10 seconds have passed since the last Blynk.run()
if (xTaskGetTickCount() - lastBlynkRunTime > pdMS_TO_TICKS(10000)) {
Blynk.run();
lastBlynkRunTime = xTaskGetTickCount(); // Update the last run time
}
}
}