我将麦克风和扬声器连接到 2 个 ESP32,并且能够使用 ESP32 从麦克风听到扬声器再现的我的声音。我也能够在他们之间发送音频信号,但我无法理解口语。我认为这是因为我正在从analogRead 函数发送当前值,并且由于 ESP Now 链接的数据速率低于 adc 采样率,因此传输中会丢失很多信息。
我尝试使用下面的代码创建一个包含 1000 个值的数组并一次发送一个数组,但它对我不起作用。感谢您的帮助,我需要在 2 个 ESP32 之间发送更高质量的音频。
#include <esp_now.h>
#include <WiFi.h>
// REPLACE WITH THE MAC Address of your receiver
uint8_t broadcastAddress[] = {0x7C,0x9E,0xBD,0x47,0x92,0x4C}; //MAC Address
float A;
const int Max = 1000;
//Define Sent Message
typedef struct test_message {
float A[Max];
} test_message;
test_message tx; //Sent Message
test_message rx; // Received Message
// Transmitting Callback
void OnDataSent(const uint8_t *mac_addr,esp_now_send_status_t status) {
char macStr[18];
// Copies the sender mac address to a string
snprintf(macStr,sizeof(macStr),"%02x:%02x:%02x:%02x:%02x:%02x:",mac_addr[0],mac_addr[1],mac_addr[2],mac_addr[3],mac_addr[4],mac_addr[5]);
}
// Receiving Callback
void OnDataRecv(const uint8_t * mac,const uint8_t *incomingData,int len) {
memcpy(&rx,incomingData,sizeof(rx));
}
void setup() {
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
// Serial.println("Error initializing ESP-NOW");
return;
esp_now_register_recv_cb(OnDataRecv);
}
// Once ESPNow is successfully Init,we will register for Send CB to
// get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);
// Register peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr,broadcastAddress,6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK){
// Serial.println("Failed to add peer");
return;
}
// Register for a callback function that will be called when data is received
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
for (int i=0; i<Max; i++){
tx.A[i] = analogRead(35);
}
esp_err_t result = esp_now_send(0,(uint8_t *) &tx,sizeof(test_message));
for (int i=0; i<Max; i++){
dacWrite(25,rx.A[i]);
Serial.println(rx.A[i]);
}
}