esp-eye Camera Audio Input Interworking WebServer Example

piter5
Posts: 2
Joined: Fri Jul 05, 2024 2:18 am

esp-eye Camera Audio Input Interworking WebServer Example

Postby piter5 » Fri Jul 05, 2024 2:29 am

The i2s data is currently not properly recognized.
Microphone recognition source code is extracted and voice recognition is performed when it is driven
In this source code, dB always stays at the 40th line.
I need your help
#include <esp_system.h>
#include <nvs_flash.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_camera.h"
#include "esp_http_server.h"
#include "esp_timer.h"
#include "connect_wifi.h"
#include "esp_log.h"
#include <math.h>
#include "driver/i2s.h"
#include "esp_heap_caps.h"


#define CAM_PIN_PWDN -1
#define CAM_PIN_RESET -1
#define CAM_PIN_XCLK 4
#define CAM_PIN_SIOD 18
#define CAM_PIN_SIOC 23

#define CAM_PIN_D7 36
#define CAM_PIN_D6 37
#define CAM_PIN_D5 38
#define CAM_PIN_D4 39
#define CAM_PIN_D3 35
#define CAM_PIN_D2 14
#define CAM_PIN_D1 13
#define CAM_PIN_D0 34
#define CAM_PIN_VSYNC 5
#define CAM_PIN_HREF 27
#define CAM_PIN_PCLK 25

static const char *TAG = "esp32-cam Webserver";

camera_fb_t *last_fb = NULL;
bool capture_flag = false;

static void print_memory_status()
{
ESP_LOGI(TAG, "Free heap: %d", (unsigned int)esp_get_free_heap_size());
ESP_LOGI(TAG, "Free internal heap: %d", (unsigned int)heap_caps_get_free_size(MALLOC_CAP_INTERNAL));
ESP_LOGI(TAG, "Free PSRAM: %d", (unsigned int)heap_caps_get_free_size(MALLOC_CAP_SPIRAM));
}

static esp_err_t init_camera(void)
{
print_memory_status();

camera_config_t camera_config = {
.pin_pwdn = CAM_PIN_PWDN,
.pin_reset = CAM_PIN_RESET,
.pin_xclk = CAM_PIN_XCLK,
.pin_sccb_sda = CAM_PIN_SIOD,
.pin_sccb_scl = CAM_PIN_SIOC,

.pin_d7 = CAM_PIN_D7,
.pin_d6 = CAM_PIN_D6,
.pin_d5 = CAM_PIN_D5,
.pin_d4 = CAM_PIN_D4,
.pin_d3 = CAM_PIN_D3,
.pin_d2 = CAM_PIN_D2,
.pin_d1 = CAM_PIN_D1,
.pin_d0 = CAM_PIN_D0,
.pin_vsync = CAM_PIN_VSYNC,
.pin_href = CAM_PIN_HREF,
.pin_pclk = CAM_PIN_PCLK,

.xclk_freq_hz = 10000000,
.ledc_timer = LEDC_TIMER_0,
.ledc_channel = LEDC_CHANNEL_0,

.pixel_format = PIXFORMAT_JPEG,
.frame_size = FRAMESIZE_QQVGA,
.jpeg_quality = 15,
.fb_count = 1,
.grab_mode = CAMERA_GRAB_WHEN_EMPTY,
.fb_location = CAMERA_FB_IN_PSRAM
};
esp_err_t err = esp_camera_init(&camera_config);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Camera init failed with error 0x%x", err);
return err;
}
return ESP_OK;
}

void capture_image_task(void *pvParameters)
{
while (true)
{
if (capture_flag)
{
capture_flag = false;

camera_fb_t *fb = esp_camera_fb_get();
if (fb)
{
if (last_fb)
{
esp_camera_fb_return(last_fb);
}
last_fb = fb;
ESP_LOGI(TAG, "Image captured");
}
else
{
ESP_LOGE(TAG, "Failed to capture image, reinitializing camera");
esp_camera_deinit();
init_camera();
}
}

vTaskDelay(pdMS_TO_TICKS(500));
}
}

esp_err_t capture_image_httpd_handler(httpd_req_t *req)
{
if (!last_fb)
{
httpd_resp_set_status(req, "204 No Content");
return httpd_resp_send(req, NULL, 0);
}

esp_err_t res = httpd_resp_set_type(req, "image/jpeg");
if (res == ESP_OK)
{
res = httpd_resp_send(req, (const char *)last_fb->buf, last_fb->len);
}
else
{
ESP_LOGE(TAG, "Failed to set HTTP response type");
}
return res;
}

