Add HTTP server

This commit is contained in:
arne 2025-10-05 10:27:36 +02:00
commit 8bd9a9b1d0
7 changed files with 206 additions and 8 deletions

View file

@ -15,6 +15,7 @@
#include <dirent.h>
#include "paper.h"
#include "server.h"
#define WIFI_SCAN_LIST_SIZE 10
#define LUA_FILE_PATH "/assets"
@ -96,7 +97,7 @@ void run_lua_file(const char *file_name, const char *test_name) {
}
// Function to run an embedded Lua script
void run_embedded_lua_test(const char *lua_script, const char *test_name) {
void run_lua_string(const char *lua_script, const char *test_name) {
ESP_LOGI(TAG, "Starting Lua test: %s", test_name);
log_memory_usage("Start of test");
@ -176,19 +177,96 @@ void scan_wifi_networks(void) {
ESP_LOGI(TAG, "Wi-Fi scan completed.");
}
void app_main(void) {
// HTTP Server
#define WRITE_HEADER(req, buffer, name, format, src) \
sprintf(buffer, format, src); \
ESP_ERROR_CHECK(httpd_resp_set_hdr(req, name, buffer));
static esp_err_t http_index(httpd_req_t* req) {
// TODO: Serve HTML file with form POSTing to `/draw`
const char* response = "Hello world!\n";
httpd_resp_set_type(req, "text/plain");
httpd_resp_set_status(req, "200");
httpd_resp_send(req, response, HTTPD_RESP_USE_STRLEN);
return ESP_OK;
}
esp_err_t http_draw(httpd_req_t* req) {
// READING STREAM
int req_size = req->content_len;
char* content = (char*)heap_caps_malloc(req_size, MALLOC_CAP_SPIRAM);
if (content == NULL) {
char msg[50];
sprintf(msg, "Failed to allocate %d chars\n", req_size);
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, msg);
return ESP_ERR_INVALID_ARG;
}
int current_pos = 0;
int amount_recieved;
while ((amount_recieved = httpd_req_recv(req, (content + current_pos), req_size)) > 0) {
ESP_LOGI(__FUNCTION__, "Read %d bytes\n", amount_recieved);
current_pos += amount_recieved;
}
if (amount_recieved < 0) {
char msg[50];
heap_caps_free(content);
ESP_LOGE(msg, "Failed to read bytes. Error code %d\n", amount_recieved);
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, msg);
return ESP_ERR_INVALID_ARG;
}
ESP_LOGI(__FUNCTION__, "Done reading %d bytes out of %d\n", current_pos, req_size);
// TODO: Error handling
run_lua_string(content, "E-Paper Script via HTTP");
heap_caps_free(content);
// Done reading
char response[100];
sprintf(
response, "script drawn!"
);
httpd_resp_set_type(req, "text/plain");
httpd_resp_set_status(req, "200");
httpd_resp_send(req, response, HTTPD_RESP_USE_STRLEN);
return ESP_OK;
}
void register_http_routes(httpd_handle_t server) {
{
httpd_uri_t uri
= { .uri = "/", .method = HTTP_GET, .handler = http_index, .user_ctx = NULL };
httpd_register_uri_handler(server, &uri);
}
{
httpd_uri_t uri
= { .uri = "/draw", .method = HTTP_POST, .handler = http_draw, .user_ctx = NULL };
httpd_register_uri_handler(server, &uri);
}
}
// init
void app_main(void) {
// Initialize and mount the filesystem
init_filesystem();
// Initialize NVS, needed for wifi
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
// Set up HTTP server
httpd_handle_t server = get_server();
if (server != NULL) {
register_http_routes(server);
}
// Run script in assets/epaper.lua
run_lua_file("epaper.lua", "E-Paper Script");
run_lua_file("epaper.lua", "E-Paper Startup Script");
ESP_LOGI(TAG, "End of testing application.");