Implement first version of http_server

This commit is contained in:
2024-03-02 22:58:40 +01:00
parent 083ce1a150
commit 694d69252d
4 changed files with 199 additions and 36 deletions

View File

@@ -1,4 +1,4 @@
set(SOURCES src/main.cpp)
set(INCLUDES include)
idf_component_register(SRCS ${SOURCES} INCLUDE_DIRS ${INCLUDES} REQUIRES wifi nvs nanofmt)
idf_component_register(SRCS ${SOURCES} INCLUDE_DIRS ${INCLUDES} REQUIRES wifi nvs nanofmt http_server)

View File

@@ -1,45 +1,60 @@
#include "esp_log.h"
#include <http/server.h>
#include <nvs/nvs.h>
#include <wifi/wifi.h>
#include "esp_log.h"
void start_ap() {
nvs::init();
wifi::init();
wifi::set_mode(wifi::Mode::soft_ap);
wifi::configure_soft_ap(
"HyperLink", "1234567890",
{.connected =
[](auto mac, auto aid) {
ESP_LOGI("DEBUG - ap callback", "Connected to mac %s",
mac.data());
},
.disconnected =
[](auto mac, auto aid) {
ESP_LOGI("DEBUG - ap callback", "Disconnected from mac %s",
mac.data());
}});
wifi::start();
}
void start_http_server() {
http::start_server({.port = 80,
.on_error =
[]() {
// TODO
},
.on_not_found =
[]() {
// TODO
}});
http::set_handlers({{http::Method::get, "/",
[]() {
return http::response_t{
"<html><body style=\"background:black; color: "
"white\">Home</body></html>"};
}},
{http::Method::get, "/hello1",
[]() {
return http::response_t{"Hello 1"};
}},
{http::Method::get, "/hello2", []() {
return http::response_t{"Hello 2"};
}}});
}
extern "C" void app_main(void) {
try {
nvs::init();
wifi::init();
// wifi::set_mode(wifi::Mode::station);
// wifi::configure_station(
// "Vodafone-180C", "giraffen!",
// {.stationStarted = []() { wifi::station_connect(); },
// .disconnected = [] { wifi::station_connect(); },
// .gotIp =
// [](auto ip) {
// ESP_LOGI("DEBUG - ip callback ", "Got ip: %s",
// ip.data());
// }});
// wifi::start();
// wifi::stop();
// wifi::clear_callbacks();
// wifi::stop();
// wifi::deinit();
wifi::set_mode(wifi::Mode::soft_ap);
wifi::configure_soft_ap(
"HyperLink", "1234567890",
{.connected =
[](auto mac, auto aid) {
ESP_LOGI("DEBUG - ap callback", "Connected to mac %s",
mac.data());
},
.disconnected =
[](auto mac, auto aid) {
ESP_LOGI("DEBUG - ap callback", "Disconnected from mac %s",
mac.data());
}});
wifi::start();
start_ap();
start_http_server();
} catch (const std::exception& e) {
ESP_LOGE("main", "Exception: %s", e.what());
}