esp_err_t index_html_handler(httpd_req_t *req)
{
const char* html = "<!DOCTYPE html><html><body>"
"<h1>ESP32 CAM</h1>"
"<img id='capture' src='/capture' width='320' height='240'/>"
"<script>"
"function updateImage() {"
" var img = document.getElementById('capture');"
" img.src = '/capture?' + new Date().getTime();"
"}"
"setInterval(updateImage, 1000);"
"</script>"
"</body></html>";

return httpd_resp_send(req, html, strlen(html));
}

httpd_uri_t uri_root = {
.uri = "/",
.method = HTTP_GET,
.handler = index_html_handler,
.user_ctx = NULL};

httpd_uri_t uri_capture = {
.uri = "/capture",
.method = HTTP_GET,
.handler = capture_image_httpd_handler,
.user_ctx = NULL};

httpd_handle_t setup_server(void)
{
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
httpd_handle_t capture_httpd = NULL;

if (httpd_start(&capture_httpd , &config) == ESP_OK)
{
httpd_register_uri_handler(capture_httpd , &uri_root);
httpd_register_uri_handler(capture_httpd , &uri_capture);
}
else
{
ESP_LOGE(TAG, "Failed to start HTTP server");
}

return capture_httpd;
}


#define I2S_NUM 0
#define SAMPLE_RATE 44100
#define SAMPLE_BITS I2S_BITS_PER_SAMPLE_16BIT
#define I2S_BUF_LEN 512

void measure_decibel_task(void *pvParameters)
{
i2s_config_t i2s_config = {
.mode = I2S_MODE_MASTER | I2S_MODE_RX,
.sample_rate = SAMPLE_RATE,
.bits_per_sample = SAMPLE_BITS,
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
.communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL2,
.dma_buf_count = 8,
.dma_buf_len = I2S_BUF_LEN,
.use_apll = false,
.tx_desc_auto_clear = false,
.fixed_mclk = 0
};

i2s_pin_config_t pin_config = {
.bck_io_num = 26,
.ws_io_num = 32,
.data_out_num = I2S_PIN_NO_CHANGE,
.data_in_num = 33
};

esp_err_t err;

ESP_LOGI(TAG, "Installing I2S driver");
err = i2s_driver_install(I2S_NUM, &i2s_config, 0, NULL);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to install I2S driver: %s", esp_err_to_name(err));
return;
}

ESP_LOGI(TAG, "Setting I2S pins");
err = i2s_set_pin(I2S_NUM, &pin_config);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to set I2S pins: %s", esp_err_to_name(err));
i2s_driver_uninstall(I2S_NUM);
return;
}

ESP_LOGI(TAG, "Setting I2S clock");
err = i2s_set_clk(I2S_NUM, SAMPLE_RATE, SAMPLE_BITS, I2S_CHANNEL_MONO);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to set I2S clock: %s", esp_err_to_name(err));
i2s_driver_uninstall(I2S_NUM);
return;
}

while (1) {

ESP_LOGI(TAG, "Reading I2S data");
int16_t i2s_buffer[I2S_BUF_LEN];

size_t bytes_read;
i2s_read(I2S_NUM, &i2s_buffer, sizeof(i2s_buffer), &bytes_read, 2000);
ESP_LOGI(TAG, "Processing I2S data");

uint32_t sum_squares = 0;
for (int i = 0; i < bytes_read / sizeof(int16_t); ++i) {
sum_squares += i2s_buffer * i2s_buffer;
printf("buffer data: %d\n",i2s_buffer);
}
float rms = sqrt((float)sum_squares / (bytes_read / sizeof(int16_t)));

float decibels = 20 * log10(rms);

printf("RMS: %f dB: %f\n", rms, decibels);
if (decibels >= 39) {
ESP_LOGI(TAG, "dB over");
capture_flag = true;
}
memset(i2s_buffer, 0, sizeof(i2s_buffer));
vTaskDelay(pdMS_TO_TICKS(1500));
}
}

void app_main()
{
esp_err_t err;

// Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}

connect_wifi();

if (wifi_connect_status)
{
err = init_camera();
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Camera init failed: %s", esp_err_to_name(err));
return;
}
setup_server();
ESP_LOGI(TAG, "ESP32 CAM Web Server is up and running\n");


xTaskCreatePinnedToCore(capture_image_task, "capture_image_task", 4096, NULL, 5, NULL, 0);


xTaskCreatePinnedToCore(measure_decibel_task, "measure_decibel_task", 4096, NULL, 6, NULL, 1);
}
}

Who is online

Users browsing this forum: No registered users and 23 guests