I2S Stereo to play mp3,TTS from flash on boot

Hi guys, I have came up with Stereo I2S player, complete project and STL files can be downloaded from here:

Code for ESPHome below:

esphome:
  name: wifi-stereo-01
  friendly_name: WiFi_Stereo_01
  on_boot:
  # Simple with lambda
  - media_player.volume_set: !lambda "return 0.3;"
  
esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "<yourencriptioncode here>"

ota:
  password: "<yourpassword>"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Wifi-Stereo-01 Fallback Hotspot"
    password: "<your hotspot fallback pass>"

captive_portal:

status_led:
  pin:
    number: GPIO2
    inverted: true

i2s_audio:
  - id: i2s_out
    i2s_lrclk_pin: GPIO26 #not used, instead 13 and 14 used for Stereo left and right
    i2s_bclk_pin: GPIO27

# LR to be grounded

media_player:
  - platform: i2s_audio
    name: ESPHome I2S Media Player
    dac_type: external
    i2s_audio_id: i2s_out
    i2s_dout_pin: GPIO25
    mode: stereo

sensor:
  - platform: internal_temperature
    name: "Internal Temperature"

#Bluetooth proxy - not enough memory!
#esp32_ble_tracker:
  #scan_parameters:
    #interval: 1100ms
    #window: 1100ms
    #active: true

#bluetooth_proxy:
  #active: true

File examples attached:
upps, JBL start and shutdown sounds, you can search internet for those, each is 23kb

Pics:




Components required for project:

  1. 24V 30W Hi-Link PSU module 1EA
  2. ESP32 WROOM32 dev board 1EA
  3. 0.96inch I2C Oled Display 12864 1EA
  4. Touch buttons PPT223 2EA
  5. M3 Bolts 40mm 4EA
  6. M3 self drive in nuts for plastic 4EA
  7. Converter board DC-DC Mp1584en 3A 1EA - adjust 24v to 5V before soldering to ESP , touch and OLED, i2s modules!!!
  8. Amp board D class Tpa3116d2 Stereo 2x120w
  9. I2S stereo module UDA1334A
  10. 24V 40x40 DC fan + 2N2222 transistor and 4.7K resistor for PWM control on GPIO33

UPDATES:

Now little fan 40x40 PWM controlled using just one 2N2222 transistor and 4.7K resistor per circuit diagram 1:

HA screen:

Updated code:

substitutions:
  device_name: wifi-stereo-01
  friendly_name: "wifi-stereo-01"
  device_description: "Fan with speed control"
  temperature_threshold_low: "30" # At what temperature, in celcius, should the fan turn on to its minimum speed
  temperature_threshold_high: "50" # At what temperature, in celcius, should the fan turn on to its maximum speed
  minimum_fan_speed: "15" # What is the minimum fan speed, as a percentage

esphome:
  friendly_name: WiFi_Stereo_01
  name: '${device_name}'
  comment: '${device_description}'
  
  on_boot:
  # Simple with lambda
  - media_player.volume_set: !lambda "return 0.3;"



esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "<your encriptioncode>"

ota:
  password: "<yourota pass>"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Wifi-Stereo-01 Fallback Hotspot"
    password: "<yourfallback pass>"

captive_portal:

status_led:
  pin:
    number: GPIO2
    inverted: true

i2s_audio:
  - id: i2s_out
    i2s_lrclk_pin: GPIO26 #not used, instead 13 and 14 used for Stereo left and right
    i2s_bclk_pin: GPIO27

# LR to be grounded

media_player:
  - platform: i2s_audio
    name: ESPHome I2S Media Player
    dac_type: external
    i2s_audio_id: i2s_out
    i2s_dout_pin: GPIO25
    mode: stereo

sensor:
  - platform: internal_temperature
    update_interval: 10s
    name: "${device_description} Temperature"
    id: temperature_sensor
    on_value:
      then:
        - script.execute: set_fan_state


#Bluetooth proxy - not enough memory!
#esp32_ble_tracker:
  #scan_parameters:
    #interval: 1100ms
    #window: 1100ms
    #active: true

#bluetooth_proxy:
  #active: true

# Fan control

output:
  - platform: ledc
    pin: GPIO33
    id: pwm_output
    frequency: 1000Hz

fan:
  - platform: speed
    output: pwm_output
    name: "Colling Fan"
    id: "the_fan"

