Skip to content

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.)

1

2

3

However, you can choose the theme color you want to use.

4

2 Download Code


Click the link below to download the code you need.

Code link:

CrowPanel-Advance-7-HMI-ESP32-S3-AI-Powered-IPS-Touch-Screen-800x480/example at master · Elecrow-RD/CrowPanel-Advance-7-HMI-ESP32-S3-AI-Powered-IPS-Touch-Screen-800x480

image-20260601145039493

2.1 Using Arduino IDE to Open Code

8

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.)

9

3 Configure library file


3.1 Select the appropriate library file based on the product screen size

Click the link below to download the relevant library files.

Library file link:

https://drive.google.com/drive/folders/1npWkVzctd7e0Y6H5hahk8uxGhy9idxCz

10

3.2 I will use the Advance 7.0-inch product as an example for operation

Copy the Libraries Advanced 7.0 folder

Open Arduino IDE runtime library file path

Reference path: C:\Users\14175\Documents\Arduino

11

Delete the existing libraries folder

12

Paste the copied library Advanced 7.0 folder into this path

13

Change the folder name to the original libraries

14

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)

15

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

speaker

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

17

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

18

Play the song you want to listen to

19

Retrieve the ID number from the link and replace it with the ID number in the code

20

This way, you can change the music you want to play. (Remember to re burn the program after changing the ID)
Tips:
1. If the program runs without errors but the song doesn't play, you should check the URL to ensure that the provided link is correct.
2. If the song you want to play is a VIP song, it cannot be played.
3. Before burning the code, remember to configure the environment in which the code will run.

5 Code presentation


5.1 OnlineAudio_large

#include <Wire.h>
#include "Arduino.h"
#include "WiFiMulti.h"
#include "Audio.h"
// #include "TCA9534.h"

#define I2S_DOUT 4
#define I2S_BCLK 5
#define I2S_LRC 6

Audio audio;
WiFiMulti wifiMulti;

String ssid = "yanfa1";
String password = "1223334444yanfa";

bool i2cScanForAddress(uint8_t address) {
  Wire.beginTransmission(address);
  return (Wire.endTransmission() == 0);
}

// Wrapper function for sending I2C commands
void sendI2CCommand(uint8_t command) {
  uint8_t error;
  // Start sending commands to the specified address
  Wire.beginTransmission(0x30);
  // Send command
  Wire.write(command);
  //  End transmission and return status
  error = Wire.endTransmission();

  if (error == 0) {
    Serial.print("command 0x");
    Serial.print(command, HEX);
    Serial.println(" Sent successfully");
  } else {
    Serial.print("Command sent error, error code:");
    Serial.println(error);
  }
}

void setup() {
  Serial.begin(115200);

  Wire.begin(15, 16);
  delay(50);
  while (1) {
    if (i2cScanForAddress(0x30) && i2cScanForAddress(0x5D)) {
      Serial.print("The microcontroller is detected: address 0x");
      Serial.println(0x30, HEX);
      Serial.print("The microcontroller is detected: address 0x");
      Serial.println(0x5D, HEX);


      break;
    } else {
      Serial.print("No microcontroller was detected: address 0x");
      Serial.println(0x30, HEX);
      Serial.print("No microcontroller was detected: address 0x");
      Serial.println(0x5D, HEX);
      //Prevent the microcontroller did not start to adjust the bright screen
      sendI2CCommand(0x19);
      pinMode(1, OUTPUT);
      digitalWrite(1, LOW);

      delay(120);
      pinMode(1, INPUT);

      delay(100);
    }
  }
  sendI2CCommand(0x17);
  Serial.printf("[LINE--%d]\n", __LINE__);
  WiFi.mode(WIFI_STA);
  wifiMulti.addAP(ssid.c_str(), password.c_str());
  wifiMulti.run();
  if (WiFi.status() != WL_CONNECTED) {
    WiFi.disconnect(true);
    wifiMulti.run();
  }
  Serial.printf("[LINE--%d]\n", __LINE__);
  Serial.println("--- WIFI_CONNECTED ---");
  audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
  audio.setVolume(20);  // 0...21

  // Choose the URL of the music you want to play

  // This is Taylor Swift singing 'Last Christmas'

  audio.connecttohost("http://music.163.com/song/media/outer/url?id=1391891631.mp3"); // Empire state of mine.mp3
  Serial.printf("[LINE--%d]\t ready to play!!\n", __LINE__);
}

void loop() {
  audio.loop();
  if (Serial.available()) {  // put streamURL in serial monitor
    audio.stopSong();
    String r = Serial.readString();
    r.trim();
    if (r.length() > 5) audio.connecttohost(r.c_str());
    log_i("free heap=%i", ESP.getFreeHeap());
  }
}

If your code compiles incorrectly, you can check if the ESP32 version number is correct. The ESP32 version number we need for this lesson is 3.0.2.

new-1

Secondly, please pay attention to replacing the corresponding size library file.

Select the appropriate library file based on the product screen size

You can refer to the third point mentioned above, "Configure library file".

Kind Reminder:

You are currently viewing the 7-inch product of CrowPanel Advance HMI, and the version here is V1.2.

(The following precautions are also applicable to the 5.0-inch V1.1 version.)

In terms of hardware, we use a microcontroller (STC8H1K28) to control the screen backlight and the mute function of the speaker.

advance-1.2-new

(However, there are other function interfaces that need to be written in the specific code, and you can refer to the complete code provided later.)

Explanation:

  • 0x30 is the I2C address of the microcontroller (STC8H1K28).

  • 0x5D is the I2C address of the touch IC (GT911).

  • sendI2CCommand(0x10) means sending the 0x10 command to the microcontroller (0x30) to instruct the microcontroller to set the screen brightness to maximum.

(For 0x10 mentioned above, you can replace it with the following values:

  • 0x05: Turn off the screen

  • 0x06 to 0x10: The screen brightness will gradually increase to the maximum)

**To set a different brightness level, you must first send 0x10 to turn on the screen, and then send another value to adjust the brightness. **

Additional notes:

For the function interface sendI2CCommand(0x10), if 0x10 is replaced with the following values, the controlled functions will be different:

  • sendI2CCommand(0x15): It means sending the 0x15 command to the microcontroller (0x30) to instruct the activation of the buzzer.
  • sendI2CCommand(0x16): It means sending the 0x16 command to the microcontroller (0x30) to instruct the buzzer to be turned off.

  • sendI2CCommand(0x17): It means sending the 0x17 command to the microcontroller (0x30) to instruct the speaker to turn on.

  • sendI2CCommand(0x18): It means sending the 0x18 command to the microcontroller (0x30) to instruct the speaker to turn off.

Be careful:

For large-sized products, 4.3-inch, 5.0-inch, 7.0-inch.

Before burning the code, be sure to set the switch on the back of the product to MIC&SPK mode.

20-1