hello everyone, i have project to measure amplitude, RT60 and SPL using max9814 connected with esp32. the data is then sent over to a websocket server, but it happens to have wrong readings. i will provide you with the code and i am hoping that someone will help me because i am a beginner in arduinoIDE, thank you all in advance.
""""CODE""""
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <ArduinoJson.h>
#include <arduinoFFT.h>
// Wi-Fi credentials
const char* ssid = "Monitor Sound";
const char* password = "12345678";
AsyncWebServer server(80);
AsyncWebSocket ws("/ws");
const int sampleRate = 1024; // Number of samples for FFT
double vReal[sampleRate];
double vImag[sampleRate];
const double samplingFrequency = 40000.0; // 40kHz sample rate
ArduinoFFT FFT = ArduinoFFT(vReal, vImag, sampleRate, samplingFrequency);
// Microphone calibration (example values)
const double micSensitivity = 0.02; // Sensitivity in V/Pa
const double referencePressure = 20e-6; // Reference pressure in Pascals
String htmlPage = R"rawliteral(
Sound Amplitude Graph let socket; let amplitude = 0, spl = 0, rt60 = 0; function initWebSocket() {
socket = new WebSocket(`ws://${location.hostname}/ws`);
socket.onmessage = function (event) {
const data = JSON.parse(event.data);
amplitude = data.amplitude;
spl = data.spl;
rt60 = data.rt60;
document.getElementById("amplitude").innerText = amplitude.toFixed(2);
document.getElementById("spl").innerText = spl.toFixed(2);
document.getElementById("rt60").innerText = rt60.toFixed(2);
updateGraph(amplitude);
};
}
let graphData = [];
function updateGraph(newValue) {
const canvas = document.getElementById("graph");
const ctx = canvas.getContext("2d");
// Normalize newValue to canvas height
const normalizedValue = Math.min(canvas.height, Math.max(0, newValue / 100)); // Adjust scaling
graphData.push(normalizedValue);
if (graphData.length > canvas.width) {
graphData.shift();
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.beginPath();
ctx.moveTo(0, canvas.height - graphData[0]);
graphData.forEach((val, index) => {
ctx.lineTo(index, canvas.height - val);
});
ctx.stroke();
}
window.onload = function () {
initWebSocket();
};
</script>
Sound Amplitude Graph
Amplitude: 0
SPL: 0 dB
RT60: 0 seconds
)rawliteral";void processFFT() {
for (int i = 0; i < sampleRate; i++) {
vReal[i] = analogRead(34); // Replace 34 with your ADC pin
vImag[i] = 0;
delayMicroseconds(25); // Adjust for proper sampling (40kHz)
}
FFT.windowing(FFT_WIN_TYP_HAMMING, FFT_FORWARD);
FFT.compute(FFT_FORWARD);
FFT.complexToMagnitude();
double amplitude = 0;
for (int i = 2; i < sampleRate / 2; i++) {
amplitude = max(amplitude, vReal[i]);
}
// SPL Calculation
double spl = 20 * log10(amplitude * micSensitivity / referencePressure);
// RT60 Calculation using energy decay
double energyDecay[sampleRate / 2];
double totalEnergy = 0;
for (int i = 2; i < sampleRate / 2; i++) {
totalEnergy += vReal[i] * vReal[i];
}
double currentEnergy = totalEnergy;
int decayIndex = 2;
while (decayIndex < sampleRate / 2 && currentEnergy > totalEnergy / 1000) { // Approx -60 dB
currentEnergy -= vReal[decayIndex] * vReal[decayIndex];
decayIndex++;
}
double rt60 = (double)decayIndex / samplingFrequency; // Time in seconds
StaticJsonDocument<200> jsonDoc;
jsonDoc["amplitude"] = amplitude;
jsonDoc["spl"] = spl;
jsonDoc["rt60"] = rt60;
String jsonString;
serializeJson(jsonDoc, jsonString);
ws.textAll(jsonString);
}
void onWebSocketEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
AwsEventType type, void *arg, uint8_t *data, size_t len) {
if (type == WS_EVT_CONNECT) {
Serial.printf("Client connected: %u\n", client->id());
} else if (type == WS_EVT_DISCONNECT) {
Serial.printf("Client disconnected: %u\n", client->id());
}
}
void setup() {
Serial.begin(115200);
WiFi.softAP(ssid, password);
IPAddress IP = WiFi.softAPIP();
Serial.print("Access Point started. IP address: ");
Serial.println(IP);
ws.onEvent(onWebSocketEvent);
server.addHandler(&ws);
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send_P(200, "text/html", htmlPage.c_str());
});
server.on("/favicon.ico", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(404);
});
server.begin();
}
void loop() {
processFFT();
delay(100); // Adjust as needed for your application
}
""""WIRING"""""
esp32 D34 -> max9814 out
positive and negative rails are connected correctely