Reputation: 1
I'm developing a "Radio Baby" project using an ESP32 with a 433MHz radio module. I'm using an INMP441 microphone with I2S for audio input. For radio transmission, I'm using WL101-341 and WL102-341 modules with the RadioHead library.
Initially, I encountered issues with the RadioHead library's limited speed, which resulted in choppy audio transmission in real-time. To address this, I integrated the esp32_opus codec, which significantly improved audio quality. However, I'm now facing a problem: while the Opus codec works within the FreeRTOS environment, I'm unable to reliably send radio data using FreeRTOS.
Specifically, the receiving module receives data very infrequently, perhaps once every 1-3 minutes, despite the transmitting module sending data in loop 3+ times per second. I've investigated potential causes, but haven't been able to pinpoint the issue.
Could anyone provide guidance or suggestions on how to troubleshoot this problem? I'm using FreeRTOS for task management and the RadioHead library for radio communication.
Here is code of sending audio and receiving.
TX (Transmitter)
//config
RH_ASK driver(8000, 25, 33, 32);
i2s_config_t i2s_mic_config = {
.mode = (i2s_mode_t) (I2S_MODE_MASTER | I2S_MODE_RX),
.sample_rate = SOUND_SAMPLE_RATE,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
.communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_STAND_I2S),
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = 2,
.dma_buf_len = 256,
.use_apll = false,
.tx_desc_auto_clear = true,
.fixed_mclk = -1
};
i2s_pin_config_t i2s_mic_pin_config = {
.bck_io_num = I2S_SCK,//14
.ws_io_num = I2S_WS, //27
.data_out_num = I2S_PIN_NO_CHANGE,
.data_in_num = I2S_SD //23
};
//packet to send
struct AudioPacket
{
uint8_t fromId = 255; //test value
uint8_t toId = 111; //test value
uint8_t command = 123; //test value
uint8_t dataPtr[50];
};
AudioPacket packet;
//read audio
void readAudio(void *param)
{
while (1)
{
if (sendAudioEnabled)
{
if (xSemaphoreTake(mutex, (TickType_t)10) == pdTRUE)
{
memset(packet.dataPtr, 0, 50);
size_t bytes_read = 0;
int16_t *audioBuffer = (int16_t *)malloc(320 * sizeof(int16_t));
Serial.println("audioBuffer");
uint8_t *encodedBuffer = (uint8_t *)malloc(50 * sizeof(uint8_t));
Serial.println("encodedBuffer");
esp_err_t result = i2s_read(I2S_NUM_0, audioBuffer, sizeof(int16_t) * 320, &bytes_read, portMAX_DELAY);
int encoded_size = opus_encode(opus_encoder_, audioBuffer, 320, encodedBuffer, 50);
memcpy(packet.dataPtr, encodedBuffer, encoded_size);
free(audioBuffer);
free(encodedBuffer);
xSemaphoreGive(mutex);
vTaskDelay(1);
}
}
else
{
vTaskDelete(NULL);
}
}
}
//send audio
void loop()
{
if (xSemaphoreTake(mutex, (TickType_t) 0) == pdTRUE)
{
// Attempt to receive a packet from the queue
// Print the packet data
Serial.println("xQueueReceive success");
for (uint8_t i = 0; i < 50; i++)
{
Serial.print(packet.dataPtr[i]);
Serial.print(" ");
} //print to see that audio successfully set to packet
driver.send((uint8_t *) &packet, sizeof(packet));
driver.waitPacketSent();
xSemaphoreGive(mutex);
}
}
RX (Receiver)
//config
RH_ASK driver(8000, 25, 33, 32);
i2s_config_t i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX),
.sample_rate = 8000,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
.communication_format = I2S_COMM_FORMAT_I2S,
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = 2,
.dma_buf_len = 128,
.use_apll = false,
.tx_desc_auto_clear = true,
.fixed_mclk = -1
};
i2s_pin_config_t pinConfig = {
.bck_io_num = -1,
.ws_io_num = -1,
.data_out_num = 26,
.data_in_num = -1
};
//take and play audio from radio
void readAndPlayAudio(void *param)
{
while (1)
{
AudioPacket packet;
if (driver.recv((uint8_t *)&packet, &buflen))
{
int16_t *outData = (int16_t *)malloc(320 * sizeof(int16_t));
for (uint8_t i = 0; i < 50; i++)
{
Serial.print(packet.dataPtr[i]);
Serial.print(" ");
} //for test then data successfully read from radio
int decoded_size = opus_decode(opus_decoder_, packet.dataPtr, 50, outData, 320, 0);
i2s_write(I2S_NUM_0, outData, sizeof(int16_t) * 320, &bytes_written, portMAX_DELAY);
free(outData);
}
}
}
By the way, I also tried take radio data in RX module in loop method, but nothing change.
I tried send raw data, like string, and that data successfully taken in RX module, but with structure I have an issue, and I don't understand the reason of it.
Upvotes: 0
Views: 55