# Sets the speed of the fan based on a linear calculation
# between the high and low temperature thresholds and
# the minimum specified fan speed
script:
  - id: set_fan_state
    then:
      - if:
          condition:
            lambda: |-
              return id(temperature_sensor).state < id(${temperature_threshold_low});
          then:
            - fan.turn_off: the_fan
          else:
            - fan.turn_on:
                id: the_fan
                speed: !lambda |-
                  if (id(temperature_sensor).state >= id(${temperature_threshold_high})) {
                    // Over upper threshold, fan speed at maximum
                    ESP_LOGD("Fan speed calc", "Temperature is above or equal to upper threshold so setting to max");
                    return 100;
                  }
                  else {
                    float calc_speed = ((100-id(${minimum_fan_speed})) / (id(${temperature_threshold_high})-id(${temperature_threshold_low})))*(id(temperature_sensor).state-id(${temperature_threshold_low}))+id(${minimum_fan_speed});
                    ESP_LOGD("Fan speed calc", "calculated speed = %f", calc_speed);
                    return calc_speed;
                  }

Things to sort out to further improve project and I need your help with that:

  1. I2C code to display useful info and touch button controls
  2. how to upload small mp3 files into flash memory, as an option to make ESP board to TTS sound on start and shutdown.
  3. Have tried to add BLE functionality, however with arduino framework code overfill memory, so need to wait till developers will come with esf framework for i2s mediaplayers
  4. Couldn’t get playlist figure out so mediaplayer plays from it, for the moment using default HA mediaplayer.
  5. Couldn’t figure out how to play over HTTPS
3 Likes

Just curious. Are you running the latest Esphome version? I have similar setup and it was working at one point. After some updates esphome, I am unable to play mp3. No audio coming out of the speaker and no error message. tts works only.

Are you trying to play from https addresses or http? Since some time ago https do not work, but you are able to use http links to play mp3.
Easiest way to check is, try to play internet radio, then if device works, play mp3. Try different mp3s, some mp3s do not work as firmware do not support some bitrates and Hz samples.

I am playing with http. These tracks were working before. I have never changed the mp3 file since. If I paste the url in a browser it works. Just odds how it stopped working one day. One day meaning almost half a year ago. I believe the internet radio is working. I tested before. Just mp3 files do not work. I even shorten the file name to number (1.mp3)

İf internet radio works, there is no reason why your device does not play mp3s which worked before.

  1. Try to flash firmware again,
  2. build another device with the same components for testing purposes
  3. share full code here so I can test.
  4. What components has been used
  5. what version of ESPHome you are using?
  6. What media player is used to play files?
  7. Try to delete files and upload new ones to the server and try.
  8. upload one mp3 file which worked before but refuses to play so I can test.

Updated code, added restart button and wifi signal level:

substitutions:
  device_name: wifi-stereo-01
  friendly_name: "wifi-stereo-01"
  device_description: "Fan with speed control"
  temperature_threshold_low: "25" # At what temperature, in celcius, should the fan turn on to its minimum speed
  temperature_threshold_high: "50" # At what temperature, in celcius, should the fan turn on to its maximum speed
  minimum_fan_speed: "15" # What is the minimum fan speed, as a percentage

esphome:
  friendly_name: WiFi_Stereo_01
  name: '${device_name}'
  comment: '${device_description}' 
  on_boot:
   - priority: 300.0 # after sensor setup, before WIFI initialization 
     then:
      - media_player.volume_set: !lambda "return 0.3;"
      #- lambda: |-
          #it.play_wav(file::jblst.pcm)

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "<key>"

ota:
  password: "<pass>"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Wifi-Stereo-01 Fallback Hotspot"
    password: "<pass>"

captive_portal:

status_led:
  pin:
    number: GPIO2
    inverted: true

i2s_audio:
  - id: i2s_out
    i2s_lrclk_pin: GPIO26 #not used, instead 13 and 14 used for Stereo left and right
    i2s_bclk_pin: GPIO27

# LR to be grounded

media_player:
  - platform: i2s_audio
    name: ESPHome I2S Media Player
    dac_type: external
    i2s_audio_id: i2s_out
    i2s_dout_pin: GPIO25
    mode: stereo

sensor:
  - platform: internal_temperature
    update_interval: 10s
    name: "${device_description} Temperature"
    id: temperature_sensor
    on_value:
      then:
        - script.execute: set_fan_state

# Example configuration entry with 2 sensors and filter
  - platform: wifi_signal # Reports the WiFi signal strength/RSSI in dB
    name: "WiFi Signal dB"
    id: wifi_signal_db
    update_interval: 60s
    entity_category: "diagnostic"

  - platform: copy # Reports the WiFi signal strength in %
    source_id: wifi_signal_db
    name: "WiFi Signal Percent"
    filters:
      - lambda: return min(max(2 * (x + 100.0), 0.0), 100.0);
    unit_of_measurement: "Signal %"
    entity_category: "diagnostic"

