Lesson2 Introduce the screen user interface and external speakers for playing songs¶
Welcome to the second lesson of learning. I believe that after completing the first lesson, everyone has gained a deeper understanding of our product. Next, I will lead you to the first application case of the Advance series products - connecting to WiFi to play songs.
1. Using previous preparations¶
Note:Plug a speaker into the "SPK" interface,Connect the development board to the computer with a Type-C cable.
Then connect the speaker to the Crowpanel-Advance-HMI-AI-Display.
This is the connection diagram for the 2.4-inch / 2.8-inch speaker.
2. Explanation of the code project¶
This project is developed and run on ESP-IDF version 5.5.4.
First, you need to specify the version of the arduino-esp32 component in the idf_component.yml file under the main folder. In this project, version 3.3.10 is used to ensure compatibility with ESP-IDF 5.5.4. After the configuration is completed, the ESP-IDF build system will automatically download and integrate the specified Arduino framework version during the first compilation.
This setup allows the project to combine the ESP-IDF development environment with the Arduino programming interface, enabling the use of Arduino-style APIs within an ESP-IDF-based project.
This section defines two string constants to store the WiFi name and connection password of the target router. The use of "const" modifier ensures that the account password will not be accidentally modified during the running process. The global scope enables all functions in the entire project to directly read this WiFi parameter. The subsequent WiFi connection function will read these two variables to complete the wireless network configuration.
Use your own WiFi account and password.
This section defines the global string constant to store the public MP3 external link address of NetEase Cloud Music. The audio library will access this address via the TCP network to retrieve the online audio data stream. Subsequent playback functions will directly call this address to achieve online song playback. The audio resources come from the online HTTP streaming media, and there is no need for local storage of audio files.
The song must be free and not a VIP song. And you just need to replace the song code "1391891631" with another one.
This section implements the blocking networking logic for ESP32 to connect to a specified WiFi hotspot. Firstly, it prints the WiFi name to be connected via the serial port, then calls WiFi.begin to start the wireless connection process. It enters an infinite loop to continuously monitor the WiFi connection status. If the connection is not successful, it prints a dot marker every 500 milliseconds to indicate the progress. Only when the wireless connection status becomes "connected" will the loop be exited and the connection success log will be printed. The program will remain stuck in this function until the network connection is completed and will not execute the subsequent audio and screen initialization code.
This section completes the audio hardware configuration and the online streaming media startup process. First, it binds the pre-defined three I2S audio pins to the global audio object, sets the global playback volume value to 20, calls the network connection interface to access the pre-stored MP3 link of NetEase Cloud, automatically completes the network request, audio buffering, MP3 decoding, and I2S output entire process. Finally, it prints the current playing audio address through the serial port, and after the execution is completed, the hardware can output the music sound.
This section represents the main logic that runs in an infinite loop after the program is powered on. The core continuously calls the "loop" refresh function of the audio library. This function is responsible for maintaining the network audio buffer, continuously decoding MP3 data, and pushing the audio signal to the I2S hardware output. It is the key to ensuring that the music does not lag or stop. It is accompanied by a 10-millisecond delay to appropriately release CPU resources, avoiding the audio process monopolizing the chip's computing power. There are no other additional business logics. The entire process only maintains the audio playback state.
This section is the program startup entry adapted for the native development framework of ESP-IDF. The "extern "C"" ensures that the underlying C language kernel can properly call this function. Firstly, the initArduino function initializes the Arduino runtime environment, then directly calls the Arduino standard setup to complete all hardware network initialization. Subsequently, a permanent while loop is started to continuously execute the loop audio refresh logic. A 1-millisecond short delay is added to schedule other tasks for the system, allowing this Arduino syntax code to be compiled and run directly in the ESP-IDF project without relying on the Arduino IDE.
3. Compile and flash the code¶
Click the link below and you can access the code for this lesson.
Code Link:
2.4 / 2.8 inch V1.1、V1.2:
Select the code folder for this lesson, right-click on it, and then choose to open it with VS Code.
Note:
Remember to replace the Wi-Fi name and Wi-Fi password you are using at the beginning of the main.cpp file.
Then, following Step 5 “Run the program” from Lesson 1, upload and execute the code in the correct sequence.
Please proceed in the following order: 1 → 2 → 3 → 4 → 5 → 6 → 10
After completing Step 10, you will be able to hear the song playing through the speaker connected to the SPK output.









