Hello ,
I had this code below working ,but I made a change and now it is not working anymore, even than I have reverted back to the working version. I'm sure that the problem is coming from the version of WiFiManager and ESP32-AudioIS libraries I have installed, but unfortunately I have reformatted my laptop and I have reinstalled versions based on what I could remember from the past but I'm not sure which version I had.
The versions installed in my Arduino IDE are as following:
1. Boards Manager: ESP32 by Espressif System/ Version 2.0.13
2. Libraries : WifiManager by tzapu / Version 2.0.0-rc.1
3. Libraries: ESP32-audioI2S-master by schreibfaul1 / Version 2.0.0
Please , please provide any help with this issue. I'm struggling from hours with this.
Thanks !!
C code is this below:
#include <WiFi.h>
#include <WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h>
#include <SPI.h>
#include <TFT_eSPI.h>
#include "Arduino.h"
#include "Audio.h"
// Initialize TFT
TFT_eSPI tft = TFT_eSPI(240, 240);
// MAX98357A I2S Pin Definitions
#define I2S_BCLK 7 // ESP32-S3 GPIO7 -> BCLK
#define I2S_LRC 8 // ESP32-S3 GPIO8 -> LRC / WS
#define I2S_DOUT 9 // ESP32-S3 GPIO9 -> DIN
// Push Button Pins
#define BTN_VOL_UP 5 // ESP32-S3 GPIO5 -> Switch to 3.3V (Volume Up)
#define BTN_VOL_DOWN 4 // ESP32-S3 GPIO4 -> Switch to 3.3V (Volume Down)
#define BTN_VOL_MUTE 3 // ESP32-S3 GPIO4 -> Switch to 3.3V (Volume Down)
Audio audio;
// Volume Management Settings
int currentVolume = 14; // Default starting volume
const int maxVolume = 20; // Maximum volume limit
const int minVolume = 1; // Minimum volume limit
// Button State & Debounce Tracking
bool lastUpState = LOW;
bool lastDownState = LOW;
unsigned long lastDebounceTimeUp = 0;
unsigned long lastDebounceTimeDown = 0;
const unsigned long debounceDelay = 50; // 50ms delay to prevent bounce glitches
void logMessage(const String &msg) {
Serial.println(msg);
tft.println(msg);
}
void setup() {
Serial.begin(115200);
delay(1000);
// Pin setups with internal pulldowns (Keeps pins LOW when switches aren't pressed)
pinMode(BTN_VOL_UP, INPUT_PULLDOWN);
pinMode(BTN_VOL_DOWN, INPUT_PULLDOWN);
// TFT Setup
tft.begin();
tft.setRotation(2);
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setCursor(0, 8, 2);
logMessage("--- Audio Streamer Booting ---");
// 1. WiFiManager Setup
WiFiManager wm;
wm.setConnectTimeout(20);
wm.setConfigPortalTimeout(180);
logMessage("Connecting to Wi-Fi...");
if (!wm.autoConnect("ESP32_Config_AP", "password123")) {
logMessage("Failed to connect. Restarting...");
delay(2000);
ESP.restart();
}
// Disable WiFi modem sleep to maintain seamless streaming
WiFi.setSleep(false);
tft.setTextColor(TFT_GREEN, TFT_BLACK);
logMessage("\nConnected to Wi-Fi!");
logMessage("IP: " + WiFi.localIP().toString());
delay(1000);
// 2. Initialize Audio AFTER Wi-Fi is fully established
audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
audio.setVolume(currentVolume);
logMessage("\nStarting Audio Stream...");
audio.connecttohost("http://xxxyyy.com");
}
void loop() {
// 1. Audio processing gets top priority
audio.loop();
// 2. Volume Control Logic
unsigned long currentMillis = millis();
// Volume UP (GPIO 5)
bool readingUp = digitalRead(BTN_VOL_UP);
if (readingUp == HIGH && lastUpState == LOW) {
if ((currentMillis - lastDebounceTimeUp) > debounceDelay) {
lastDebounceTimeUp = currentMillis;
if (currentVolume < maxVolume) {
currentVolume++;
audio.setVolume(currentVolume);
tft.setTextColor(TFT_YELLOW, TFT_BLACK);
logMessage("Vol +: " + String(currentVolume) + " / " + String(maxVolume));
} else {
tft.setTextColor(TFT_RED, TFT_BLACK);
logMessage("Vol Max: " + String(maxVolume));
}
}
}
lastUpState = readingUp;
// Volume DOWN (GPIO 4)
bool readingDown = digitalRead(BTN_VOL_DOWN);
if (readingDown == HIGH && lastDownState == LOW) {
if ((currentMillis - lastDebounceTimeDown) > debounceDelay) {
lastDebounceTimeDown = currentMillis;
if (currentVolume > minVolume) {
currentVolume--;
audio.setVolume(currentVolume);
tft.setTextColor(TFT_YELLOW, TFT_BLACK);
logMessage("Vol -: " + String(currentVolume) + " / " + String(maxVolume));
} else {
tft.setTextColor(TFT_RED, TFT_BLACK);
logMessage("Vol Min (1)");
}
}
}
lastDownState = readingDown;
// 3. Non-blocking Wi-Fi reconnect check
if (WiFi.status() != WL_CONNECTED) {
static unsigned long lastReconnectAttempt = 0;
if (currentMillis - lastReconnectAttempt >= 5000) {
lastReconnectAttempt = currentMillis;
Serial.println("[Wi-Fi] Connection lost. Attempting background reconnect...");
WiFi.reconnect();
}
}
}
// Audio Info Callbacks
void audio_info(const char *info) {
Serial.print("info "); Serial.println(info);
}
void audio_showstation(const char *info) {
tft.setTextColor(TFT_CYAN, TFT_BLACK);
logMessage("Station: " + String(info));
}
void audio_showstreamtitle(const char *info) {
tft.setTextColor(TFT_MAGENTA, TFT_BLACK);
logMessage("Now Playing: " + String(info));
}