#Bluetooth proxy - not enough memory!
#esp32_ble_tracker:
  #scan_parameters:
    #interval: 1100ms
    #window: 1100ms
    #active: true

#bluetooth_proxy:
  #active: true

# Fan control

output:
  - platform: ledc
    pin: GPIO33
    id: pwm_output
    frequency: 1000Hz

fan:
  - platform: speed
    output: pwm_output
    name: "Colling Fan"
    id: "the_fan"

# Sets the speed of the fan based on a linear calculation
# between the high and low temperature thresholds and
# the minimum specified fan speed
script:
  - id: set_fan_state
    then:
      - if:
          condition:
            lambda: |-
              return id(temperature_sensor).state < id(${temperature_threshold_low});
          then:
            - fan.turn_off: the_fan
          else:
            - fan.turn_on:
                id: the_fan
                speed: !lambda |-
                  if (id(temperature_sensor).state >= id(${temperature_threshold_high})) {
                    // Over upper threshold, fan speed at maximum
                    ESP_LOGD("Fan speed calc", "Temperature is above or equal to upper threshold so setting to max");
                    return 100;
                  }
                  else {
                    float calc_speed = ((100-id(${minimum_fan_speed})) / (id(${temperature_threshold_high})-id(${temperature_threshold_low})))*(id(temperature_sensor).state-id(${temperature_threshold_low}))+id(${minimum_fan_speed});
                    ESP_LOGD("Fan speed calc", "calculated speed = %f", calc_speed);
                    return calc_speed;
                  }

switch:
  - platform: restart
    name: "Stereo Speaker restart"

Some updates.
I tested a few radio station url. Some will play and some don’t. I get failed in log.
I have tried several other mp3 files and all will not play. However, mp3 files that are short ring tones or short messages will play.

This link from a user said his device stopped working with ESP32-audioI2S 2.1.0 v. Reverting back to esphome 2022.11.5 works.

I found this link from another user on steps on downgrading an add-on. Haven’t tried it and have questions before attempting to do so.

Esphome current version installed is 2023.6.5
I am using this DAC Module.

Razer Leviathan sound bar is the speaker I am using.

Check your mp3 files then. I’m using latest esphome, latest HA and same hardware.
Some radio stations stopped streaming online most likely thats why you can’t play those or streaming some other way than before, format might be different.
For mp3s - most of the mp3 files work just fine, others don’t, guess this is related to file names and format.
Another thing to pay attention is chip selection, Im using devboard listed in above article, tested S2 too, but it is too weak to play mp3s, radio etc as has only one core.
Device is very sensitive to signal strength, there for I have added code to display WiFi signal strength, position your device to keep it above 50%, 65-70% is perfect.
Sometimes device hangs-up for unknown reason, it still reacts to interface commands but refuses to play, guess that is my TV from which I normaly control HA, might be something else, not sure.
I have added soft reset to the interface - helps.
Esphome continuousely getting improvements, all required from end users - to go to github and submit issue or feature requests, someone from developers will either fix issue in next release or develop new feature.
For sure due to software being free that takes quite a time, people has their own businesses and HA developed on free time as a hobby.

I guess it boils down to the device that I am using or the mp3 files. I don’t know what chip I am using, but it is so strange it has been working after I finished setting it up. Never had any issues with playback when I used it. Then it stopped working one day. I didn’t change any settings. Just upgrade Ha and esphome whenever there was an update.

Thanks for the info.

I have lilygo ttgo v1 board where lora portion maybe defective. I may just tried to setup another player with it, but will need to purchase another dac since the one I have now has been soldered onto the board. I can’t find correct info if it is a single or dual core.

Chipset:ESPRESSIF-ESP32 (Wi-Fi & Bluetooth)240MHz Xtensa® single-/dual-core 32-bit LX6 microprocessor

So I got it fixed. It was the mp3 files after all. I used Audacity to reformat some of the mp3 files and it works. This is a shame because I want to mainly use this setup to play my music collections. Having to upload the files to media folder is one thing, but I will need to reformat each files just so this device can play would be redundant.

Check you wifi signal, I had around 30% of mp3 files unplayable and some of MP3 files was playing with a lot of interruptions, some radio channels even played with poor quality. Today I have finally replaced my router with very good one from ASUS TUF, gaming one with WiFi6 support and you know what? Im able to play everything smooth, even those mp3 files which couldn’t be played, now play perfectly fine, no interruptions whatsoever.
So check your router, problem could be in it.

Back Horn speaker implementation: Backhorn Speaker V4 by elik745i - Thingiverse




New design, 17cm drivers, way better than 4’’ speakers.


Have a nice day. I would like to request an overall wiring diagram of the updated version with a fan. Thank you in advance