ESP32 with max98357a board works. But I can't use void loop() function for other purposes

mustafaxfe
Posts: 2
Joined: Tue May 17, 2022 8:56 pm

ESP32 with max98357a board works. But I can't use void loop() function for other purposes

Postby mustafaxfe » Fri May 20, 2022 5:23 pm

I have been learning how to program in esp32 using arduino IDE.

I couldn't figure out how to use max98357a board with Esp32-DevKitC. I have tried "Audio.h" and "AudioTools.h" libraries but not is changed.

My first attempt was just testing voice output. My code is below:

Code: Select all

#include "Arduino.h"
#include "WiFi.h"
#include "Audio.h"
 
// Digital I/O used
#define I2S_DOUT      26  // DIN connection
#define I2S_BCLK      27  // Bit clock
#define I2S_LRC       14  // Left Right Clock
 
Audio audio;
 
String ssid =     "MYCROFT";
String password = "145678abc789";
 
void setup() {
    Serial.begin(115200);
    WiFi.disconnect();
    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid.c_str(), password.c_str());
    while (WiFi.status() != WL_CONNECTED) delay(1500);
    audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
    audio.setVolume(21); // 0...21
 
//    audio.connecttohost("http://www.wdr.de/wdrlive/media/einslive.m3u");
//    audio.connecttohost("http://macslons-irish-pub-radio.com/media.asx");
//    audio.connecttohost("http://mp3.ffh.de/radioffh/hqlivestream.aac"); //  128k aac
//     audio.connecttohost("http://mp3.ffh.de/radioffh/hqlivestream.mp3"); //  128k mp3
      audio.connecttohost("http://vis.media-ice.musicradio.com/CapitalMP3"); //  128k mp3
//    audio.connecttospeech("Wenn die Hunde schlafen, kann der Wolf gut Schafe stehlen.", "de");
//    audio.connecttohost("http://media.ndr.de/download/podcasts/podcast4161/AU-20190404-0844-1700.mp3"); // podcast
    
}
 
void loop()
{
    audio.loop();    
}
It works as intended but when I try to add some commands to the loop function, It stops working.

I mean if I change the loop function as below, it stops connecting to the audio stream.

Code: Select all

void loop()
{
    delay(3000);
    Serial.println("Hello");
    audio.loop();    
}
Is it possible to use max98357a with some additional codes apart from just simple audio.loop().

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 with max98357a board works. But I can't use void loop() function for other purposes

Postby ESP_Sprite » Sun May 22, 2022 6:19 am

My guess is that audio.loop only schedules a small chunk of audio to be played next (e.g. 1/10th of a second). In other words, if you call it only once every 3 seconds, like you do right now, you get 3 seconds of silence and 1/10th second of audio. (Maybe not even that as the audio driver may notice it's not called fast enough and just stop).

Easiest way may be to do things at the correct time rather than delay:

Code: Select all

int last_print; //timestamp of the last time we printed "Hi"

void loop() {
    if (millis()-last_print>3000) {//was the last time we printed more than 3s ago?
        Serial.println("Hi!\n"); //Yes - print line
        last_print=millis(); //Update timestamp
   }
   audio.loop();  //Keep on playing audio
}
Alternatively, you could perhaps use multiple tasks to do the audio and your main loop (look for tutorials on ESP multicore Arduino) but the audio library does need to support re-entrancy for that to work without even more complicated stuff like muxes etc.

mustafaxfe
Posts: 2
Joined: Tue May 17, 2022 8:56 pm

Re: ESP32 with max98357a board works. But I can't use void loop() function for other purposes

Postby mustafaxfe » Tue May 24, 2022 5:05 pm

Hi, thanks for the reply.
It solved some parts of my issue. But I also want to set loop times of audio.loop().
For instance, if I set

Code: Select all

audio.connecttohost(tts.getSpeechUrl("Hello, World!").c_str());
I want to hear "Hello, World! once until I set another word to connecthost function.
I figure out to set with this function

Code: Select all

   if (millis()-last_print>3000) {//was the last time we printed more than 3s ago?
       Serial.println("Hi!\n"); //Yes - print line
       last_print=millis(); //Update timestamp
      audio.stopSong();
      audio.connecttohost(tts.getSpeechUrl("Hello There").c_str());
      audio.loop();       
   }

  audio.loop();
But it is still an infinite loop that says "Hello There".
I simply want to call audio's play function for play once.

Who is online

Users browsing this forum: No registered users and 66 guests