2022-06-19 18:27:37 +01:00
|
|
|
// Copyright (c) 2020 Cesanta Software Limited
|
|
|
|
// All rights reserved
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
const char *s_listening_url = "http://0.0.0.0:80";
|
|
|
|
|
2024-04-03 15:48:57 -03:00
|
|
|
struct mg_str config_read(void) {
|
|
|
|
return mg_file_read(&mg_fs_posix, FS_ROOT "/config.json");
|
2022-06-19 18:27:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void config_write(struct mg_str config) {
|
2024-03-15 07:42:24 +00:00
|
|
|
mg_file_write(&mg_fs_posix, FS_ROOT "/config.json", config.buf, config.len);
|
2022-06-19 18:27:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void app_main(void) {
|
|
|
|
// 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);
|
|
|
|
MG_INFO(("FS at %s initialised, status: %d", conf.base_path, res));
|
|
|
|
|
|
|
|
// Try to connect to wifi by using saved WiFi credentials
|
2024-02-16 10:44:56 +00:00
|
|
|
struct mg_str json = mg_file_read(&mg_fs_posix, WIFI_FILE);
|
2024-03-15 07:42:24 +00:00
|
|
|
if (json.buf != NULL) {
|
2024-02-16 10:44:56 +00:00
|
|
|
char *ssid = mg_json_get_str(json, "$.ssid");
|
|
|
|
char *pass = mg_json_get_str(json, "$.pass");
|
2022-06-19 18:27:37 +01:00
|
|
|
while (!wifi_init(ssid, pass)) (void) 0;
|
|
|
|
free(ssid);
|
|
|
|
free(pass);
|
2024-03-15 07:42:24 +00:00
|
|
|
free((void *) json.buf);
|
2022-06-19 18:27:37 +01:00
|
|
|
} else {
|
|
|
|
// If WiFi is not configured, run CLI until configured
|
|
|
|
MG_INFO(("WiFi is not configured, running CLI. Press enter"));
|
2022-08-02 14:39:36 -03:00
|
|
|
cli_init();
|
2022-06-19 18:27:37 +01:00
|
|
|
for (;;) {
|
|
|
|
uint8_t ch = getchar();
|
|
|
|
cli(ch);
|
|
|
|
usleep(10000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connected to WiFi, now start HTTP server
|
|
|
|
struct mg_mgr mgr;
|
|
|
|
mg_mgr_init(&mgr);
|
2022-08-01 11:19:32 +01:00
|
|
|
mg_log_set(MG_LL_DEBUG); // Set log level
|
2022-06-19 18:27:37 +01:00
|
|
|
MG_INFO(("Mongoose v%s on %s", MG_VERSION, s_listening_url));
|
2024-02-16 10:44:56 +00:00
|
|
|
mg_http_listen(&mgr, s_listening_url, uart_bridge_fn, NULL);
|
2022-06-19 18:27:37 +01:00
|
|
|
for (;;) mg_mgr_poll(&mgr, 10); // Infinite event loop
|
|
|
|
}
|