Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

M5Stack Core3 SE not working.... #865

Open
lemonhall opened this issue Oct 10, 2024 · 1 comment
Open

M5Stack Core3 SE not working.... #865

lemonhall opened this issue Oct 10, 2024 · 1 comment

Comments

@lemonhall
Copy link

2 days work, but still not working ....
and forgive my english...

`/**

#include "Audio.h"
#include <SPI.h>
#include <SD.h>
#include <M5CoreS3.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>

#define I2S_DOUT 13
#define I2S_BCLK 34 // I2S
#define I2S_LRC 33

Audio audio(0,1,I2S_NUM_1);

// 标志位,初始状态为清醒
bool isAwake = true;
// 记录上次收到 http command的时间
unsigned long lastHttpCommandTime = 0;

const char* ssid = "xxxxxx";
const char* password = "xxxxxxx";

WebServer server(80);

//不要定义为13,这会影响audio那边的
const int led = 5;

//根目录的处理器
void handleRoot() {
// 收到Http的命令时更新时间
lastHttpCommandTime = millis();
digitalWrite(led, 1);
server.send(200, "text/plain", "Hello from vermelia!");
digitalWrite(led, 0);
}

//未找到的处理器
void handleNotFound() {
// 收到Http的命令时更新时间
lastHttpCommandTime = millis();
digitalWrite(led, 1);
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
digitalWrite(led, 0);
}

#define SD_SPI_SCK_PIN 36
#define SD_SPI_MISO_PIN 35
#define SD_SPI_MOSI_PIN 37
#define SD_SPI_CS_PIN 4

void listDir(fs::FS &fs, const char *dirname, uint8_t levels);

M5Canvas canvas(&CoreS3.Display);
M5GFX display = CoreS3.Display;

void printf_log(const char *format, ...);
void println_log(const char *str);
void setupWifi();
void setupSD();
void setupHTTPServer();
void wakeup();

void setupWifi() {
delay(10);
//初始化wifi部分
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");

// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
  delay(500);
  Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

if (MDNS.begin("vermelia")) {
  Serial.println("MDNS responder started with [vermelia]");
}
//初始化wifi部分

}

void setupSD() {
//初始化屏幕
canvas.setColorDepth(1); // mono color
canvas.createSprite(CoreS3.Display.width(), CoreS3.Display.height());
canvas.setPaletteColor(1, GREEN);
canvas.setTextSize((float)canvas.width() / 160);
canvas.setTextScroll(true);

//初始化SD卡
// SD Card Initialization
SPI.begin(SD_SPI_SCK_PIN, SD_SPI_MISO_PIN, SD_SPI_MOSI_PIN, SD_SPI_CS_PIN);

if (!SD.begin(SD_SPI_CS_PIN, SPI, 25000000)) {
    // Print a message if the SD card initialization
    // fails orif the SD card does not exist.
    // 如果SD卡初始化失败或者SD卡不存在,则打印消息.
    println_log("Card failed, or not present");
    while (1)
        ;
}

uint8_t cardType = SD.cardType();

if (cardType == CARD_NONE) {
    println_log("No SD card attached");
    return;
}

println_log("SD Card Type: ");
if (cardType == CARD_MMC) {
    println_log("MMC");
} else if (cardType == CARD_SD) {
    println_log("SDSC");
} else if (cardType == CARD_SDHC) {
    println_log("SDHC");
} else {
    println_log("UNKNOWN");
}

uint64_t cardSize = SD.cardSize() / (1024 * 1024);
printf_log("SD Card Size: %lluMB\n", cardSize);

//读取根目录并列出
listDir(SD, "/", 0);
printf_log("Total space: %lluMB\n", SD.totalBytes() / (1024 * 1024));
printf_log("Used space: %lluMB\n", SD.usedBytes() / (1024 * 1024));

}

//初始化HTTP服务器
void setupHTTPServer() {
//初始化服务器部分
server.on("/", handleRoot);

server.on("/inline", []() {
  // 收到Http的命令时更新时间
  lastHttpCommandTime = millis();
  server.send(200, "text/plain", "this works as well");
});

server.onNotFound(handleNotFound);

server.begin();
Serial.println("HTTP server started");
//初始化服务器部分结束

}

//收到的任何的http请求都会更新lastHttpCommandTime的值
//使得!isAwake && now - lastHttpCommandTime < 60000
//成立,继而loop当中会触发唤醒例程
void wakeup(){
//显示图片
display.wakeup();
isAwake = true;
}

void setup() {
CoreS3.begin();
//启动LED灯,定义阵脚为13,要看看对不对的
pinMode(led, OUTPUT);
digitalWrite(led, 0);
//灯显示结束
//程序开始部分
Serial.begin(115200);
println_log("\nHello From SD card demo program");
display.begin();

//初始化wifi
setupWifi();

//初始化服务器
setupHTTPServer();
//初始化SD卡
setupSD();
//显示图片
display.drawPngFile(SD, "/Base.png", 0, 0);

audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
audio.setVolume(21); // 0...21
audio.connecttoFS(SD,"/Neon_Nights_16bits.mp3");

}

void loop() {
//web服务器的死循环部分
server.handleClient();
delay(20);//allow the cpu to switch to other tasks

unsigned long now = millis(); // Obtain the host startup duration. 获取主机开机时长

// 如果 1 分钟内没有收到 HTTP command 消息,则休眠
if (now - lastHttpCommandTime > 60000 && isAwake ) {
// 在此处添加休眠的相关代码
Serial.println("Going to sleep due to no HTTP messages for 1 minutes.");
// 在此处调用相关的关机函数或执行关机操作
display.sleep();
// 进入睡眠状态后设置标志位为睡眠
isAwake = false;
}else if (!isAwake && now - lastHttpCommandTime < 60000) {
wakeup();
}

audio.loop();

}

// optional
void audio_info(const char *info){
Serial.print("info "); Serial.println(info);
}
void audio_id3data(const char *info){ //id3 metadata
Serial.print("id3data ");Serial.println(info);
}
void audio_eof_mp3(const char *info){ //end of file
Serial.print("eof_mp3 ");Serial.println(info);
}
void audio_showstation(const char *info){
Serial.print("station ");Serial.println(info);
}
void audio_showstreamtitle(const char *info){
Serial.print("streamtitle ");Serial.println(info);
}
void audio_bitrate(const char *info){
Serial.print("bitrate ");Serial.println(info);
}
void audio_commercial(const char *info){ //duration in sec
Serial.print("commercial ");Serial.println(info);
}
void audio_icyurl(const char *info){ //homepage
Serial.print("icyurl ");Serial.println(info);
}
void audio_lasthost(const char *info){ //stream URL played
Serial.print("lasthost ");Serial.println(info);
}
void audio_eof_speech(const char *info){
Serial.print("eof_speech ");Serial.println(info);
}

void listDir(fs::FS &fs, const char *dirname, uint8_t levels) {
printf_log("Listing directory: %s\n", dirname);

File root = fs.open(dirname);
if (!root) {
    println_log("Failed to open directory");
    return;
}
if (!root.isDirectory()) {
    println_log("Not a directory");
    return;
}

File file = root.openNextFile();
while (file) {
    if (file.isDirectory()) {
        Serial.print("  DIR : ");
        println_log(file.name());
        if (levels) {
            listDir(fs, file.path(), levels - 1);
        }
    } else {
        Serial.print("  FILE: ");
        Serial.print(file.name());
        Serial.print("  SIZE: ");
        println_log(String(file.size()).c_str());
    }
    file = root.openNextFile();
}

}

void printf_log(const char *format, ...) {
char buf[256];
va_list args;
va_start(args, format);
vsnprintf(buf, 256, format, args);
va_end(args);
Serial.print(buf);
canvas.printf(buf);
canvas.pushSprite(0, 0);
}

void println_log(const char *str) {
Serial.println(str);
canvas.println(str);
canvas.pushSprite(0, 0);
}
`

