mongoose/examples/esp32/uart-bridge/main/main.c

51 lines
1.5 KiB
C
Raw Normal View History

2022-06-20 01:27:37 +08:00
// Copyright (c) 2020 Cesanta Software Limited
// All rights reserved
#include "main.h"
const char *s_listening_url = "http://0.0.0.0:80";
char *config_read(void) {
return mg_file_read(&mg_fs_posix, FS_ROOT "/config.json", NULL);
}
void config_write(struct mg_str config) {
mg_file_write(&mg_fs_posix, FS_ROOT "/config.json", config.ptr, config.len);
}
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
char *json = mg_file_read(&mg_fs_posix, WIFI_FILE, NULL);
if (json != NULL) {
char *ssid = mg_json_get_str(mg_str(json), "$.ssid");
char *pass = mg_json_get_str(mg_str(json), "$.pass");
while (!wifi_init(ssid, pass)) (void) 0;
free(ssid);
free(pass);
free(json);
} else {
// If WiFi is not configured, run CLI until configured
MG_INFO(("WiFi is not configured, running CLI. Press enter"));
2022-08-03 01:39:36 +08:00
cli_init();
2022-06-20 01:27:37 +08: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 18:19:32 +08:00
mg_log_set(MG_LL_DEBUG); // Set log level
2022-06-20 01:27:37 +08:00
MG_INFO(("Mongoose v%s on %s", MG_VERSION, s_listening_url));
mg_http_listen(&mgr, s_listening_url, uart_bridge_fn, &mgr);
for (;;) mg_mgr_poll(&mgr, 10); // Infinite event loop
}