请看看我的代码和错误信息,搞了几天还是不行。帮帮忙,谢谢。
main.c
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "nvs_flash.h"
// ESP-ADF Bluetooth components
#include "bt_service.h"
#include "a2dp_stream.h"
#include "periph_bluetooth.h"
// Pin definitions
#define MOTION_SENSOR_GPIO GPIO_NUM_4 // RCWL-0516 OUT pin
#define LED_GPIO GPIO_NUM_2 // Built-in LED (optional)
static const char *TAG = "BIRD_DETERRENT";
// Bluetooth configuration
static const char *SPEAKER_NAME = "BT SPEAKER"; // Change to your speaker's name
static bool speaker_connected = false;
// Motion detection state
static QueueHandle_t motion_queue;
static bool is_playing = false;
static uint32_t last_trigger_time = 0;
// Timing constants (milliseconds)
#define PLAY_DURATION_MS 8000 // How long eagle call plays
#define COOLDOWN_MS 15000 // Minimum time between triggers
#define SENSOR_HOLD_TIME_MS 2000 // RCWL-0516 internal hold time
/**
* Bluetooth event handler for connection status
*/
static void bt_event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
switch (event_id) {
case PERIPH_BLUETOOTH_CONNECTED:
ESP_LOGI(TAG, "
speaker_connected = true;
break;
case PERIPH_BLUETOOTH_DISCONNECTED:
ESP_LOGI(TAG, "
speaker_connected = false;
break;
case PERIPH_BLUETOOTH_AUDIO_STARTED:
ESP_LOGI(TAG, "Audio streaming started");
break;
case PERIPH_BLUETOOTH_AUDIO_STOPPED:
ESP_LOGI(TAG, "Audio streaming stopped");
break;
default:
break;
}
}
/**
* Send AVRCP Play command to the speaker
*/
static void send_play_command(void)
{
if (!speaker_connected) {
ESP_LOGW(TAG, "Speaker not connected - cannot send PLAY command");
return;
}
ESP_LOGI(TAG, "Sending PLAY command to speaker");
// Use ESP-ADF's AVRCP passthrough command function [citation:1]
periph_bt_play();
// The speaker will play the audio from its SD card
ESP_LOGI(TAG, "
}
/**
* Send AVRCP Pause command to the speaker
*/
static void send_pause_command(void)
{
if (!speaker_connected) return;
ESP_LOGI(TAG, "Sending PAUSE command");
periph_bt_pause(); // AVRCP pause command [citation:1]
}
/**
* GPIO interrupt handler for motion sensor
*/
static void IRAM_ATTR motion_isr_handler(void* arg)
{
int pin = (int)arg;
xQueueSendFromISR(motion_queue, &pin, NULL);
}
/**
* Initialize the RCWL-0516 motion sensor with interrupt
*/
static void motion_sensor_init(void) // line 244
{
gpio_config_t io_conf = {};
io_conf.intr_type = GPIO_INTR_ANYEDGE; // Interrupt on both edges
io_conf.pin_bit_mask = (1ULL << MOTION_SENSOR_GPIO);
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
gpio_config(&io_conf);
// Create queue for interrupt events
motion_queue = xQueueCreate(10, sizeof(int));
// Install ISR service and handler
gpio_install_isr_service(0);
gpio_isr_handler_add(MOTION_SENSOR_GPIO, motion_isr_handler,
(void*)MOTION_SENSOR_GPIO);
ESP_LOGI(TAG, "Motion sensor initialized on GPIO %d", MOTION_SENSOR_GPIO);
}
/**
* Initialize Bluetooth in A2DP source mode with AVRCP control
*/
static void bluetooth_init(void) // line 245
{
// Initialize Bluetooth service with A2DP source role
// The ESP-ADF bt_service handles both A2DP and AVRCP [citation:1]
bt_service_config_t bt_config = {
.device_name = "ESP32-Bird-Deterrent",
.mode = BT_MODE_CLASSIC, // Use Classic Bluetooth for A2DP/AVRCP
.a2dp_role = A2DP_ROLE_SOURCE, // ESP32 is the source (transmitter)
.avrc_ctrl_enable = true, // Enable AVRCP controller
.avrc_tg_enable = false,
};
// Start Bluetooth service
bt_service_start(&bt_config);
// Register event handler for connection status [citation:1]
esp_event_handler_register(PERIPH_BLUETOOTH_EVENTS, ESP_EVENT_ANY_ID,
&bt_event_handler, NULL);
ESP_LOGI(TAG, "Bluetooth initialized in A2DP Source mode");
ESP_LOGI(TAG, "Device name: ESP32-Bird-Deterrent");
ESP_LOGI(TAG, "Waiting for connection to speaker: %s", SPEAKER_NAME);
}
/**
* Motion detection task - runs on separate core
*/
static void motion_task(void *arg) // 255
{
int pin_num;
int motion_state = 0;
int last_motion_state = 0;
while (1) {
// Wait for motion interrupt with timeout
if (xQueueReceive(motion_queue, &pin_num, pdMS_TO_TICKS(100))) {
// Read current state
motion_state = gpio_get_level(MOTION_SENSOR_GPIO);
uint32_t current_time = xTaskGetTickCount() * portTICK_PERIOD_MS;
// Detect rising edge (motion starts)
if (motion_state == 1 && last_motion_state == 0) {
ESP_LOGI(TAG, "
// Check cooldown and connection status
if (!is_playing &&
(current_time - last_trigger_time > COOLDOWN_MS)) {
if (speaker_connected) {
// Send PLAY command to start eagle call
send_play_command();
is_playing = true;
last_trigger_time = current_time;
} else {
ESP_LOGE(TAG, "Speaker not connected!");
}
} else if (is_playing) {
ESP_LOGI(TAG, "Already playing, ignoring");
} else {
ESP_LOGI(TAG, "In cooldown period, ignoring");
}
}
last_motion_state = motion_state;
}
// Handle playback timeout - send PAUSE after eagle call finishes
if (is_playing &&
(xTaskGetTickCount() * portTICK_PERIOD_MS - last_trigger_time >
PLAY_DURATION_MS)) {
send_pause_command();
is_playing = false;
ESP_LOGI(TAG, "Playback period ended - PAUSE sent");
}
}
}
/**
* Task to periodically check connection and attempt reconnection
*/
static void connection_monitor_task(void *arg) // line 256
{
while (1) {
if (!speaker_connected) {
ESP_LOGI(TAG, "Attempting to connect to speaker: %s", SPEAKER_NAME);
// The bt_service handles auto-reconnection
// We just need to ensure the speaker is in pairing mode initially
vTaskDelay(pdMS_TO_TICKS(10000)); // Check every 10 seconds
} else {
vTaskDelay(pdMS_TO_TICKS(30000)); // Check every 30 seconds when connected
}
}
}
/**
* Main application entry point
*/
void app_main(void)
{
// Initialize NVS (required for Bluetooth)
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();
}
ESP_ERROR_CHECK(ret);
ESP_LOGI(TAG, "Bird Deterrent System Starting...");
ESP_LOGI(TAG, "Using RCWL-0516 motion sensor + Bluetooth AVRCP");
// Initialize components
motion_sensor_init(); // line 107
bluetooth_init(); // line 131
// Give Bluetooth time to initialize
vTaskDelay(pdMS_TO_TICKS(2000));
// Note: First-time pairing required!
ESP_LOGI(TAG, "\n
ESP_LOGI(TAG, "The ESP32 will connect automatically after pairing is complete\n");
// Create tasks
xTaskCreatePinnedToCore(motion_task, "motion_task", 4096, NULL, 5, NULL, 1); // line 158
xTaskCreatePinnedToCore(connection_monitor_task, "conn_monitor", 3072, NULL, 3, NULL, 0); // line 211
}
CMakeLists.txt/b]
idf_component_register(SRCS "main.c" INCLUDE_DIRS "." REQUIRES bt esp_adf)
CMakeLists.txt/b]
# The following five lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
# "Trim" the build. Include the minimal set of components, main, and anything it depends on.
idf_build_set_property(MINIMAL_BUILD ON)
project(bird_warning-esp32)
error message
tombolo@tom-HP-Pro-3130:~/esp/bird_warning-esp32$ idf.py set-target esp32 fullclean
Executing action: fullclean
Build directory '/home/tombolo/esp/bird_warning-esp32/build' not found. Nothing to clean.
Executing action: set-target
Set Target to: esp32, new sdkconfig will be created.
Running cmake in directory /home/tombolo/esp/bird_warning-esp32/build
Executing "cmake -G Ninja -DPYTHON_DEPS_CHECKED=1 -DPYTHON=/home/tombolo/.espressif/python_env/idf5.5_py3.12_env/bin/python -DESP_PLATFORM=1 -DIDF_TARGET=esp32 -DCCACHE_ENABLE=0 /home/tombolo/esp/bird_warning-esp32"...
-- Found Git: /usr/bin/git (found version "2.43.0")
-- Minimal build - ON
-- The C compiler identification is GNU 14.2.0
-- The CXX compiler identification is GNU 14.2.0
-- The ASM compiler identification is GNU
-- Found assembler: /home/tombolo/.espressif/tools/xtensa-esp-elf/esp-14.2.0_20251107/xtensa-esp-elf/bin/xtensa-esp32-elf-gcc
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /home/tombolo/.espressif/tools/xtensa-esp-elf/esp-14.2.0_20251107/xtensa-esp-elf/bin/xtensa-esp32-elf-gcc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /home/tombolo/.espressif/tools/xtensa-esp-elf/esp-14.2.0_20251107/xtensa-esp-elf/bin/xtensa-esp32-elf-g++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- git rev-parse returned 'fatal: not a git repository (or any of the parent directories): .git'
-- Could not use 'git describe' to determine PROJECT_VER. (是在cmakelists.txt 吗?)
-- Building ESP-IDF components for target esp32
-- ESP-TEE is currently supported only on the esp32c6;esp32h2;esp32c5 SoCs ( ??)
CMake Error at /home/tombolo/esp/esp-adf/esp-idf/tools/cmake/build.cmake:328 (message):
Failed to resolve component 'esp_adf' required by component 'main': unknown (错在字体吗?)
name.
Call Stack (most recent call first):
/home/tombolo/esp/esp-adf/esp-idf/tools/cmake/build.cmake:371 (__build_resolve_and_add_req)
/home/tombolo/esp/esp-adf/esp-idf/tools/cmake/build.cmake:685 (__build_expand_requirements)
/home/tombolo/esp/esp-adf/esp-idf/tools/cmake/project.cmake:740 (idf_build_process)
CMakeLists.txt:8 (project) 怎么改
-- Configuring incomplete, errors occurred!
HINT: The component 'esp_adf' could not be found. This could be because: component name was misspelled, the component was not added to the build, the component has been moved to the IDF component manager, the component has been removed and refactored into some other component or the component may not be supported by the selected target.
Please look out for component in 'https://components.espressif.com' and add using 'idf.py add-dependency' command.
Refer to the migration guide for more details about moved components. 找不到
Refer to the build-system guide for more details about how components are found and included in the build. 找不到
cmake failed with exit code 1, output of the command is in the /home/tombolo/esp/bird_warning-esp32/build/log/idf_py_stderr_output_3318 and /home/tombolo/esp/bird_warning-esp32/build/log/idf_py_stdout_output_3318 不明白