2020-12-14 20:58:27 +08:00
|
|
|
// Copyright (c) 2020 Cesanta Software Limited
|
|
|
|
// All rights reserved
|
|
|
|
|
2021-02-03 10:13:05 +08:00
|
|
|
#include "esp_spiffs.h"
|
|
|
|
#include "freertos/FreeRTOS.h"
|
2020-12-14 20:58:27 +08:00
|
|
|
#include "mongoose.h"
|
|
|
|
|
2020-12-27 09:29:42 +08:00
|
|
|
#define WIFI_SSID "WIFI_NETWORK" // SET THIS!
|
|
|
|
#define WIFI_PASS "WIFI_PASSWORD" // SET THIS!
|
2021-02-03 10:13:05 +08:00
|
|
|
#define FS_ROOT "/spiffs"
|
2020-12-14 20:58:27 +08:00
|
|
|
|
2021-10-23 05:06:10 +08:00
|
|
|
// SPIFFS is flat, so tell Mongoose that the FS root is a directory
|
|
|
|
// This cludge is not required for filesystems with directory support
|
|
|
|
static int my_stat(const char *path, size_t *size, time_t *mtime) {
|
2022-02-08 21:43:34 +08:00
|
|
|
int flags = mg_fs_posix.st(path, size, mtime);
|
2021-10-23 05:06:10 +08:00
|
|
|
if (strcmp(path, FS_ROOT) == 0) flags |= MG_FS_DIR;
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2021-02-03 10:13:05 +08:00
|
|
|
// Event handler for an server (accepted) connection. Implemented endpoints:
|
|
|
|
// /api/stats - return JSON object with ESP32 stats (free RAM)
|
|
|
|
// any other - serve files from the filesystem
|
2020-12-14 20:58:27 +08:00
|
|
|
static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
|
|
|
if (ev == MG_EV_HTTP_MSG) {
|
2021-02-03 10:13:05 +08:00
|
|
|
struct mg_http_message *hm = ev_data;
|
|
|
|
if (mg_http_match_uri(hm, "/api/stats")) {
|
|
|
|
mg_http_reply(c, 200, "", "{\"ram\": %lu}\n", xPortGetFreeHeapSize());
|
|
|
|
} else {
|
2021-10-23 05:06:10 +08:00
|
|
|
struct mg_fs fs = mg_fs_posix;
|
2022-02-08 22:13:47 +08:00
|
|
|
fs.st = my_stat;
|
2021-10-23 05:06:10 +08:00
|
|
|
struct mg_http_serve_opts opts = {.root_dir = FS_ROOT, .fs = &fs};
|
2021-02-03 10:13:05 +08:00
|
|
|
mg_http_serve_dir(c, hm, &opts);
|
|
|
|
}
|
2020-12-25 00:52:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 20:58:27 +08:00
|
|
|
void app_main(void) {
|
2021-02-03 10:13:05 +08:00
|
|
|
// Mount filesystem
|
|
|
|
esp_vfs_spiffs_conf_t conf = {
|
|
|
|
.base_path = FS_ROOT, .max_files = 20, .format_if_mount_failed = true};
|
|
|
|
int res = esp_vfs_spiffs_register(&conf);
|
2022-02-13 02:17:25 +08:00
|
|
|
MG_INFO(("FS %s, %d", conf.base_path, res));
|
2022-01-19 01:11:02 +08:00
|
|
|
mg_file_printf(&mg_fs_posix, FS_ROOT "/hello.txt", "%s", "hello from ESP");
|
2021-02-03 10:13:05 +08:00
|
|
|
|
2020-12-14 20:58:27 +08:00
|
|
|
// Setup wifi. This function is implemented in wifi.c
|
|
|
|
// It blocks until connected to the configured WiFi network
|
|
|
|
void wifi_init(const char *ssid, const char *pass);
|
|
|
|
wifi_init(WIFI_SSID, WIFI_PASS);
|
|
|
|
|
2021-02-03 10:13:05 +08:00
|
|
|
// Connected to WiFi, now start HTTP server
|
|
|
|
struct mg_mgr mgr;
|
|
|
|
mg_log_set("3");
|
|
|
|
mg_mgr_init(&mgr);
|
|
|
|
mg_http_listen(&mgr, "http://0.0.0.0:80", cb, &mgr); // Listening server
|
2022-02-13 02:17:25 +08:00
|
|
|
MG_INFO(("Starting Mongoose web server v%s", MG_VERSION));
|
2021-02-03 10:13:05 +08:00
|
|
|
for (;;) mg_mgr_poll(&mgr, 1000); // Infinite event loop
|
2020-12-14 20:58:27 +08:00
|
|
|
}
|