Submit the data to an HTTP endpoint

This commit is contained in:
Thomas Kolb 2022-07-24 00:18:10 +02:00
parent 1a9ac19d60
commit 75611bb4e1
1 changed files with 44 additions and 19 deletions

View File

@ -2,6 +2,7 @@
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#include <AutoAnalogAudio.h>
@ -19,7 +20,7 @@ extern "C" {
#define ENDLESS_LOOP() while(true) { delay(100); }
#define OUTPUT_INTERVAL 1000 // milliseconds
#define OUTPUT_INTERVAL 10000 // milliseconds
#define SAMPLES_PER_BLOCK 512
@ -89,7 +90,7 @@ void wifi_setup(void)
void setup()
{
Serial.begin(115200);
Serial.begin(115300);
pinMode(LED_BUILTIN, OUTPUT);
@ -134,8 +135,8 @@ void loop() {
static uint32_t last_output_time = 0;
static uint32_t nsamples = 0;
static value_type total_energy_0_to_200_hz = 0.0f;
static value_type total_energy_200_to_3500_hz = 0.0f;
static value_type total_energy_0_to_300_hz = 0.0f;
static value_type total_energy_300_to_3500_hz = 0.0f;
static value_type total_energy_3500_to_8000_hz = 0.0f;
static value_type total_energy_8000_to_20000_hz = 0.0f;
@ -165,34 +166,34 @@ void loop() {
fft_transform(timedomain, fft_re, fft_im);
complex_to_absolute(fft_re, fft_im, fft_abs);
value_type energy_0_to_200_hz = get_energy_density_in_band(fft_abs, 0, 200);
value_type energy_200_to_3500_hz = get_energy_density_in_band(fft_abs, 200, 3500);
value_type energy_0_to_300_hz = get_energy_density_in_band(fft_abs, 0, 300);
value_type energy_300_to_3500_hz = get_energy_density_in_band(fft_abs, 300, 3500);
value_type energy_3500_to_8000_hz = get_energy_density_in_band(fft_abs, 3500, 8000);
value_type energy_8000_to_20000_hz = get_energy_density_in_band(fft_abs, 8000, 20000);
total_energy_0_to_200_hz += energy_0_to_200_hz;
total_energy_200_to_3500_hz += energy_200_to_3500_hz;
total_energy_0_to_300_hz += energy_0_to_300_hz;
total_energy_300_to_3500_hz += energy_300_to_3500_hz;
total_energy_3500_to_8000_hz += energy_3500_to_8000_hz;
total_energy_8000_to_20000_hz += energy_8000_to_20000_hz;
nsamples++;
uint32_t now = millis();
if(now - last_output_time > OUTPUT_INTERVAL) {
total_energy_0_to_200_hz /= nsamples;
total_energy_200_to_3500_hz /= nsamples;
total_energy_0_to_300_hz /= nsamples;
total_energy_300_to_3500_hz /= nsamples;
total_energy_3500_to_8000_hz /= nsamples;
total_energy_8000_to_20000_hz /= nsamples;
// calculate dBV
value_type dBV_per_Hz_0_to_200_hz = 20*log10(total_energy_0_to_200_hz);
value_type dBV_per_Hz_200_to_3500_hz = 20*log10(total_energy_200_to_3500_hz);
value_type dBV_per_Hz_0_to_300_hz = 20*log10(total_energy_0_to_300_hz);
value_type dBV_per_Hz_300_to_3500_hz = 20*log10(total_energy_300_to_3500_hz);
value_type dBV_per_Hz_3500_to_8000_hz = 20*log10(total_energy_3500_to_8000_hz);
value_type dBV_per_Hz_8000_to_20000_hz = 20*log10(total_energy_8000_to_20000_hz);
Serial.print(" 0 - 200 Hz [dBV/Hz]: ");
Serial.println(dBV_per_Hz_0_to_200_hz);
Serial.print(" 200 - 3500 Hz [dBV/Hz]: ");
Serial.println(dBV_per_Hz_200_to_3500_hz);
Serial.print(" 0 - 300 Hz [dBV/Hz]: ");
Serial.println(dBV_per_Hz_0_to_300_hz);
Serial.print(" 300 - 3500 Hz [dBV/Hz]: ");
Serial.println(dBV_per_Hz_300_to_3500_hz);
Serial.print(" 3500 - 8000 Hz [dBV/Hz]: ");
Serial.println(dBV_per_Hz_3500_to_8000_hz);
Serial.print(" 8000 - 20000 Hz [dBV/Hz]: ");
@ -200,10 +201,34 @@ void loop() {
Serial.println();
// TODO: send the data to graphite
// encode the data into JSON
String json_string = "{"
"\"0_to_300_hz\":" + String(dBV_per_Hz_0_to_300_hz, 2) + ","
"\"300_to_3500_hz\":" + String(dBV_per_Hz_300_to_3500_hz, 2) + ","
"\"3500_to_8000_hz\":" + String(dBV_per_Hz_3500_to_8000_hz, 2) + ","
"\"8000_to_20000_hz\":" + String(dBV_per_Hz_8000_to_20000_hz, 2) + "}";
total_energy_0_to_200_hz = 0.0f;
total_energy_200_to_3500_hz = 0.0f;
// send the data to graphite
HTTPClient client;
client.begin("http://stats.tkolb.de/sensor/loudness.php");
client.addHeader("Content-Type", "application/json");
int httpResponseCode = client.POST(json_string);
if(httpResponseCode>0) {
Serial.print("HTTP request result: ");
Serial.println(httpResponseCode); //Print return code
//String response = http.getString(); //Get the response to the request
//Serial.println(response); //Print request answer
} else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
// reset accumulation
total_energy_0_to_300_hz = 0.0f;
total_energy_300_to_3500_hz = 0.0f;
total_energy_3500_to_8000_hz = 0.0f;
total_energy_8000_to_20000_hz = 0.0f;
nsamples = 0;