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"
|
|
|
|
|
2022-04-05 19:54:20 +08:00
|
|
|
#define WIFI_SSID "VMDF554B9" // SET THIS!
|
|
|
|
#define WIFI_PASS "Mp7wjmamPafa" // SET THIS!
|
2021-02-03 10:13:05 +08:00
|
|
|
#define FS_ROOT "/spiffs"
|
2020-12-14 20:58:27 +08:00
|
|
|
|
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 {
|
2022-04-05 19:54:20 +08:00
|
|
|
struct mg_http_serve_opts opts = {.root_dir = FS_ROOT};
|
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;
|
2022-04-05 19:54:20 +08:00
|
|
|
mg_log_set("4");
|
2021-02-03 10:13:05 +08:00
|
|
|
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
|
|
|
}
|