2 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 Product Interface Introduction¶
When you power on the product, you will see the following interface.
The following interface was generated by introducing the official firmware of LVGL, and the specific operation will not be explained in this lesson for now. In the next class, we will mainly explain the LVGL graphics library. You can follow our updates to facilitate your learning.
(The interface has not yet developed any functions and can only be viewed by sliding the interface.)
However, you can choose the theme color you want to use.
2 Select a match code for the screen¶
The code ( D: CrowPanel Advance HMI Course code ) under this path is the functional code we provide for all sizes in this lesson. Everyone can choose the appropriate code according to the size you are using.
Here, I will introduce a product with a size of 2.4 as an example
(2.4-inch, 2.8-inch, 3.5-inch using OnlineAudio (small))
(3.5-inch, 5.0-inch, 7.0-inch using OnlineAudio (Largel))
2.1 Using Arduino IDE to Open Code¶
2.2 Code Introduction¶
You can understand the overall code based on the comments after each line of code.
(If you want to have a deeper understanding of code construction, you can go watch a video explanation.)
3 Configure library file¶
3.1 Select the appropriate library file based on the product screen size¶
Path referenceļ¼C:\ESP32_Code\CrowPanel_Advanced_HMI\Arduino_lib Series Library
3.2 I will use the Advance 2.4-inch product as an example for operation¶
Copy the Libraries Advanced 2.4 folder
Open Arduino IDE runtime library file path
Reference path: C:\Users\14175\Documents\Arduino
Delete the existing libraries folder
Paste the copied library Advanced 2.4 folder into this path
Change the folder name to the original libraries
Library update completed, restart Arduino IDE.
When using other sizes, changing the library file is the same operation
4 How to play the song we want to play¶
Principle:
After the CrowPanel ESP32 Advance HMI is connected to WIFI, it will search for this song through the URL link provided in our code and achieve network playback (this is an external link for a song)
Note: The audio stream encoding formats supported by the parameters of the audio.connecttohost function, such as MP3, AAC, FLAC, etc
The reason why people do not use familiar music websites such as Spotify, Apple Music, and YouTube Music to play songs is because most legitimate music streaming services only provide song page links or embedded player code to play music. To protect copyright, not providing direct download links in MP3 format, distributing or obtaining MP3 download links directly may violate the terms of service and copyright laws of these platforms. So we didn't use it. We use Chinese music websites to play music here. If you want to use a music website that you are familiar with, just find a URL link in MP3 format similar to the one in the code and replace it.
First, connect our speakers
In NetEase Cloud Music or other music platforms, the ID of a song is usually a unique identifier for each song. So the main thing is to replace the ID number in the URL link in the code
Next, I will use NetEase Cloud Music as an example to introduce, searching for an ID number
Use a browser to search for NetEase Cloud Music and open it
Play the song you want to listen to
Retrieve the ID number from the link and replace it with the ID number in the code
This way, you can change the music you want to play (remember to re burn the program after changing the ID.
Before burning the code, remember to configure the environment in which the code will run.
For small sizes, there is no need to dial the code and it can be directly burned.
5 How to adjust the size of the sound output from the speaker¶
When an external speaker is connected, this is the power consumption that our built-in amplifier chip can provide
5.1 The volume can be adjusted by adjusting the knob¶
5.2 Adjustment principle¶
Open the schematic diagram we provide
By changing the resistance of the R51 adjustable resistor, the volume can be adjusted.
6 Code presentation¶
6.1 OnlineAudio_small¶
#include <Wire.h>
#include "Arduino.h"
#include "WiFiMulti.h"
#include "Audio.h"
// Built-in amplifier chip pins
#define I2S_DOUT 12
#define I2S_BCLK 13
#define I2S_LRC 11
Audio audio;
WiFiMulti wifiMulti;
String ssid = "yanfa1"; // WiFi name
String password = "1223334444yanfa"; // WiFi password
void setup() {
Serial.begin(115200); // Set baud rate
pinMode(21, OUTPUT); // Sound shutdown pin
pinMode(14, OUTPUT); // MUTE pin
digitalWrite(14, LOW);
digitalWrite(21, HIGH); // Set pins to enable music playback
delay(50);
Serial.printf("[LINE--%d]\n", __LINE__);
WiFi.mode(WIFI_STA); // Set the WiFi mode of the device to Station mode.
wifiMulti.addAP(ssid.c_str(), password.c_str()); // Add WiFi credentials
wifiMulti.run(); // Connect to WiFi
if (WiFi.status() != WL_CONNECTED) { // WiFi connection failed
WiFi.disconnect(true);
wifiMulti.run(); // Attempt to connect again
}
Serial.printf("[LINE--%d]\n", __LINE__);
Serial.println("----- WIFI_CONNECTED -----");
audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT); // Initialize audio
audio.setVolume(20); // Volume level: 0...21
digitalWrite(21, LOW); // Pull the mute control pin low to enable sound
// Choose the URL of the music you want to play
// audio.connecttohost("http://music.163.com/song/media/outer/url?id=2086327879.mp3"); // Flowers.mp3
// This is Taylor Swift singing 'Last Christmas'
audio.connecttohost("http://music.163.com/song/media/outer/url?id=1405259103.mp3"); // Last Christmas.mp3
// audio.connecttohost("http://music.163.com/song/media/outer/url?id=5103312.mp3"); // Empire state of mine.mp3
Serial.printf("[LINE--%d]\t ready to play!!\n", __LINE__);
}
void loop() {
audio.loop(); // Play each frame of the music
if (Serial.available()) { // Condition to stop music, triggered when serial data is received
audio.stopSong(); // Stop playback
String r = Serial.readString(); // Read music data from serial
r.trim(); // Ensure there are no extra spaces or line breaks in the received data,
// so that the subsequent check of r.length()>5 accurately reflects the length of valid characters.
if (r.length() > 5) audio.connecttohost(r.c_str()); // Try connecting to the next song URL
log_i("free heap=%i", ESP.getFreeHeap());
}
}