Open huster-songtao opened 6 months ago
#if defined(ESP8266)
#include <ESP8266mDNS.h>
#elif defined(ESP32)
#include <ESPmDNS.h>
#endif
#include <FS.h>
#include <LittleFS.h>
#include <AsyncFsWebServer.h> // https://github.com/cotestatnt/async-esp-fs-webserver/
#define FILESYSTEM LittleFS
AsyncFsWebServer server(80, FILESYSTEM);
// #ifndef LED_BUILTIN
// #define LED_BUILTIN 2
// #endif
// In order to set SSID and password open the /setup webserver page
// const char* ssid;
// const char* password;
const char* hostname = "heap-chart";
// Timezone definition to get properly time from NTP server
#define MYTZ "CET-1CEST,M3.5.0,M10.5.0/3"
struct tm Time;
//////////////////////////////// WebSocket Handler /////////////////////////////
void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){
switch (type) {
case WS_EVT_DISCONNECT:
Serial.print("WebSocket client disconnected!\n");
break;
case WS_EVT_CONNECT: {
IPAddress ip = client->remoteIP();
Serial.printf("WebSocket client %d.%d.%d.%d connected.\n", ip[0], ip[1], ip[2], ip[3]);
client->printf("%s", "{\"Connected\": true}");
}
break;
default:
break;
}
}
//////////////////////////////// NTP Time /////////////////////////////////////
void getUpdatedtime(const uint32_t timeout)
{
uint32_t start = millis();
Serial.print("Sync time...");
while (millis() - start < timeout && Time.tm_year <= (1970 - 1900)) {
time_t now = time(nullptr);
Time = *localtime(&now);
delay(5);
}
Serial.println(" done.");
}
//////////////////////////////// Filesystem /////////////////////////////////////////
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
Serial.printf("\nListing directory: %s\n", dirname);
File root = fs.open(dirname, "r");
if (!root) {
Serial.println("- failed to open directory");
return;
}
if (!root.isDirectory()) {
Serial.println(" - not a directory");
return;
}
File file = root.openNextFile();
while (file) {
if (file.isDirectory()) {
if (levels) {
#ifdef ESP32
listDir(fs, file.path(), levels - 1);
#elif defined(ESP8266)
listDir(fs, file.fullName(), levels - 1);
#endif
}
} else {
Serial.printf("|__ FILE: %s (%d bytes)\n",file.name(), file.size());
}
file = root.openNextFile();
}
}
bool startFilesystem() {
if (FILESYSTEM.begin()){
listDir(FILESYSTEM, "/", 1);
return true;
}
else {
Serial.println("ERROR on mounting filesystem. It will be reformatted!");
FILESYSTEM.format();
ESP.restart();
}
return false;
}
void setup() {
// pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
// FILESYSTEM INIT
startFilesystem();
// Try to connect to flash stored SSID, start AP if fails after timeout
IPAddress myIP = server.startWiFi(15000, "ESP_AP", "123456789" );
// Enable ACE FS file web editor and add FS info callback function
server.enableFsCodeEditor();
/*
* Getting FS info (total and free bytes) is strictly related to
* filesystem library used (LittleFS, FFat, SPIFFS etc etc) and ESP framework
*/
#ifdef ESP32
server.setFsInfoCallback([](fsInfo_t* fsInfo) {
fsInfo->fsName = "LittleFS";
fsInfo->totalBytes = LittleFS.totalBytes();
fsInfo->usedBytes = LittleFS.usedBytes();
});
#endif
// Start server with custom websocket event handler
server.init(onWsEvent);
Serial.print(F("ESP Web Server started on IP Address: "));
Serial.println(myIP);
Serial.println(F(
"This is \"highcharts.ino\" example.\n"
"Open /setup page to configure optional parameters.\n"
"Open /edit page to view, edit or upload example or your custom webserver source files."
));
// Start MDNS responder
if (WiFi.status() == WL_CONNECTED) {
// Set hostname
#ifdef ESP8266
WiFi.hostname(hostname);
#elif defined(ESP32)
WiFi.setHostname(hostname);
#endif
if (MDNS.begin(hostname)) {
Serial.println(F("MDNS responder started."));
Serial.printf("You should be able to connect with address\t http://%s.local/\n", hostname);
// Add service to MDNS-SD
MDNS.addService("http", "tcp", 80);
}
}
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
#ifdef ESP8266
MDNS.update();
#endif
}
// Send ESP system time (epoch) and heap stats to WS client
static uint32_t sendToClientTime;
if (millis() - sendToClientTime > 1000 ) {
sendToClientTime = millis();
// digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); -- 通过API控制
time_t now = time(nullptr);
StaticJsonDocument<1024> doc;
doc["addPoint"] = true;
doc["timestamp"] = now;
#ifdef ESP32
doc["totalHeap"] = heap_caps_get_free_size(0);
doc["maxBlock"] = heap_caps_get_largest_free_block(0);
#elif defined(ESP8266)
uint32_t free;
uint32_t max;
ESP.getHeapStats(&free, &max, nullptr);
doc["totalHeap"] = free;
doc["maxBlock"] = max;
#endif
String msg;
serializeJson(doc, msg);
server.wsBroadcast(msg.c_str());
}
}
Hi,
I don't know if this can be linked to your pb but I notice you mention :
Listing directory: /config
|__ FILE: config.json (0 bytes)
And I have lot of problem when my config.json file is corrupted to 0 byte. In my case the config.json file is corrupted quite often when I flash my device... I don't know why... (See my #41 issue)
Arduino IDE 2.3.2 esp32 2.0.14 by Espressif Systems
Listing directory: /
Listing directory: /config |__ FILE: conf Listing directory: /
Listing directory: /config | FILE: config.json (0 bytes) | FILE: img-logo-128_128.txt (3098 bytes) | FILE: raw-css-fetch.css (290 bytes) | FILE: raw-html-fetch-test.htm (462 bytes) | FILE: raw-html-ts.htm (1089 bytes) | F [I][AsyncFsWebServer.cpp:152] startCaptivePortal(): Captive portal started. Redirecting all requests to /setup abort() was called at PC 0x4208a5b5 on core 0 Core 0 register dump: MEPC : 0x40381dc6 RA : 0x40386e34 SP : 0x3fc9e030 GP : 0x3fc8e400
TP : 0x3fc86030 T0 : 0x37363534 T1 : 0x7271706f T2 : 0x33323130
S0/FP : 0x3fc9e05c S1 : 0x3fc9e05c A0 : 0x3fc9e068 A1 : 0x3fc9e04a
A2 : 0x00000000 A3 : 0x3fc9e095 A4 : 0x00000001 A5 : 0x3fc97000
A6 : 0x7a797877 A7 : 0x76757473 S2 : 0x00000000 S3 : 0x00000000
S4 : 0x00000000 S5 : 0x00000000 S6 : 0x00000000 S7 : 0x00000000
S8 : 0x00000000 S9 : 0x00000000 S10 : 0x00000000 S11 : 0x00000000
T3 : 0x6e6d6c6b T4 : 0x6a696867 T5 : 0x66656463 T6 : 0x62613938
MSTATUS : 0x00001801 MTVEC : 0x40380001 MCAUSE : 0x00000007 MTVAL : 0x00000000
MHARTID : 0x00000000
Stack memory: 3fc9e030: 0x00000000 0x00000000 0x3fc9e048 0x4038c454 0x00000000 0x00000000 0x00000030 0x3fc909a4 3fc9e050: 0x3fc9e05c 0x3fc909c0 0x3fc9e048 0x38303234 0x35623561 0x00000000 0x726f6261 0x20292874 3fc9e070: 0x20736177 0x6c6c6163 0x61206465 0x43502074 0x34783020 0x61383032 0x20356235 0x63206e6f 3fc9e090: 0x2065726f 0x00000030 0x00000000 0xd223c2d5 0x00000000 0x3fc91500 0x3fc9e42c 0x4208a5b8 3fc9e0b0: 0x3fc968d4 0x3fca9b68 0x00000100 0x00000000 0x3c0a2000 0x3fc9e11c 0x00000000 0x3fc9e3fc 3fc9e0d0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x3fc9e3f8 0x3fc9e3f4 3fc9e0f0: 0x3fc9e3f0 0x3fc9e3ec 0x3fc9e3e8 0x3fc9e3e4 0x00000000 0x00000000 0x00000000 0x00000000 3fc9e110: 0x3fc9e3e0 0x3fc9e3dc 0x3fc9e3d8 0x3fc9e3d4 0x3fc9e3d0 0x3fc9e3cc 0x3fc9e3c8 0x3fc9e3c4 3fc9e130: 0x3fc9e3c0 0x3fc9e3bc 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 3fc9e150: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 3fc9e170: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 3fc9e190: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 3fc9e1b0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 3fc9e1d0: 0x00000000 0x3fc9e400 0x420011cc 0x00000000 0x00000000 0x00000000 0x4208a53c 0x40000000 3fc9e1f0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 3fc9e210: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 3fc9e230: 0x00000000 0x00000000 0x00000000 0x00000000 0x3fc9e45c 0x00000000 0x00000000 0x00000000 3fc9e250: 0x00000000 0x00000000 0x00000000 0x3fc9e458 0x3fc9e454 0x3fc9e3f0 0x3fc9e3ec 0x3fc9e3e8 3fc9e270: 0x3fc9e3e4 0x00000000 0x00000000 0x00000000 0x00000000 0x3fc9e3e0 0x3fc9e3dc 0x3fc9e3d8 3fc9e290: 0x3fc9e3d4 0x3fc9e3d0 0x3fc9e3cc 0x3fc9e3c8 0x3fc9e3c4 0x3fc9e3c0 0x3fc9e3bc 0x00000000 3fc9e2b0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 3fc9e2d0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 3fc9e2f0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 3fc9e310: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 3fc9e330: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x3fc9e460 0x40389a9c 3fc9e350: 0x00000000 0x00000000 0x00000000 0x42012980 0x40000000 0x00000000 0x00000000 0x00000000 3fc9e370: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 3fc9e390: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 3fc9e3b0: 0x00000000 0x00000001 0x3c0a1600 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 3fc9e3d0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000800 0x3fca9b60 0x3fc9e294 3fc9e3f0: 0x3fc9e42c 0x3fc91500 0x3fc9e42c 0x420011cc 0x00000000 0x00000000 0x00000000 0x3c0a2f90 3fc9e410: 0x08080808 0x00000000 0x08080808 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
ELF file SHA256: 23a0bcae19c516f6
Rebooting... ESP-ROM:esp32c3-api1-20210207 Build:Feb 7 2021 rst:0x3 (RTC_SW_SYS_RST),boot:0x8 (SPI_FAST_FLASH_BOOT) Saved PC:0x4038199e SPIWP:0xee mode:DIO, clock div:1 load:0x3fcd5810,len:0x438 load:0x403cc710,len:0x90c load:0x403ce710,len:0x25f4 entry 0x403cc710