LOG

23:57:13.285 -> Hello From SD card demo program 23:57:13.525 -> 23:57:14.029 -> . 23:57:14.029 -> Connected to xxxxxx 23:57:14.029 -> IP address: xxxxxxx 23:57:14.029 -> MDNS responder started with [vermelia] 23:57:14.029 -> HTTP server started 23:57:14.067 -> SD Card Type: 23:57:14.107 -> SDHC 23:57:14.148 -> SD Card Size: 15193MB 23:57:14.188 -> Listing directory: / 23:57:14.221 -> FILE: autorun.inf SIZE: 200

@lemonhall
Copy link
Author

23:57:14.724 -> Used space: 4MB
23:57:15.512 -> info PSRAM found, inputBufferSize: 638965 bytes
23:57:15.555 -> info buffers freed, free Heap: 224704 bytes
23:57:15.555 -> info Reading file: "/Neon_Nights_16bits.mp3"
23:57:15.555 -> info MP3Decoder has been initialized, free Heap: 220336 bytes , free stack 5172 DWORDs
23:57:16.029 -> info Content-Length: 666115
23:57:16.029 -> info ID3 framesSize: 115
23:57:16.029 -> info ID3 version: 2.3
23:57:16.071 -> info ID3 normal frames
23:57:16.147 -> id3data Artist: LemonHall
23:57:16.227 -> id3data Title: Neon Nights
23:57:16.309 -> id3data Year: 2024
23:57:16.542 -> info Audio-Length: 666000
23:57:16.583 -> info stream ready
23:57:16.583 -> info syncword found at pos 0
23:57:16.583 -> info MPEG-1, Layer I
23:57:16.583 -> info Channels: 1
23:57:16.583 -> info SampleRate: 16000
23:57:16.583 -> info BitsPerSample: 16
23:57:16.583 -> info BitRate: 64000
23:58:11.932 -> Going to sleep due to no HTTP messages for 1 minutes.
23:59:33.514 -> info Closing audio file "Neon_Nights_16bits.mp3"
23:59:33.514 -> eof_mp3 Neon_Nights_16bits.mp3
23:59:33.514 -> info End of file "Neon_Nights_16bits.mp3"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant